Skip to content

Commit

Permalink
cmd/atlas/internal/cmdlog: url DirURL in env info
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Jun 19, 2024
1 parent 19fe084 commit 9153831
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
15 changes: 12 additions & 3 deletions cmd/atlas/internal/cmdapi/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/atlas/internal/cmdext/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
11 changes: 6 additions & 5 deletions cmd/atlas/internal/cmdlog/cmdlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(),
}
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/atlas/internal/migrate/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"strings"

"ariga.io/atlas/cmd/atlas/internal/cmdlog"
Expand All @@ -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.
Expand All @@ -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}})
Expand Down
4 changes: 2 additions & 2 deletions doc/md/lint/analyzers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <old_name>`, 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 <old_name>`, it will ignore this check.
```sql
ALTER TABLE `users` RENAME TO `Users`;
CREATE VIEW `users` AS SELECT * FROM `Users`;
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/testdata/mysql/cli-migrate-apply.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;"\]'
Expand Down

0 comments on commit 9153831

Please sign in to comment.