Skip to content

Commit

Permalink
schema/inspect: support inspect for non-database resource (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Apr 15, 2023
1 parent c9294dc commit 02312ad
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 35 deletions.
37 changes: 27 additions & 10 deletions cmd/atlas/internal/cmdapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ func schemaDiffRun(cmd *cobra.Command, _ []string, flags schemaDiffFlags) error
}

type schemaInspectFlags struct {
url string // URL of database to apply the changes on.
url string // URL of resource to inspect.
devURL string // URL of the dev database.
logFormat string // Format of the log output.
schemas []string // Schemas to take into account when diffing.
exclude []string // List of glob patterns used to filter resources from applying (see schema.InspectOptions).
Expand Down Expand Up @@ -435,6 +436,7 @@ flag.
)
cmd.Flags().SortFlags = false
addFlagURL(cmd.Flags(), &flags.url)
addFlagDevURL(cmd.Flags(), &flags.devURL)
addFlagSchemas(cmd.Flags(), &flags.schemas)
addFlagExclude(cmd.Flags(), &flags.exclude)
addFlagLog(cmd.Flags(), &flags.logFormat)
Expand All @@ -445,25 +447,40 @@ flag.
}

func schemaInspectRun(cmd *cobra.Command, _ []string, flags schemaInspectFlags) error {
client, err := sqlclient.Open(cmd.Context(), flags.url)
var (
ctx = cmd.Context()
dev *sqlclient.Client
)
if flags.devURL != "" {
var err error
dev, err = sqlclient.Open(ctx, flags.devURL)
if err != nil {
return err
}
defer dev.Close()
}
r, err := stateReader(ctx, &stateReaderConfig{
urls: []string{flags.url},
dev: dev,
vars: GlobalFlags.Vars,
schemas: flags.schemas,
exclude: flags.exclude,
})
if err != nil {
return err
}
defer client.Close()
schemas := flags.schemas
if client.URL.Schema != "" {
schemas = append(schemas, client.URL.Schema)
defer r.Close()
client, ok := r.Closer.(*sqlclient.Client)
if !ok && dev != nil {
client = dev
}
s, err := client.InspectRealm(cmd.Context(), &schema.InspectRealmOption{
Schemas: schemas,
Exclude: flags.exclude,
})
format := cmdlog.SchemaInspectTemplate
if v := flags.logFormat; v != "" {
if format, err = template.New("format").Funcs(cmdlog.InspectTemplateFuncs).Parse(v); err != nil {
return fmt.Errorf("parse log format: %w", err)
}
}
s, err := r.ReadState(ctx)
return format.Execute(cmd.OutOrStdout(), &cmdlog.SchemaInspect{
Client: client,
Realm: s,
Expand Down
1 change: 1 addition & 0 deletions doc/md/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ flag.
#### Flags
```
-u, --url string [driver://username:password@address/dbname?param=value] select a resource using the URL format
--dev-url string [driver://username:password@address/dbname?param=value] select a dev database using the URL format
-s, --schema strings set schema names
--exclude strings list of glob patterns used to filter resources from applying
--format string Go template to use to format the output
Expand Down
38 changes: 38 additions & 0 deletions internal/integration/testdata/mysql/cli-inspect-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
only mysql8

# inspect without dev-db will failed
! atlas schema inspect -u file://a.sql
stderr 'Error: --dev-url cannot be empty'

# inspect file to HCL
atlas schema inspect -u file://a.sql --dev-url URL > inspected.hcl
cmp inspected.hcl script_cli_inspect.hcl

# inspect file to SQL
atlas schema inspect -u file://a.sql --dev-url URL --format '{{ sql . }}' > inspected.sql
cmp inspected.sql script_cli_inspect.sql

-- a.sql --
create table users (
id int NOT NULL,
PRIMARY KEY (id)
)

-- script_cli_inspect.hcl --
table "users" {
schema = schema.script_cli_inspect_file
column "id" {
null = false
type = int
}
primary_key {
columns = [column.id]
}
}
schema "script_cli_inspect_file" {
charset = "utf8mb4"
collate = "utf8mb4_0900_ai_ci"
}
-- script_cli_inspect.sql --
-- Create "users" table
CREATE TABLE `users` (`id` int NOT NULL, PRIMARY KEY (`id`)) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
34 changes: 34 additions & 0 deletions internal/integration/testdata/postgres/cli-inspect-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# inspect without dev-db will failed
! atlas schema inspect -u file://a.sql
stderr 'Error: --dev-url cannot be empty'

# inspect file to HCL
atlas schema inspect -u file://a.sql --dev-url URL > inspected.hcl
cmp inspected.hcl script_cli_inspect.hcl

# inspect file to SQL
atlas schema inspect -u file://a.sql --dev-url URL --format '{{ sql . }}' > inspected.sql
cmp inspected.sql script_cli_inspect.sql

-- a.sql --
create table users (
id int NOT NULL,
PRIMARY KEY (id)
)

-- script_cli_inspect.hcl --
table "users" {
schema = schema.script_cli_inspect_file
column "id" {
null = false
type = integer
}
primary_key {
columns = [column.id]
}
}
schema "script_cli_inspect_file" {
}
-- script_cli_inspect.sql --
-- Create "users" table
CREATE TABLE "users" ("id" integer NOT NULL, PRIMARY KEY ("id"));
92 changes: 92 additions & 0 deletions internal/integration/testdata/postgres/cli-inspect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
apply 1.hcl

# test url flag
atlas schema inspect -u URL > inspected.hcl
cmp inspected.hcl script_cli_inspect.hcl

# test exclude flag on table.
atlas schema inspect -u URL --exclude "users" > inspected.hcl
cmp inspected.hcl notable.hcl

# test exclude flag on column.
atlas schema inspect -u URL --exclude "*.[ab]*" > inspected.hcl
cmp inspected.hcl id.hcl

# test exclude flag on column.
atlas schema inspect -u URL --exclude "*.*" > inspected.hcl
cmp inspected.hcl nocolumn.hcl


-- 1.hcl --
table "users" {
schema = schema.$db
column "id" {
null = false
type = int
}
column "a" {
null = false
type = int
}
column "b" {
null = false
type = int
}
column "ab" {
null = false
type = int
}
column "ac" {
null = false
type = int4
}
}
schema "$db" {
}

-- script_cli_inspect.hcl --
table "users" {
schema = schema.script_cli_inspect
column "id" {
null = false
type = integer
}
column "a" {
null = false
type = integer
}
column "b" {
null = false
type = integer
}
column "ab" {
null = false
type = integer
}
column "ac" {
null = false
type = integer
}
}
schema "script_cli_inspect" {
}
-- empty.hcl --
-- notable.hcl --
schema "script_cli_inspect" {
}
-- id.hcl --
table "users" {
schema = schema.script_cli_inspect
column "id" {
null = false
type = integer
}
}
schema "script_cli_inspect" {
}
-- nocolumn.hcl --
table "users" {
schema = schema.script_cli_inspect
}
schema "script_cli_inspect" {
}
18 changes: 9 additions & 9 deletions internal/integration/testdata/postgres/cli-migrate-apply.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ stderr 'unknown tx-mode "invalid"'

! atlas migrate apply --url URL --revisions-schema $db --tx-mode all
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions
atlas schema inspect --url URL --exclude atlas_schema_revisions
cmp stdout empty.hcl

# Apply one migration, after rolling everything back, the first revision must still exist.
atlas migrate apply --url URL --revisions-schema $db 1
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users
atlas schema inspect --url URL --exclude atlas_schema_revisions --exclude users
cmp stdout empty.hcl
cmpshow users users_1.sql

! atlas migrate apply --url URL --revisions-schema $db --tx-mode all
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users
atlas schema inspect --url URL --exclude atlas_schema_revisions --exclude users
cmp stdout empty.hcl

# If the broken migration is gone, we can apply everything without any problems.
Expand All @@ -75,7 +75,7 @@ atlas migrate hash
atlas migrate apply --url URL --revisions-schema $db
cmpshow users users.sql
cmpshow pets pets.sql
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
atlas schema inspect --url URL --exclude atlas_schema_revisions --exclude users --exclude pets
cmp stdout empty.hcl

clearSchema
Expand All @@ -87,12 +87,12 @@ atlas migrate hash

! atlas migrate apply --url URL --revisions-schema $db --tx-mode file
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions
atlas schema inspect --url URL --exclude atlas_schema_revisions
cmpshow users users.sql
cmpshow pets pets.sql

# Table "broken" does not exist since we rolled back that migration.
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
atlas schema inspect --url URL --exclude atlas_schema_revisions --exclude users --exclude pets
cmp stdout empty.hcl

# If the broken migration is gone, we can apply everything without any problems.
Expand All @@ -102,7 +102,7 @@ atlas migrate hash
atlas migrate apply --url URL --revisions-schema $db
cmpshow users users.sql
cmpshow pets pets.sql
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
atlas schema inspect --url URL --exclude atlas_schema_revisions --exclude users --exclude pets
cmp stdout empty.hcl

clearSchema
Expand All @@ -114,12 +114,12 @@ atlas migrate hash

! atlas migrate apply --url URL --revisions-schema $db --tx-mode none
stderr 'executing statement "THIS IS A FAILING STATEMENT;" from version "4"'
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions
atlas schema inspect --url URL --exclude atlas_schema_revisions
cmpshow users users.sql
cmpshow pets pets.sql

# Table "broken" does exist since we do not have transactions.
atlas schema inspect --url URL --exclude $db.atlas_schema_revisions --exclude $db.users --exclude $db.pets
atlas schema inspect --url URL --exclude atlas_schema_revisions --exclude users --exclude pets
cmp stdout broken.hcl

-- migrations/1_first.sql --
Expand Down
10 changes: 3 additions & 7 deletions internal/integration/testdata/sqlite/cli-inspect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ apply 1.hcl
atlas schema inspect -u URL > inspected.hcl
cmp inspected.hcl 1.hcl

# test exclude flag on schema.
atlas schema inspect -u URL --exclude "main" > inspected.hcl
cmp inspected.hcl empty.hcl

# test exclude flag on table.
atlas schema inspect -u URL --exclude "*.users" > inspected.hcl
atlas schema inspect -u URL --exclude "users" > inspected.hcl
cmp inspected.hcl notable.hcl

# test exclude flag on column.
atlas schema inspect -u URL --exclude "main.*.[ab]*" > inspected.hcl
atlas schema inspect -u URL --exclude "*.[ab]*" > inspected.hcl
cmp inspected.hcl id.hcl

# test exclude flag on column.
atlas schema inspect -u URL --exclude "*.*.*" > inspected.hcl
atlas schema inspect -u URL --exclude "*.*" > inspected.hcl
cmp inspected.hcl nocolumn.hcl


Expand Down
Loading

0 comments on commit 02312ad

Please sign in to comment.