-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
153 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |