Skip to content

Commit

Permalink
fix gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
covrom committed Sep 1, 2021
1 parent 9550192 commit b10ed6c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 8 deletions.
14 changes: 6 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@

# Dependency directories (remove the comment below to include it)
# vendor/
schema.yaml
goerd
schema.puml
schema copy.yaml
sql/test_ddl_full.sql
schema/yaml_test.go
schema2.puml
schema2.yaml

/goerd
/cmd/goerd/goerd
/main
/sql/test_ddl_full.sql
/schema/yaml_test.go
.vscode/launch.json
111 changes: 111 additions & 0 deletions cmd/goerd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"flag"
"fmt"
"log"
"os"

"github.com/covrom/goerd/datasource"
"github.com/covrom/goerd/schema"
_ "github.com/jackc/pgx/v4/stdlib"
)

var (
dsn = flag.String("dsn", "", "Build a DSN e.g. postgres://username:password@url:port/dbName")
inyml = flag.String("iy", "", "input yaml filename")
yml = flag.String("oy", "schema.yaml", "output yaml filename")
pml = flag.String("op", "schema.puml", "output plant uml filename")
dist = flag.Int("opdist", 2, "distance for plant uml")
fromyml = flag.String("from", "", "source schema yaml filename")
toyml = flag.String("to", "", "destination schema yaml filename")
)

func main() {
flag.Parse()
if (*dsn == "" && *inyml == "" && *fromyml == "" && *toyml == "") ||
(*yml == "" && *pml == "" && *fromyml == "" && *toyml == "") {
flag.Usage()
return
}

s := &schema.Schema{}

if *dsn != "" {
var err error
s, err = datasource.Analyze(*dsn)
if err != nil {
log.Fatal(err)
}
} else if *inyml != "" {
f, err := os.Open(*inyml)
if err != nil {
log.Fatal(err)
}
if err := s.LoadYaml(f); err != nil {
f.Close()
log.Fatal(err)
}
f.Close()
}

if *yml != "" {
wr, err := os.OpenFile(*yml, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}

if err := s.SaveYaml(wr); err != nil {
wr.Close()
log.Fatal(err)
}
wr.Close()
}

if *pml != "" {
wr, err := os.OpenFile(*pml, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}

if err := s.SavePlantUml(wr, *dist); err != nil {
wr.Close()
log.Fatal(err)
}
wr.Close()
}

if *fromyml != "" || *toyml != "" {
sfrom := &schema.Schema{}
if *fromyml != "" {
ffrom, err := os.Open(*fromyml)
if err != nil {
log.Fatal(err)
}
if err := sfrom.LoadYaml(ffrom); err != nil {
ffrom.Close()
log.Fatal(err)
}
ffrom.Close()
}
sto := &schema.Schema{}
if *toyml != "" {
fto, err := os.Open(*toyml)
if err != nil {
log.Fatal(err)
}
if err := sto.LoadYaml(fto); err != nil {
fto.Close()
log.Fatal(err)
}
fto.Close()
}

ptch := &schema.PatchSchema{CurrentSchema: sfrom.CurrentSchema}
ptch.Build(sfrom, sto)
qs := ptch.GenerateSQL()
for _, q := range qs {
fmt.Println(q)
}
}
}
36 changes: 36 additions & 0 deletions output/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: ""
schema: public
tables:
accounts:
type: TABLE
columns:
created_on:
type: timestamp without time zone
email:
type: varchar(255)
last_login:
type: timestamp without time zone
nullable: true
parent_id:
type: integer
nullable: true
password:
type: varchar(50)
user_id:
type: integer
pk: true
default: nextval('accounts_user_id_seq'::regclass)
username:
type: varchar(50)
constraints:
accounts_email_key:
type: UNIQUE
columns: [email]
accounts_username_key:
type: UNIQUE
columns: [username]
relations:
accounts:
columns: [parent_id]
parentColumns: [user_id]
onDelete: CASCADE

0 comments on commit b10ed6c

Please sign in to comment.