diff --git a/.env.example_2 b/.env.example_2 new file mode 100644 index 0000000..073116a --- /dev/null +++ b/.env.example_2 @@ -0,0 +1,4 @@ +TBLS_DATABASE_NAME=db-example +TBLS_DATABASE_SCHEMA=example2_schema +TBLS_MIGRATION_DIR=example/migrations +TBLS_CONFIG_FILE=example2.tbls.yml \ No newline at end of file diff --git a/.gitignore b/.gitignore index ecd47cc..7ca1d22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/.env +/*.env dist/ -dist/ +dist/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8637a21 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +dbdoc: + @echo "generate schema example" + go run cmd/tblsrun/tblsrun.go postgres docker + @echo "generate schema example 2" + go run cmd/tblsrun/tblsrun.go --env-file=.env.example_2 postgres docker \ No newline at end of file diff --git a/README.md b/README.md index f8af8ef..cd20631 100644 --- a/README.md +++ b/README.md @@ -55,5 +55,9 @@ Available Commands: postgres Run tbls with postgres Flags: + --env-file string --env-file="custom.env" (default ".env") -h, --help help for tblsrun ``` + +## Testing +To do manual test we can run `make dbdoc` and check there should be 2 output \ No newline at end of file diff --git a/cmd/tblsrun/main.go b/cmd/tblsrun/tblsrun.go similarity index 79% rename from cmd/tblsrun/main.go rename to cmd/tblsrun/tblsrun.go index 30c9bf7..3b85073 100644 --- a/cmd/tblsrun/main.go +++ b/cmd/tblsrun/tblsrun.go @@ -20,22 +20,22 @@ func main() { } } -var cfg tblsrun.Config +var ( + envFile string +) -func run(args []string) (err error) { - cfg, err = tblsrun.NewConfig(".env") - if err != nil { - return fmt.Errorf("new config: %w", err) - } +const defaultEnvFile = ".env" +func run(args []string) (err error) { cmd := &cobra.Command{ Use: "tblsrun", Short: "Generate database documentation from migration files", } cmd.SetArgs(args[1:]) - cmd.AddCommand(cmdPostgres()) + cmd.PersistentFlags().StringVar(&envFile, "env-file", defaultEnvFile, `--env-file="custom.env"`) + cmd.AddCommand(cmdPostgres()) return cmd.Execute() } @@ -53,6 +53,11 @@ func cmdPostgresDocker() *cobra.Command { Use: "docker", Short: "Run tbls with postgres in docker", RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := tblsrun.NewConfig(envFile) + if err != nil { + return fmt.Errorf("new config: %w", err) + } + return runner. NewRunner( cfg, @@ -69,6 +74,11 @@ func cmdPostgresEmbedded() *cobra.Command { Use: "embedded", Short: "Run tbls with embedded postgres", RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := tblsrun.NewConfig(envFile) + if err != nil { + return fmt.Errorf("new config: %w", err) + } + return runner. NewRunner( cfg, diff --git a/config.go b/config.go index bb0e262..02aa087 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" + _ "github.com/davecgh/go-spew/spew" "github.com/joeshaw/envdecode" "github.com/joho/godotenv" ) diff --git a/example/dbdoc/schema.json b/example/dbdoc/schema.json index 6b91d7f..d378d0c 100644 --- a/example/dbdoc/schema.json +++ b/example/dbdoc/schema.json @@ -1 +1 @@ -{"name":"db-example","desc":"","tables":[{"name":"example_schema.schema_migrations","type":"BASE TABLE","comment":"","columns":[{"name":"version","type":"bigint","nullable":false,"default":null,"comment":""},{"name":"dirty","type":"boolean","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"schema_migrations_pkey","def":"CREATE UNIQUE INDEX schema_migrations_pkey ON example_schema.schema_migrations USING btree (version)","table":"example_schema.schema_migrations","columns":["version"],"comment":""}],"constraints":[{"name":"schema_migrations_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (version)","table":"example_schema.schema_migrations","referenced_table":"","columns":["version"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""},{"name":"example_schema.example","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"integer","nullable":false,"default":"nextval('example_id_seq'::regclass)","comment":""},{"name":"name","type":"varchar(255)","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"example_pkey","def":"CREATE UNIQUE INDEX example_pkey ON example_schema.example USING btree (id)","table":"example_schema.example","columns":["id"],"comment":""}],"constraints":[{"name":"example_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"example_schema.example","referenced_table":"","columns":["id"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""},{"name":"example_schema.example_2","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"integer","nullable":false,"default":"nextval('example_2_id_seq'::regclass)","comment":""},{"name":"name","type":"varchar(255)","nullable":false,"default":null,"comment":""},{"name":"example_1_id","type":"integer","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"example_2_pkey","def":"CREATE UNIQUE INDEX example_2_pkey ON example_schema.example_2 USING btree (id)","table":"example_schema.example_2","columns":["id"],"comment":""}],"constraints":[{"name":"example_2_example_1_id_fkey","type":"FOREIGN KEY","def":"FOREIGN KEY (example_1_id) REFERENCES example(id)","table":"example_schema.example_2","referenced_table":"example","columns":["example_1_id"],"referenced_columns":["id"],"comment":""},{"name":"example_2_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"example_schema.example_2","referenced_table":"","columns":["id"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""}],"relations":[{"table":"example_schema.example_2","columns":["example_1_id"],"cardinality":"Zero or more","parent_table":"example_schema.example","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (example_1_id) REFERENCES example(id)","virtual":false}],"functions":[],"driver":{"name":"postgres","database_version":"PostgreSQL 13.9 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4, 64-bit","meta":{"current_schema":"example_schema","search_paths":["example_schema"],"dict":{"Functions":"Stored procedures and functions"}}}} +{"name":"db-example","desc":"","tables":[{"name":"example_schema.schema_migrations","type":"BASE TABLE","comment":"","columns":[{"name":"version","type":"bigint","nullable":false,"default":null,"comment":""},{"name":"dirty","type":"boolean","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"schema_migrations_pkey","def":"CREATE UNIQUE INDEX schema_migrations_pkey ON example_schema.schema_migrations USING btree (version)","table":"example_schema.schema_migrations","columns":["version"],"comment":""}],"constraints":[{"name":"schema_migrations_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (version)","table":"example_schema.schema_migrations","referenced_table":"","columns":["version"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""},{"name":"example_schema.example","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"integer","nullable":false,"default":"nextval('example_id_seq'::regclass)","comment":""},{"name":"name","type":"varchar(255)","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"example_pkey","def":"CREATE UNIQUE INDEX example_pkey ON example_schema.example USING btree (id)","table":"example_schema.example","columns":["id"],"comment":""}],"constraints":[{"name":"example_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"example_schema.example","referenced_table":"","columns":["id"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""},{"name":"example_schema.example_2","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"integer","nullable":false,"default":"nextval('example_2_id_seq'::regclass)","comment":""},{"name":"name","type":"varchar(255)","nullable":false,"default":null,"comment":""},{"name":"example_1_id","type":"integer","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"example_2_pkey","def":"CREATE UNIQUE INDEX example_2_pkey ON example_schema.example_2 USING btree (id)","table":"example_schema.example_2","columns":["id"],"comment":""}],"constraints":[{"name":"example_2_example_1_id_fkey","type":"FOREIGN KEY","def":"FOREIGN KEY (example_1_id) REFERENCES example(id)","table":"example_schema.example_2","referenced_table":"example","columns":["example_1_id"],"referenced_columns":["id"],"comment":""},{"name":"example_2_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"example_schema.example_2","referenced_table":"","columns":["id"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""}],"relations":[{"table":"example_schema.example_2","columns":["example_1_id"],"cardinality":"Zero or more","parent_table":"example_schema.example","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (example_1_id) REFERENCES example(id)","virtual":false}],"functions":[],"driver":{"name":"postgres","database_version":"PostgreSQL 13.8 (Debian 13.8-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit","meta":{"current_schema":"example_schema","search_paths":["example_schema"],"dict":{"Functions":"Stored procedures and functions"}}}} diff --git a/example2.tbls.yml b/example2.tbls.yml new file mode 100644 index 0000000..4be7604 --- /dev/null +++ b/example2.tbls.yml @@ -0,0 +1,2 @@ +# Path to generate document +docPath: example2/dbdoc/ \ No newline at end of file diff --git a/example2/dbdoc/README.md b/example2/dbdoc/README.md new file mode 100644 index 0000000..6d0a1e2 --- /dev/null +++ b/example2/dbdoc/README.md @@ -0,0 +1,17 @@ +# db-example + +## Tables + +| Name | Columns | Comment | Type | +| ---- | ------- | ------- | ---- | +| [example2_schema.schema_migrations](example2_schema.schema_migrations.md) | 2 | | BASE TABLE | +| [example2_schema.example](example2_schema.example.md) | 2 | | BASE TABLE | +| [example2_schema.example_2](example2_schema.example_2.md) | 3 | | BASE TABLE | + +## Relations + +![er](schema.svg) + +--- + +> Generated by [tbls](https://github.com/k1LoW/tbls) diff --git a/example2/dbdoc/example2_schema.example.md b/example2/dbdoc/example2_schema.example.md new file mode 100644 index 0000000..1407ee1 --- /dev/null +++ b/example2/dbdoc/example2_schema.example.md @@ -0,0 +1,30 @@ +# example2_schema.example + +## Description + +## Columns + +| Name | Type | Default | Nullable | Children | Parents | Comment | +| ---- | ---- | ------- | -------- | -------- | ------- | ------- | +| id | integer | nextval('example_id_seq'::regclass) | false | [example2_schema.example_2](example2_schema.example_2.md) | | | +| name | varchar(255) | | false | | | | + +## Constraints + +| Name | Type | Definition | +| ---- | ---- | ---------- | +| example_pkey | PRIMARY KEY | PRIMARY KEY (id) | + +## Indexes + +| Name | Definition | +| ---- | ---------- | +| example_pkey | CREATE UNIQUE INDEX example_pkey ON example2_schema.example USING btree (id) | + +## Relations + +![er](example2_schema.example.svg) + +--- + +> Generated by [tbls](https://github.com/k1LoW/tbls) diff --git a/example2/dbdoc/example2_schema.example.svg b/example2/dbdoc/example2_schema.example.svg new file mode 100644 index 0000000..681b7ee --- /dev/null +++ b/example2/dbdoc/example2_schema.example.svg @@ -0,0 +1,54 @@ + + + + + + +example2_schema.example + + + +example2_schema.example + + +example2_schema.example + +[BASE TABLE] + +id +[integer] + +name +[varchar(255)] + + + + +example2_schema.example_2 + + +example2_schema.example_2 + +[BASE TABLE] + +id +[integer] + +name +[varchar(255)] + +example_1_id +[integer] + + + +example2_schema.example_2:example_1_id->example2_schema.example:id + + +FOREIGN KEY (example_1_id) REFERENCES example(id) + + + diff --git a/example2/dbdoc/example2_schema.example_2.md b/example2/dbdoc/example2_schema.example_2.md new file mode 100644 index 0000000..ac5e0e6 --- /dev/null +++ b/example2/dbdoc/example2_schema.example_2.md @@ -0,0 +1,32 @@ +# example2_schema.example_2 + +## Description + +## Columns + +| Name | Type | Default | Nullable | Children | Parents | Comment | +| ---- | ---- | ------- | -------- | -------- | ------- | ------- | +| id | integer | nextval('example_2_id_seq'::regclass) | false | | | | +| name | varchar(255) | | false | | | | +| example_1_id | integer | | false | | [example2_schema.example](example2_schema.example.md) | | + +## Constraints + +| Name | Type | Definition | +| ---- | ---- | ---------- | +| example_2_example_1_id_fkey | FOREIGN KEY | FOREIGN KEY (example_1_id) REFERENCES example(id) | +| example_2_pkey | PRIMARY KEY | PRIMARY KEY (id) | + +## Indexes + +| Name | Definition | +| ---- | ---------- | +| example_2_pkey | CREATE UNIQUE INDEX example_2_pkey ON example2_schema.example_2 USING btree (id) | + +## Relations + +![er](example2_schema.example_2.svg) + +--- + +> Generated by [tbls](https://github.com/k1LoW/tbls) diff --git a/example2/dbdoc/example2_schema.example_2.svg b/example2/dbdoc/example2_schema.example_2.svg new file mode 100644 index 0000000..51230d5 --- /dev/null +++ b/example2/dbdoc/example2_schema.example_2.svg @@ -0,0 +1,54 @@ + + + + + + +example2_schema.example_2 + + + +example2_schema.example_2 + + +example2_schema.example_2 + +[BASE TABLE] + +id +[integer] + +name +[varchar(255)] + +example_1_id +[integer] + + + + +example2_schema.example + + +example2_schema.example + +[BASE TABLE] + +id +[integer] + +name +[varchar(255)] + + + +example2_schema.example_2:example_1_id->example2_schema.example:id + + +FOREIGN KEY (example_1_id) REFERENCES example(id) + + + diff --git a/example2/dbdoc/example2_schema.schema_migrations.md b/example2/dbdoc/example2_schema.schema_migrations.md new file mode 100644 index 0000000..361d74a --- /dev/null +++ b/example2/dbdoc/example2_schema.schema_migrations.md @@ -0,0 +1,30 @@ +# example2_schema.schema_migrations + +## Description + +## Columns + +| Name | Type | Default | Nullable | Children | Parents | Comment | +| ---- | ---- | ------- | -------- | -------- | ------- | ------- | +| version | bigint | | false | | | | +| dirty | boolean | | false | | | | + +## Constraints + +| Name | Type | Definition | +| ---- | ---- | ---------- | +| schema_migrations_pkey | PRIMARY KEY | PRIMARY KEY (version) | + +## Indexes + +| Name | Definition | +| ---- | ---------- | +| schema_migrations_pkey | CREATE UNIQUE INDEX schema_migrations_pkey ON example2_schema.schema_migrations USING btree (version) | + +## Relations + +![er](example2_schema.schema_migrations.svg) + +--- + +> Generated by [tbls](https://github.com/k1LoW/tbls) diff --git a/example2/dbdoc/example2_schema.schema_migrations.svg b/example2/dbdoc/example2_schema.schema_migrations.svg new file mode 100644 index 0000000..ed36553 --- /dev/null +++ b/example2/dbdoc/example2_schema.schema_migrations.svg @@ -0,0 +1,29 @@ + + + + + + +example2_schema.schema_migrations + + + +example2_schema.schema_migrations + + +example2_schema.schema_migrations + +[BASE TABLE] + +version +[bigint] + +dirty +[boolean] + + + + diff --git a/example2/dbdoc/schema.json b/example2/dbdoc/schema.json new file mode 100644 index 0000000..b8fcd60 --- /dev/null +++ b/example2/dbdoc/schema.json @@ -0,0 +1 @@ +{"name":"db-example","desc":"","tables":[{"name":"example2_schema.schema_migrations","type":"BASE TABLE","comment":"","columns":[{"name":"version","type":"bigint","nullable":false,"default":null,"comment":""},{"name":"dirty","type":"boolean","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"schema_migrations_pkey","def":"CREATE UNIQUE INDEX schema_migrations_pkey ON example2_schema.schema_migrations USING btree (version)","table":"example2_schema.schema_migrations","columns":["version"],"comment":""}],"constraints":[{"name":"schema_migrations_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (version)","table":"example2_schema.schema_migrations","referenced_table":"","columns":["version"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""},{"name":"example2_schema.example","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"integer","nullable":false,"default":"nextval('example_id_seq'::regclass)","comment":""},{"name":"name","type":"varchar(255)","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"example_pkey","def":"CREATE UNIQUE INDEX example_pkey ON example2_schema.example USING btree (id)","table":"example2_schema.example","columns":["id"],"comment":""}],"constraints":[{"name":"example_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"example2_schema.example","referenced_table":"","columns":["id"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""},{"name":"example2_schema.example_2","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"integer","nullable":false,"default":"nextval('example_2_id_seq'::regclass)","comment":""},{"name":"name","type":"varchar(255)","nullable":false,"default":null,"comment":""},{"name":"example_1_id","type":"integer","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"example_2_pkey","def":"CREATE UNIQUE INDEX example_2_pkey ON example2_schema.example_2 USING btree (id)","table":"example2_schema.example_2","columns":["id"],"comment":""}],"constraints":[{"name":"example_2_example_1_id_fkey","type":"FOREIGN KEY","def":"FOREIGN KEY (example_1_id) REFERENCES example(id)","table":"example2_schema.example_2","referenced_table":"example","columns":["example_1_id"],"referenced_columns":["id"],"comment":""},{"name":"example_2_pkey","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"example2_schema.example_2","referenced_table":"","columns":["id"],"referenced_columns":[],"comment":""}],"triggers":[],"def":""}],"relations":[{"table":"example2_schema.example_2","columns":["example_1_id"],"cardinality":"Zero or more","parent_table":"example2_schema.example","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (example_1_id) REFERENCES example(id)","virtual":false}],"functions":[],"driver":{"name":"postgres","database_version":"PostgreSQL 13.8 (Debian 13.8-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit","meta":{"current_schema":"example2_schema","search_paths":["example2_schema"],"dict":{"Functions":"Stored procedures and functions"}}}} diff --git a/example2/dbdoc/schema.svg b/example2/dbdoc/schema.svg new file mode 100644 index 0000000..6f5c3f6 --- /dev/null +++ b/example2/dbdoc/schema.svg @@ -0,0 +1,68 @@ + + + + + + +db-example + + + +example2_schema.schema_migrations + + +example2_schema.schema_migrations + +[BASE TABLE] + +version +[bigint] + +dirty +[boolean] + + + +example2_schema.example + + +example2_schema.example + +[BASE TABLE] + +id +[integer] + +name +[varchar(255)] + + + +example2_schema.example_2 + + +example2_schema.example_2 + +[BASE TABLE] + +id +[integer] + +name +[varchar(255)] + +example_1_id +[integer] + + + +example2_schema.example_2:example_1_id->example2_schema.example:id + + +FOREIGN KEY (example_1_id) REFERENCES example(id) + + +