forked from ariga/atlas
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathatlas_migrate_example_test.go
More file actions
73 lines (66 loc) · 2.17 KB
/
atlas_migrate_example_test.go
File metadata and controls
73 lines (66 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package atlasexec_test
import (
"context"
"fmt"
"log"
"os"
"ariga.io/atlas/atlasexec"
)
func ExampleClient_MigrateApply() {
// Define the execution context, supplying a migration directory
// and potentially an `atlas.hcl` configuration file using `atlasexec.WithHCL`.
workdir, err := atlasexec.NewWorkingDir(
atlasexec.WithMigrations(
os.DirFS("./migrations"),
),
)
if err != nil {
log.Fatalf("failed to load working directory: %v", err)
}
// atlasexec works on a temporary directory, so we need to close it
defer workdir.Close()
// Initialize the client.
client, err := atlasexec.NewClient(workdir.Path(), "atlas")
if err != nil {
log.Fatalf("failed to initialize client: %v", err)
}
// Run `atlas migrate apply` on a SQLite database under /tmp.
res, err := client.MigrateApply(context.Background(), &atlasexec.MigrateApplyParams{
URL: "sqlite:///tmp/demo.db?_fk=1&cache=shared",
})
if err != nil {
log.Fatalf("failed to apply migrations: %v", err)
}
fmt.Printf("Applied %d migrations\n", len(res.Applied))
}
func ExampleClient_MigrateSet() {
// Define the execution context, supplying a migration directory
// and potentially an `atlas.hcl` configuration file using `atlasexec.WithHCL`.
workdir, err := atlasexec.NewWorkingDir(
atlasexec.WithMigrations(
os.DirFS("./migrations"),
),
)
if err != nil {
log.Fatalf("failed to load working directory: %v", err)
}
// atlasexec works on a temporary directory, so we need to close it
defer workdir.Close()
// Initialize the client.
client, err := atlasexec.NewClient(workdir.Path(), "atlas")
if err != nil {
log.Fatalf("failed to initialize client: %v", err)
}
// Run `atlas migrate set` to mark migrations as applied up to version "3".
err = client.MigrateSet(context.Background(), &atlasexec.MigrateSetParams{
URL: "sqlite:///tmp/demo.db?_fk=1&cache=shared",
Version: "3",
})
if err != nil {
log.Fatalf("failed to set migrations: %v", err)
}
fmt.Println("Migration version set successfully")
}