-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
58 lines (53 loc) · 1.47 KB
/
example_test.go
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
package migrate_test
import (
"context"
"database/sql"
"log"
"github.com/noonat/migrate"
)
// This example shows how you could define a set of simple SQL migrations
// using ExecQueries and then apply them to the database using Up. Note that
// you need to create an adapter for your database, so that migrate knows how
// to do things like specify placeholders. If your migration requires more
// advanced logic, you can also specify a custom MigrationFunc instead of a
// SQL string.
func ExampleUp() {
adapter := migrate.NewPostgreSQLAdapter(log.Printf)
db, err := sql.Open("migrate_test", "")
if err != nil {
log.Panic(err)
}
migrations := []migrate.Migration{
{
Comment: "Add user and app tables",
Up: migrate.ExecQueries([]string{
`CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL)`,
`CREATE TABLE apps (
id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL)`,
}),
Down: migrate.ExecQueries([]string{
`DROP TABLE apps`,
`DROP TABLE users`,
}),
},
{
Comment: "Add user app join table",
Up: migrate.ExecQueries([]string{
`CREATE TABLE user_apps (
user_id INT NOT NULL REFERENCES users(id),
app_id INT NOT NULL REFERENCES apps(id),
UNIQUE (user_id, app_id))`,
}),
Down: migrate.ExecQueries([]string{
`DROP TABLE user_apps`,
}),
},
}
err = migrate.Up(context.Background(), db, adapter, migrations)
if err != nil {
log.Panicf("error running migrations: %s", err)
}
}