Skip to content

Commit 9ed1ac8

Browse files
Merge branch 'master' into LostImagin4tion/sync-develop
2 parents bf7848c + c7a906b commit 9ed1ac8

File tree

13 files changed

+481
-112
lines changed

13 files changed

+481
-112
lines changed

atlasexec/atlas_migrate.go

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ type (
165165
URL string
166166
RevisionsSchema string
167167
}
168+
// MigrateLsParams are the parameters for the `migrate ls` command.
169+
MigrateLsParams struct {
170+
ConfigURL string
171+
Env string
172+
Vars VarArgs
173+
174+
DirURL string
175+
Short bool // -s: print only migration version (omit description and .sql suffix)
176+
Latest bool // -l: print only the latest migration file
177+
}
178+
// MigrateSetParams are the parameters for the `migrate set` command.
179+
MigrateSetParams struct {
180+
ConfigURL string
181+
Env string
182+
Vars VarArgs
183+
184+
DirURL string
185+
URL string
186+
RevisionsSchema string
187+
Version string
188+
}
168189
// MigrateDiffParams are the parameters for the `migrate diff` command.
169190
MigrateDiffParams struct {
170191
ConfigURL string
@@ -419,6 +440,58 @@ func (c *Client) MigrateStatus(ctx context.Context, params *MigrateStatusParams)
419440
return firstResult(jsonDecode[MigrateStatus](c.runCommand(ctx, args)))
420441
}
421442

443+
// MigrateLs runs the 'migrate ls' command and returns the listed migration file names (or versions when Short is true), one per line.
444+
func (c *Client) MigrateLs(ctx context.Context, params *MigrateLsParams) (string, error) {
445+
args := []string{"migrate", "ls"}
446+
if params.ConfigURL != "" {
447+
args = append(args, "--config", params.ConfigURL)
448+
}
449+
if params.Env != "" {
450+
args = append(args, "--env", params.Env)
451+
}
452+
if params.DirURL != "" {
453+
args = append(args, "--dir", params.DirURL)
454+
}
455+
if params.Vars != nil {
456+
args = append(args, params.Vars.AsArgs()...)
457+
}
458+
if params.Short {
459+
args = append(args, "--short")
460+
}
461+
if params.Latest {
462+
args = append(args, "--latest")
463+
}
464+
return stringVal(c.runCommand(ctx, args))
465+
}
466+
467+
// MigrateSet runs the 'migrate set' command.
468+
func (c *Client) MigrateSet(ctx context.Context, params *MigrateSetParams) error {
469+
args := []string{"migrate", "set"}
470+
if params.Env != "" {
471+
args = append(args, "--env", params.Env)
472+
}
473+
if params.ConfigURL != "" {
474+
args = append(args, "--config", params.ConfigURL)
475+
}
476+
if params.URL != "" {
477+
args = append(args, "--url", params.URL)
478+
}
479+
if params.DirURL != "" {
480+
args = append(args, "--dir", params.DirURL)
481+
}
482+
if params.RevisionsSchema != "" {
483+
args = append(args, "--revisions-schema", params.RevisionsSchema)
484+
}
485+
if params.Vars != nil {
486+
args = append(args, params.Vars.AsArgs()...)
487+
}
488+
if params.Version != "" {
489+
args = append(args, params.Version)
490+
}
491+
_, err := c.runCommand(ctx, args)
492+
return err
493+
}
494+
422495
// MigrateDiff runs the 'migrate diff --dry-run' command and returns the generated migration files without changing the filesystem.
423496
// Requires atlas CLI to be logged in to the cloud.
424497
func (c *Client) MigrateDiff(ctx context.Context, params *MigrateDiffParams) (*MigrateDiff, error) {
@@ -526,7 +599,7 @@ func (c *Client) MigrateRebase(ctx context.Context, params *MigrateRebaseParams)
526599
if params.DirURL != "" {
527600
args = append(args, "--dir", params.DirURL)
528601
}
529-
args = append(args, strings.Join(params.Files, " "))
602+
args = append(args, params.Files...)
530603
_, err := c.runCommand(ctx, args)
531604
return err
532605
}
@@ -734,10 +807,16 @@ func newMigrateApplyError(r []*MigrateApply, stderr string) error {
734807

735808
// Error implements the error interface.
736809
func (e *MigrateApplyError) Error() string {
810+
var errs []string
811+
for _, r := range e.Result {
812+
if r.Error != "" {
813+
errs = append(errs, r.Error)
814+
}
815+
}
737816
if e.Stderr != "" {
738-
return e.Stderr
817+
errs = append(errs, e.Stderr)
739818
}
740-
return last(e.Result).Error
819+
return strings.Join(errs, "\n")
741820
}
742821

743822
func plural(n int) (s string) {

atlasexec/atlas_migrate_example_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,33 @@ func ExampleClient_MigrateApply() {
4141
}
4242
fmt.Printf("Applied %d migrations\n", len(res.Applied))
4343
}
44+
45+
func ExampleClient_MigrateSet() {
46+
// Define the execution context, supplying a migration directory
47+
// and potentially an `atlas.hcl` configuration file using `atlasexec.WithHCL`.
48+
workdir, err := atlasexec.NewWorkingDir(
49+
atlasexec.WithMigrations(
50+
os.DirFS("./migrations"),
51+
),
52+
)
53+
if err != nil {
54+
log.Fatalf("failed to load working directory: %v", err)
55+
}
56+
// atlasexec works on a temporary directory, so we need to close it
57+
defer workdir.Close()
58+
59+
// Initialize the client.
60+
client, err := atlasexec.NewClient(workdir.Path(), "atlas")
61+
if err != nil {
62+
log.Fatalf("failed to initialize client: %v", err)
63+
}
64+
// Run `atlas migrate set` to mark migrations as applied up to version "3".
65+
err = client.MigrateSet(context.Background(), &atlasexec.MigrateSetParams{
66+
URL: "sqlite:///tmp/demo.db?_fk=1&cache=shared",
67+
Version: "3",
68+
})
69+
if err != nil {
70+
log.Fatalf("failed to set migrations: %v", err)
71+
}
72+
fmt.Println("Migration version set successfully")
73+
}

0 commit comments

Comments
 (0)