aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration.go
blob: efd29e3584ad74bd9c48eba7160ff3ed3c495c4b (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package repositories

import (
	"database/sql"
	"errors"
	"fmt"
	"os"

	"github.com/eugenetriguba/bolt/internal/config"
)

type IsNotDirError struct {
	path string
}

func (e *IsNotDirError) Error() string {
	return fmt.Sprintf(
		"The specified migrations directory path '%s' is not a directory.",
		e.path,
	)
}

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

// Create a new Migration Repo.
//
// This handles the interactions with applying
// and reverting migrations.
func NewMigrationRepo(db *sql.DB, c *config.Config) (*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
	}

	fileInfo, err := os.Stat(c.MigrationsDir)
	if errors.Is(err, os.ErrNotExist) {
		err = os.MkdirAll(c.MigrationsDir, 0755)
		if err != nil {
			return nil, err
		}
	} else if err != nil {
		return nil, err
	} else if err == nil && !fileInfo.IsDir() {
		return nil, &IsNotDirError{path: c.MigrationsDir}
	}

	return &MigrationRepo{db: db, config: c}, nil
}

func (mr *MigrationRepo) Create(message string) error {
	tx, err := mr.db.Begin()
	if err != nil {
		return err
	}
	defer tx.Rollback()

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

	return nil
}