From 915383130a5171d62091d79db08d434ed5094c23 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Wed, 19 Jun 2024 12:06:11 +0300 Subject: [PATCH] cmd/atlas/internal/cmdlog: url DirURL in env info --- cmd/atlas/internal/cmdapi/migrate.go | 15 ++++++++++++--- cmd/atlas/internal/cmdext/reader.go | 2 +- cmd/atlas/internal/cmdlog/cmdlog.go | 11 ++++++----- cmd/atlas/internal/migrate/report.go | 5 ++++- doc/md/lint/analyzers.mdx | 4 ++-- .../testdata/mysql/cli-migrate-apply.txt | 2 +- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/cmd/atlas/internal/cmdapi/migrate.go b/cmd/atlas/internal/cmdapi/migrate.go index c9bad882ea8..c2a3f5e9824 100644 --- a/cmd/atlas/internal/cmdapi/migrate.go +++ b/cmd/atlas/internal/cmdapi/migrate.go @@ -173,8 +173,12 @@ func migrateApplyRun(cmd *cobra.Command, args []string, flags migrateApplyFlags, return fmt.Errorf("cannot apply '%d' migration files", count) } } + dirURL, err := url.Parse(flags.dirURL) + if err != nil { + return fmt.Errorf("parse dir-url: %w", err) + } // Open and validate the migration directory. - dir, err := cmdmigrate.Dir(ctx, flags.dirURL, false) + dir, err := cmdmigrate.DirURL(ctx, dirURL, false) if err != nil { return err } @@ -217,7 +221,7 @@ func migrateApplyRun(cmd *cobra.Command, args []string, flags migrateApplyFlags, return err } // Setup reporting info. - report := cmdlog.NewMigrateApply(ctx, client, dir) + report := cmdlog.NewMigrateApply(ctx, client, dirURL) mr.Init(client, report, mrrw) // If cloud reporting is enabled, and we cannot obtain the current // target identifier, abort and report it to the user. @@ -1298,7 +1302,11 @@ func migrateStatusCmd() *cobra.Command { func migrateStatusRun(cmd *cobra.Command, _ []string, flags migrateStatusFlags) error { ctx := cmd.Context() - dir, err := cmdmigrate.Dir(ctx, flags.dirURL, false) + dirURL, err := url.Parse(flags.dirURL) + if err != nil { + return fmt.Errorf("parse dir-url: %w", err) + } + dir, err := cmdmigrate.DirURL(ctx, dirURL, false) if err != nil { return err } @@ -1313,6 +1321,7 @@ func migrateStatusRun(cmd *cobra.Command, _ []string, flags migrateStatusFlags) report, err := (&cmdmigrate.StatusReporter{ Client: client, Dir: dir, + DirURL: dirURL, Schema: revisionSchemaName(client, flags.revisionSchema), }).Report(ctx) if err != nil { diff --git a/cmd/atlas/internal/cmdext/reader.go b/cmd/atlas/internal/cmdext/reader.go index c7d6a6ef301..1f058eedede 100644 --- a/cmd/atlas/internal/cmdext/reader.go +++ b/cmd/atlas/internal/cmdext/reader.go @@ -121,7 +121,7 @@ func stateSchemaSQL(ctx context.Context, cfg *StateReaderConfig, dir migrate.Dir if cfg.Dev == nil { return nil, errNoDevURL } - log := cmdlog.NewMigrateApply(ctx, cfg.Dev, dir) + log := cmdlog.NewMigrateApply(ctx, cfg.Dev, nil) r, err := stateReaderSQL(ctx, cfg, dir, []migrate.ExecutorOption{migrate.WithLogger(log)}, nil) if n := len(log.Applied); err != nil && n > 0 { if serr := log.Applied[n-1].Error; serr != nil && serr.Stmt != "" && serr.Text != "" { diff --git a/cmd/atlas/internal/cmdlog/cmdlog.go b/cmd/atlas/internal/cmdlog/cmdlog.go index a517cf12138..18900864ef2 100644 --- a/cmd/atlas/internal/cmdlog/cmdlog.go +++ b/cmd/atlas/internal/cmdlog/cmdlog.go @@ -9,6 +9,7 @@ import ( "context" "encoding/json" "fmt" + "net/url" "slices" "sort" "strings" @@ -102,13 +103,13 @@ func (f Files) MarshalJSON() ([]byte, error) { } // NewEnv returns an initialized Env. -func NewEnv(c *sqlclient.Client, dir migrate.Dir) Env { +func NewEnv(c *sqlclient.Client, dirURL *url.URL) Env { e := Env{ Driver: c.Name, URL: c.URL, } - if p, ok := dir.(interface{ Path() string }); ok { - e.Dir = p.Path() + if dirURL != nil { + e.Dir = dirURL.Redacted() } return e } @@ -361,10 +362,10 @@ type ( ) // NewMigrateApply returns an MigrateApply. -func NewMigrateApply(ctx context.Context, client *sqlclient.Client, dir migrate.Dir) *MigrateApply { +func NewMigrateApply(ctx context.Context, client *sqlclient.Client, dirURL *url.URL) *MigrateApply { return &MigrateApply{ ctx: ctx, - Env: NewEnv(client, dir), + Env: NewEnv(client, dirURL), Start: time.Now(), } } diff --git a/cmd/atlas/internal/migrate/report.go b/cmd/atlas/internal/migrate/report.go index 049708215f7..f2b22685c6a 100644 --- a/cmd/atlas/internal/migrate/report.go +++ b/cmd/atlas/internal/migrate/report.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "net/url" "strings" "ariga.io/atlas/cmd/atlas/internal/cmdlog" @@ -21,6 +22,8 @@ import ( type StatusReporter struct { // Client configures the connection to the database to file a MigrateStatus for. Client *sqlclient.Client + // DirURL of the migration directory. + DirURL *url.URL // Dir is used for scanning and validating the migration directory. Dir migrate.Dir // Schema name the revision table resides in. @@ -29,7 +32,7 @@ type StatusReporter struct { // Report creates and writes a MigrateStatus. func (r *StatusReporter) Report(ctx context.Context) (*cmdlog.MigrateStatus, error) { - rep := &cmdlog.MigrateStatus{Env: cmdlog.NewEnv(r.Client, r.Dir)} + rep := &cmdlog.MigrateStatus{Env: cmdlog.NewEnv(r.Client, r.DirURL)} // Check if there already is a revision table in the defined schema. // Inspect schema and check if the table does already exist. sch, err := r.Client.InspectSchema(ctx, r.Schema, &schema.InspectOptions{Tables: []string{revision.Table}}) diff --git a/doc/md/lint/analyzers.mdx b/doc/md/lint/analyzers.mdx index 102f982306b..c6ed289f2f1 100644 --- a/doc/md/lint/analyzers.mdx +++ b/doc/md/lint/analyzers.mdx @@ -391,8 +391,8 @@ entity while still pointing to its previous table name. e.g., here is how you ca 2. If renaming is desired but the previous version of the application uses the old table name, a temporary `VIEW` can be created to mimic the previous schema version in the deployment phase. However, the downside of this solution is -that mutations using the old table name will fail. Yet, if Atlas detects a consecutive statement with a -`CREATE VIEW `, it will ignore this check. +that mutations using the old table name might fail (e.g., if the VIEW is not [Updatable/Insertable](https://dev.mysql.com/doc/refman/8.4/en/view-updatability.html)). +Yet, if Atlas detects a consecutive statement with a `CREATE VIEW `, it will ignore this check. ```sql ALTER TABLE `users` RENAME TO `Users`; CREATE VIEW `users` AS SELECT * FROM `Users`; diff --git a/internal/integration/testdata/mysql/cli-migrate-apply.txt b/internal/integration/testdata/mysql/cli-migrate-apply.txt index 2b8741cf2b1..0eafc5b610d 100644 --- a/internal/integration/testdata/mysql/cli-migrate-apply.txt +++ b/internal/integration/testdata/mysql/cli-migrate-apply.txt @@ -49,7 +49,7 @@ atlas migrate apply --url URL --revisions-schema $db --log '{{ json . }}' validJSON stdout stdout '"Driver":"mysql"' stdout '"Scheme":"mysql"' -stdout '"Dir":"migrations"' +stdout '"Dir":"files://migrations"' stdout '"Target":"3"' stdout '"Pending":\[{"Name":"1_first.sql","Version":"1","Description":"first"},{"Name":"2_second.sql","Version":"2","Description":"second"},{"Name":"3_third.sql","Version":"3","Description":"third"}\]' stdout '"Applied":\["CREATE TABLE `users` \(`id` bigint NOT NULL AUTO_INCREMENT, `age` bigint NOT NULL, `name` varchar\(255\) NOT NULL, PRIMARY KEY \(`id`\)\) CHARSET utf8mb4 COLLATE utf8mb4_bin;"\]'