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
|
package migrations
import (
"context"
"database/sql"
"github.com/pressly/goose/v3"
)
func init() {
goose.AddMigrationContext(upDevice, downDevice)
}
func upDevice(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`CREATE TABLE IF NOT EXISTS devices (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
status TINYINT(1) DEFAULT 0,
is_logging TINYINT(1) DEFAULT 0,
user_id INTEGER,
cpu_id INTEGER,
ram_id INTEGER,
disk_id INTEGER,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (cpu_id) REFERENCES cpus(id),
FOREIGN KEY (ram_id) REFERENCES rams(id),
FOREIGN KEY (disk_id) REFERENCES disks(id)
)`)
if err != nil {
return err
}
return nil
}
func downDevice(ctx context.Context, tx *sql.Tx) error {
// This code is executed when the migration is rolled back.
_, err := tx.Exec(`DROP TABLE IF EXISTS devices`)
if err != nil {
return err
}
return nil
}
|