aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration.go
blob: 488d838bde07172a8e8ccf0eb141a4f5b4d1c94f (plain) (blame)
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
package repositories

import (
	"database/sql"
)

type MigrationRepo struct {
	// The database connection that will be used
	// to look up migrations that have already been
	// applied.
	db *sql.DB
}

// Create a new Migration Repo.
//
// This handles the interactions with applying
// and reverting migrations.
func NewMigrationRepo(db *sql.DB) (*MigrationRepo, error) {
	_, err := db.Exec(`
		CREATE TABLE IF NOT EXISTS bolt_migrations(
			version CHARACTER(32) PRIMARY KEY NOT NULL
		);
	`)
	if err != nil {
		return nil, err
	}

	return &MigrationRepo{db: db}, nil
}

func (m *MigrationRepo) Apply() error {
	tx, err := m.db.Begin()
	if err != nil {
		return err
	}
	defer tx.Rollback()

	sqlStatement := `INSERT INTO applied_migration(version) VALUES ($1);`
	_, err = m.db.Exec(sqlStatement)
	if err != nil {
		return err
	}

	return nil
}