初始化
This commit is contained in:
@@ -1,113 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
var (
|
||||
client *ent.Client
|
||||
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
||||
func setUp() {
|
||||
db, err := sql.Open("pgx", "postgresql://postgres:postgres@127.0.0.1:5432/back_cover")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.Query("select 1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
drv := entsql.OpenDB(dialect.Postgres, db)
|
||||
client = ent.NewClient(ent.Driver(drv)).Debug()
|
||||
}
|
||||
|
||||
func tearDown() {
|
||||
err := client.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var commands = map[string]func(){
|
||||
"signals": initSignals,
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatal("please specify the command")
|
||||
}
|
||||
if len(os.Args) > 1 {
|
||||
setUp()
|
||||
defer tearDown()
|
||||
args := os.Args[1:]
|
||||
for _, cmd := range args {
|
||||
if v, ok := commands[cmd]; ok {
|
||||
log.Println("initialize", cmd)
|
||||
v()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func initSignals() {
|
||||
signalFilePath, _ := filepath.Abs("../doc/signal.csv")
|
||||
file, err := os.Open(signalFilePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Fatalf("%s is not found", signalFilePath)
|
||||
}
|
||||
log.Fatal("signals initialized failed!", err)
|
||||
}
|
||||
r := csv.NewReader(file)
|
||||
|
||||
client.Signal.Delete().ExecX(ctx)
|
||||
|
||||
line := 0
|
||||
for {
|
||||
records, err := r.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if line == 0 {
|
||||
line++
|
||||
continue
|
||||
}
|
||||
name := records[1]
|
||||
desc := records[2]
|
||||
isStatus, _ := strconv.Atoi(records[3])
|
||||
sender, _ := strconv.Atoi(records[4])
|
||||
address := records[5]
|
||||
offset, _ := strconv.Atoi(records[6])
|
||||
client.Signal.Create().
|
||||
SetID(line).
|
||||
SetIsStatus(isStatus).
|
||||
SetName(name).
|
||||
SetDescription(desc).
|
||||
SetSender(sender).
|
||||
SetAddress(address).
|
||||
SetReplyOffset(offset).
|
||||
SaveX(ctx)
|
||||
line++
|
||||
}
|
||||
|
||||
log.Println("signals initialized successfully!")
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"entgo.io/ent/entc"
|
||||
"entgo.io/ent/entc/gen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := entc.Generate(
|
||||
".",
|
||||
&gen.Config{
|
||||
Target: "../ent",
|
||||
Package: "bj_power_mes/ent",
|
||||
Features: []gen.Feature{
|
||||
gen.FeatureExecQuery, // Allows users to execute statements using the ExecContext/QueryContext methods of the underlying driver
|
||||
gen.FeatureLock, // Allows users to use row-level locking in SQL using the 'FOR {UPDATE|SHARE}' clauses
|
||||
gen.FeatureModifier, // Allows users to attach custom modifiers to queries
|
||||
gen.FeatureNamedEdges, // NamedEdges provides an API for eager-loading edges with dynamic names
|
||||
},
|
||||
}); err != nil {
|
||||
log.Fatal("running ent codegen:", err)
|
||||
}
|
||||
log.Println("ent code generated successfully!")
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/db"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/schema"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
)
|
||||
|
||||
// 建表工具:DSN 一律从 etc 配置读取,不再硬编码
|
||||
// 用法(在 bj_power_mes 目录下):go run schema/tools/migrate.go
|
||||
|
||||
var (
|
||||
client *ent.Client
|
||||
|
||||
ctx = context.Background()
|
||||
configFile = flag.String("config", "etc/bj_power_mes-api.yaml", "配置文件路径")
|
||||
)
|
||||
|
||||
// migrateConf 与主配置结构一致,只取 Database 段
|
||||
type migrateConf struct {
|
||||
Database db.DatabaseConf
|
||||
}
|
||||
|
||||
func setUp() {
|
||||
var c migrateConf
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
|
||||
c.Database.Host, c.Database.Port, c.Database.User, c.Database.Password, c.Database.Dbname)
|
||||
db, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err = db.Query("select 1"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
drv := entsql.OpenDB(dialect.Postgres, db)
|
||||
client = ent.NewClient(ent.Driver(drv))
|
||||
}
|
||||
|
||||
func tearDown() {
|
||||
err := client.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
setUp()
|
||||
defer tearDown()
|
||||
|
||||
// Run the auto migration tool.
|
||||
// WithDropColumn(false): 只建新表/加列,不删任何列,保证生产数据安全
|
||||
if err := client.Debug().Schema.Create(
|
||||
context.Background(),
|
||||
schema.WithDropIndex(true),
|
||||
schema.WithDropColumn(false),
|
||||
schema.WithDialect(dialect.Postgres), // Ent dialect to use
|
||||
schema.WithForeignKeys(false), // 不创建外键约束
|
||||
); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
log.Println("schemas migrated successfully!")
|
||||
}
|
||||
Reference in New Issue
Block a user