aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories/migration.go
blob: 562b2e5fd88381c3744e5d6bbb9256f56626d703 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package repositories

import (
	"database/sql"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"sort"
	"strings"

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

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(m *models.Migration) error {
	path := filepath.Join(mr.config.MigrationsDir, m.Dirname())
	err := os.Mkdir(path, 0755)
	if err != nil {
		return err
	}

	_, err = os.Create(filepath.Join(path, "upgrade.sql"))
	if err != nil {
		return err
	}

	return nil
}

func (mr *MigrationRepo) List() ([]*models.Migration, error) {
	rows, err := mr.db.Query(`SELECT version FROM bolt_migrations;`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	migrations := make(map[string]*models.Migration)
	for rows.Next() {
		var version string
		err := rows.Scan(&version)
		if err != nil {
			return nil, err
		}
		version = strings.TrimSpace(version)
		migrations[version] = &models.Migration{
			Version: version,
			Message: "",
			Applied: true,
		}
	}

	entries, err := os.ReadDir(mr.config.MigrationsDir)
	if err != nil {
		return nil, err
	}
	for _, entry := range entries {
		parts := strings.SplitN(entry.Name(), "_", 2)
		if len(parts) != 2 {
			return nil, errors.New(
				fmt.Sprintf(
					"%s is an invalid migration name. Expected a "+
						"migration directory of the format <version>_<message>.",
					entry.Name(),
				),
			)
		}
		version := parts[0]
		message := parts[1]
		val, ok := migrations[version]
		if ok {
			val.Message = message
		} else {
			migrations[version] = &models.Migration{
				Version: version,
				Message: message,
				Applied: false,
			}
		}
	}

	values := make([]*models.Migration, 0, len(migrations))
	for _, value := range migrations {
		values = append(values, value)
	}

	sort.Slice(values, func(i, j int) bool {
		return values[i].Dirname() < values[j].Dirname()
	})

	return values, nil
}

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

	upgradeScriptPath := filepath.Join(
		mr.config.MigrationsDir, migration.Dirname(), "upgrade.sql",
	)
	contents, err := os.ReadFile(upgradeScriptPath)
	if err != nil {
		return err
	}

	tx.Exec(string(contents))
	tx.Exec(
		`INSERT INTO bolt_migrations(version) VALUES ($1);`,
		migration.Version,
	)
	migration.Applied = true
	tx.Commit()

	return nil
}