aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/migrations.go
blob: 7de255af557dec2432aa51bf630f04825a2a1ef6 (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
package database

import "database/sql"

// allMigrations is the list of migrations to perform to get an up to date database
// Order is important. Add new migrations at the end of the list.
var allMigrations = []func(tx *sql.Tx) error{
	func(tx *sql.Tx) (err error) {
		sql := `
			CREATE TABLE schema_version (
				version INTEGER NOT NULL
			);
			CREATE TABLE users (
				id INTEGER PRIMARY KEY,
				username TEXT NOT NULL UNIQUE,
				hash TEXT,
				email TEXT,
				created_at DATE DEFAULT (datetime('now')),
				last_login_at DATE DEFAULT NULL
			);
			CREATE TABLE sessions (
				token TEXT NOT NULL UNIQUE,
				user_id INTEGER NOT NULL,
				created_at DATE DEFAULT (datetime('now')),
				FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
			);`
		_, err = tx.Exec(sql)
		return err
	},
}

// This variable exists so that tests can override it
var migrations = allMigrations