~egtann/migrate

ref: f8bba57067c303761315e2916cc06bc61880b2cd migrate/mysql/mysql.go -rw-r--r-- 6.5 KiB
f8bba570 — Evan Tann update module path 1 year, 1 month 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
260
261
262
263
264
package mysql

import (
	"crypto/tls"
	"crypto/x509"
	"database/sql"
	"fmt"
	"io/ioutil"
	"strings"

	"egt.run/pkg/migrate"
	"github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	"github.com/pkg/errors"
)

type DB struct {
	connURL   string
	tlsConfig *tlsConfig

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

func New(
	user, pass, host, dbName string,
	port int,
	sslKey, sslCert, sslCA, sslServerName string,
) (*DB, error) {
	db := &DB{}
	db.connURL = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", user,
		pass, host, port, dbName)
	if sslKey != "" {
		db.connURL = fmt.Sprintf("%s&tls=%s", db.connURL, sslServerName)
		var err error
		db.tlsConfig, err = newTLSConfig(dbName, sslKey,
			sslCert, sslCA, sslServerName)
		if err != nil {
			return nil, errors.Wrap(err, "new tls config")
		}
	}
	return db, nil
}

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) CreateMetaIfNotExists() error {
	q := `CREATE TABLE IF NOT EXISTS meta (
		filename VARCHAR(255) UNIQUE NOT NULL,
		md5 VARCHAR(255) NOT NULL,
		content TEXT NOT NULL,
		createdat DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
	)`
	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 VARCHAR(255) NOT NULL,
		idx INTEGER NOT NULL,
		md5 VARCHAR(255) NOT NULL,
		content TEXT NOT NULL,
		createdat DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
		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
	ORDER BY filename * 1`
	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=? 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 (?, ?, ?)
		ON DUPLICATE KEY UPDATE md5=?, content=?`
	_, 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 (?, ?, ?, ?)`
	_, 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 (?, ?, ?)`
	_, err := db.Exec(q, filename, content, checksum)
	return err
}

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

// 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
	q := `ALTER TABLE meta DROP INDEX md5`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "remove md5 unique")
		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=? WHERE filename=?`
		if _, err = tx.Exec(q, m.Content, m.Filename); err != nil {
			err = errors.Wrap(err, "update meta content")
			return
		}
	}
	q = `ALTER TABLE meta MODIFY COLUMN content TEXT NOT NULL`
	if _, err = tx.Exec(q); err != nil {
		err = errors.Wrap(err, "update meta content not null")
		return
	}

	// Add the content column to metacheckpoints
	q = `
	ALTER TABLE metacheckpoints
	ADD COLUMN content TEXT NOT NULL`
	_, err = tx.Exec(q)
	if err != nil {
		// Ignore duplicate column errors
		if !strings.Contains(err.Error(), "Duplicate column name") {
			err = errors.Wrap(err, "add metacheckpoints content")
			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, "insert metaversion")
		return
	}
	return nil
}

func (db *DB) Open() error {
	if db.tlsConfig != nil {
		err := mysql.RegisterTLSConfig(db.tlsConfig.ServerName,
			db.tlsConfig.Config)
		if err != nil {
			return errors.Wrap(err, "register tls config")
		}
	}
	var err error
	db.DB, err = sqlx.Open("mysql", db.connURL)
	if err != nil {
		return errors.Wrap(err, "open db connection")
	}
	return nil
}

type tlsConfig struct {
	ServerName string
	Config     *tls.Config
}

func newTLSConfig(
	dbName, keyPath, certPath, caPath, serverName string,
) (*tlsConfig, error) {
	rootCertPool := x509.NewCertPool()
	pem, err := ioutil.ReadFile(caPath)
	if err != nil {
		return nil, errors.Wrap(err, "read sql server cert file")
	}
	if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
		return nil, errors.New("failed to append to pem")
	}
	certs, err := tls.LoadX509KeyPair(certPath, keyPath)
	if err != nil {
		return nil, errors.Wrap(err, "load x509 key pair")
	}
	clientCert := []tls.Certificate{certs}
	conf := &tlsConfig{
		ServerName: serverName,
		Config: &tls.Config{
			RootCAs:      rootCertPool,
			Certificates: clientCert,
			ServerName:   serverName,
		},
	}
	return conf, nil
}