aboutsummaryrefslogtreecommitdiff
path: root/db/atlasdb/001_init_db.go
blob: 8550fc0b90862cf0bc3159657ab18bda7d63b1ae (plain)
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
package atlasdb

import (
	"context"
	"fmt"
	"strings"

	"github.com/jmoiron/sqlx"
)

func init() {
	migrate(up001, down001)
}

func up001(ctx context.Context, tx *sqlx.Tx) error {
	if _, err := tx.ExecContext(ctx, strings.ReplaceAll(`
		CREATE TABLE accounts (
			uid         TEXT PRIMARY KEY NOT NULL,
			username    TEXT NOT NULL DEFAULT '' COLLATE NOCASE,
			auth_ip     TEXT,
			auth_token  TEXT,
			auth_expiry INTEGER,
			last_server TEXT
		) STRICT;
	`, `
		`, "\n")); err != nil {
		return fmt.Errorf("create accounts table: %w", err)
	}
	if _, err := tx.ExecContext(ctx, `CREATE INDEX accounts_username_idx ON accounts(username, uid)`); err != nil {
		return fmt.Errorf("create accounts index: %w", err)
	}
	return nil
}

func down001(ctx context.Context, tx *sqlx.Tx) error {
	if _, err := tx.ExecContext(ctx, `DROP INDEX accounts_username_idx`); err != nil {
		return fmt.Errorf("drop accounts_username_idx index: %w", err)
	}
	if _, err := tx.ExecContext(ctx, `DROP TABLE accounts`); err != nil {
		return fmt.Errorf("drop accounts table: %w", err)
	}
	return nil
}