~egtann/migrate

ref: e5cd7ca5dd9f9bd3e57414765ec3f69419d0683e migrate/sqlite/sqlite.go -rw-r--r-- 6.7 KiB
e5cd7ca5 — Evan Tann fix mysql 5.7 bug 1 year, 2 months ago
                                                                                
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package sqlite

import (
	"database/sql"

	"github.com/egtann/migrate"
	"github.com/jmoiron/sqlx"
	"github.com/pkg/errors"

	_ "github.com/mattn/go-sqlite3"
)

type DB struct {
	filepath string

	// Embed the sqlx DB struct
	*sqlx.DB
}

func New(dbFile string) *DB {
	return &DB{filepath: dbFile}
}

func (db *DB) CreateMetaIfNotExists() error {
	q := `CREATE TABLE IF NOT EXISTS meta (
		filename TEXT UNIQUE NOT NULL,
		md5 TEXT NOT NULL,
		content TEXT NOT NULL,
		createdat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)`
	if _, err := db.Exec(q); err != nil {
		return errors.Wrap(err, "create meta table")
	}
	return nil
}

func (db *DB) CreateMetaCheckpointsIfNotExists() error {
	q := `CREATE TABLE IF NOT EXISTS metacheckpoints (
		filename TEXT NOT NULL,
		content TEXT NOT NULL,
		idx INTEGER NOT NULL,
		md5 TEXT NOT NULL,
		createdat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY (filename, idx)
	)`
	if _, err := db.Exec(q); err != nil {
		return errors.Wrap(err, "create metacheckpoints table")
	}
	return nil
}

func (db *DB) GetMigrations() ([]migrate.Migration, error) {
	migrations := []migrate.Migration{}
	q := `SELECT filename, content, md5 AS checksum FROM meta`
	err := db.Select(&migrations, q)
	return migrations, err

}

func (db *DB) GetMetaCheckpoints(filename string) ([]string, error) {
	checkpoints := []string{}
	q := `SELECT md5 FROM metacheckpoints WHERE filename=$1 ORDER BY idx`
	err := db.Select(&checkpoints, q, filename)
	return checkpoints, err
}

func (db *DB) UpsertMigration(filename, content, checksum string) error {
	q := `
		INSERT INTO meta (filename, content, md5) VALUES ($1, $2, $3)
		ON CONFLICT(filename) DO UPDATE SET md5=$4, content=$5`
	_, err := db.Exec(q, filename, content, checksum, checksum, content)
	return err
}

func (db *DB) InsertMetaCheckpoint(
	filename, content, checksum string,
	idx int,
) error {
	q := `
		INSERT INTO metacheckpoints (filename, content, idx, md5)
		VALUES ($1, $2, $3, $4)`
	_, err := db.Exec(q, filename, content, idx, checksum)
	return err
}

func (db *DB) InsertMigration(filename, content, checksum string) error {
	q := `INSERT INTO meta (filename, content, md5) VALUES ($1, $2, $3)`
	_, err := db.Exec(q, filename, content, checksum)
	return err
}

func (db *DB) DeleteMetaCheckpoints() error {
	q := `DELETE FROM metacheckpoints`
	_, err := db.Exec(q)
	return err
}

func (db *DB) CreateMetaVersionIfNotExists() (int, error) {
	q := `CREATE TABLE IF NOT EXISTS metaversion (
		version INTEGER NOT NULL
	)`
	if _, err := db.Exec(q); err != nil {
		return 0, errors.Wrap(err, "create metaversion table")
	}

	var version int
	q = `SELECT version FROM metaversion`
	err := db.Get(&version, q)
	switch {
	case err == sql.ErrNoRows:
		return 0, nil
	case err != nil:
		return 0, errors.Wrap(err, "get version")
	}
	return version, nil
}

func (db *DB) Open() error {
	var err error
	db.DB, err = sqlx.Open("sqlite3", db.filepath)
	if err != nil {
		return errors.Wrap(err, "open db connection")
	}
	return nil
}

// UpgradeToV1 migrates existing meta tables to the v1 format. Complete any
// migrations before running this function; this will not succeed if have any
// existing metacheckpoints.
func (db *DB) UpgradeToV1(migrations []migrate.Migration) (err error) {
	// Begin Tx
	tx, err := db.Beginx()
	if err != nil {
		return errors.Wrap(err, "begin tx")
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
			return
		}
		err = tx.Commit()
	}()

	// Remove the uniqueness constraint from md5. sqlite doesn't support
	// MODIFY COLUMN so we recreate the table.
	q := `CREATE TABLE metatmp (
		filename TEXT UNIQUE NOT NULL,
		md5 TEXT NOT NULL,
		createdat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "create metatmp")
		return
	}
	q = `INSERT INTO metatmp SELECT filename, md5, createdat FROM meta`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "insert metatmp")
	}
	q = `DROP TABLE meta`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "drop meta")
	}
	q = `ALTER TABLE metatmp RENAME TO meta`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "rename metatmp 1")
		return
	}

	// Add a content column to record the exact migration that ran
	// alongside the md5, insert the appropriate data, then set not null
	q = `ALTER TABLE meta ADD COLUMN content TEXT`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "add content column")
		return
	}
	for _, m := range migrations {
		q = `UPDATE meta SET content=$1 WHERE filename=$2`
		if _, err = tx.Exec(q, m.Content, m.Filename); err != nil {
			err = errors.Wrap(err, "update meta content")
			return
		}
	}

	// Once again, sqlite3 doesn't support modify column, so we have to
	// recreate our tables
	q = `CREATE TABLE metatmp (
		filename TEXT UNIQUE NOT NULL,
		content TEXT NOT NULL,
		md5 TEXT NOT NULL,
		createdat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "create metatmp")
		return
	}
	q = `
		INSERT INTO metatmp
		SELECT filename, content, md5, createdat FROM meta`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "")
	}
	q = `DROP TABLE meta`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "drop meta")
		return
	}
	q = `ALTER TABLE metatmp RENAME TO meta`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "rename metatmp 2")
		return
	}

	// Add the content column to metacheckpoints. Same song and dance as above
	q = `CREATE TABLE metacheckpointstmp (
		filename TEXT NOT NULL,
		content TEXT NOT NULL,
		idx INTEGER NOT NULL,
		md5 TEXT NOT NULL,
		createdat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY (filename, idx)
	)`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "create metacheckpointstmp")
		return
	}
	q = `
		INSERT INTO metacheckpointstmp
		SELECT filename, md5, createdat FROM meta`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "insert metacheckpointstmp")
	}
	q = `DROP TABLE metacheckpoints`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "drop metacheckpoints")
		return
	}
	q = `ALTER TABLE metacheckpointstmp RENAME TO metacheckpoints`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "rename metacheckpointstmp")
		return
	}

	q = `CREATE TABLE IF NOT EXISTS metaversion (version INTEGER NOT NULL)`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "create metaversion table")
		return
	}
	q = `DELETE FROM metaversion`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "delete metaversion")
		return
	}
	q = `INSERT INTO metaversion (version) VALUES (1)`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "update metaversion")
		return
	}
	return nil
}