Skip to content

Commit

Permalink
feat: override config to support multi db/schema in on repository (#3)
Browse files Browse the repository at this point in the history
* feat: override config to support multi db/schema in on repo

* doc: update commands
  • Loading branch information
fahmifan authored Mar 7, 2023
1 parent ef4e5b5 commit 4c98087
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .env.example_2
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.env
/*.env
dist/

dist/
dist/
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 17 additions & 7 deletions cmd/tblsrun/main.go → cmd/tblsrun/tblsrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"

_ "github.com/davecgh/go-spew/spew"
"github.com/joeshaw/envdecode"
"github.com/joho/godotenv"
)
Expand Down
2 changes: 1 addition & 1 deletion example/dbdoc/schema.json
Original file line number Diff line number Diff line change
@@ -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"}}}}
2 changes: 2 additions & 0 deletions example2.tbls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Path to generate document
docPath: example2/dbdoc/
17 changes: 17 additions & 0 deletions example2/dbdoc/README.md
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions example2/dbdoc/example2_schema.example.md
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 54 additions & 0 deletions example2/dbdoc/example2_schema.example.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions example2/dbdoc/example2_schema.example_2.md
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 54 additions & 0 deletions example2/dbdoc/example2_schema.example_2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4c98087

Please sign in to comment.