tidb.go

 1package dialectquery
 2
 3import "fmt"
 4
 5type Tidb struct{}
 6
 7var _ Querier = (*Tidb)(nil)
 8
 9func (t *Tidb) CreateTable(tableName string) string {
10	q := `CREATE TABLE %s (
11		id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
12		version_id bigint NOT NULL,
13		is_applied boolean NOT NULL,
14		tstamp timestamp NULL default now(),
15		PRIMARY KEY(id)
16	)`
17	return fmt.Sprintf(q, tableName)
18}
19
20func (t *Tidb) InsertVersion(tableName string) string {
21	q := `INSERT INTO %s (version_id, is_applied) VALUES (?, ?)`
22	return fmt.Sprintf(q, tableName)
23}
24
25func (t *Tidb) DeleteVersion(tableName string) string {
26	q := `DELETE FROM %s WHERE version_id=?`
27	return fmt.Sprintf(q, tableName)
28}
29
30func (t *Tidb) GetMigrationByVersion(tableName string) string {
31	q := `SELECT tstamp, is_applied FROM %s WHERE version_id=? ORDER BY tstamp DESC LIMIT 1`
32	return fmt.Sprintf(q, tableName)
33}
34
35func (t *Tidb) ListMigrations(tableName string) string {
36	q := `SELECT version_id, is_applied from %s ORDER BY id DESC`
37	return fmt.Sprintf(q, tableName)
38}
39
40func (t *Tidb) GetLatestVersion(tableName string) string {
41	q := `SELECT MAX(version_id) FROM %s`
42	return fmt.Sprintf(q, tableName)
43}