Skip to content

Commit 405d0ff

Browse files
committed
Bare minimum db schema
1 parent 82956c0 commit 405d0ff

File tree

4 files changed

+38
-59
lines changed

4 files changed

+38
-59
lines changed

db.go

+15-19
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package gomigrate
33
type Migratable interface {
44
SelectMigrationTableSql() string
55
CreateMigrationTableSql() string
6-
MigrationStatusSql() string
6+
GetMigrationSql() string
77
MigrationLogInsertSql() string
8-
MigrationLogUpdateSql() string
8+
MigrationLogDeleteSql() string
99
}
1010

11-
// POSTGRESQL
11+
// POSTGRES
1212

1313
type Postgres struct{}
1414

@@ -19,22 +19,20 @@ func (p Postgres) SelectMigrationTableSql() string {
1919
func (p Postgres) CreateMigrationTableSql() string {
2020
return `CREATE TABLE gomigrate (
2121
id SERIAL PRIMARY KEY,
22-
migration_id INT UNIQUE NOT NULL,
23-
name VARCHAR(100) UNIQUE NOT NULL,
24-
status INT NOT NULL
22+
migration_id BIGINT UNIQUE NOT NULL
2523
)`
2624
}
2725

28-
func (p Postgres) MigrationStatusSql() string {
29-
return "SELECT status FROM gomigrate WHERE name = $1"
26+
func (p Postgres) GetMigrationSql() string {
27+
return `SELECT migration_id FROM gomigrate WHERE migration_id = $1`
3028
}
3129

3230
func (p Postgres) MigrationLogInsertSql() string {
33-
return "INSERT INTO gomigrate (migration_id, name, status) values ($1, $2, $3)"
31+
return "INSERT INTO gomigrate (migration_id) values ($1)"
3432
}
3533

36-
func (p Postgres) MigrationLogUpdateSql() string {
37-
return "UPDATE gomigrate SET status = $1 WHERE migration_id = $2"
34+
func (p Postgres) MigrationLogDeleteSql() string {
35+
return "DELETE FROM gomigrate WHERE migration_id = $1"
3836
}
3937

4038
// MYSQL
@@ -48,21 +46,19 @@ func (m Mysql) SelectMigrationTableSql() string {
4846
func (m Mysql) CreateMigrationTableSql() string {
4947
return `CREATE TABLE gomigrate (
5048
id INT NOT NULL AUTO_INCREMENT,
51-
migration_id INT NOT NULL UNIQUE,
52-
name VARCHAR(100) NOT NULL UNIQUE,
53-
status INT NOT NULL,
49+
migration_id BIGINT NOT NULL UNIQUE,
5450
PRIMARY KEY (id)
5551
) ENGINE=MyISAM`
5652
}
5753

58-
func (m Mysql) MigrationStatusSql() string {
59-
return "SELECT status FROM gomigrate WHERE name = ?"
54+
func (m Mysql) GetMigrationSql() string {
55+
return `SELECT migration_id FROM gomigrate WHERE migration_id = ?`
6056
}
6157

6258
func (m Mysql) MigrationLogInsertSql() string {
63-
return "INSERT INTO gomigrate (migration_id, name, status) values (?, ?, ?)"
59+
return "INSERT INTO gomigrate (migration_id) values (?)"
6460
}
6561

66-
func (m Mysql) MigrationLogUpdateSql() string {
67-
return "UPDATE gomigrate SET status = ? WHERE migration_id = ?"
62+
func (m Mysql) MigrationLogDeleteSql() string {
63+
return "DELETE FROM gomigrate WHERE migration_id = ?"
6864
}

gomigrate.go

+8-19
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ func (m *Migrator) fetchMigrations() error {
155155
// migration.
156156
func (m *Migrator) getMigrationStatuses() error {
157157
for _, migration := range m.migrations {
158-
rows, err := m.DB.Query(m.dbAdapter.MigrationStatusSql(), migration.Name)
158+
row := m.DB.QueryRow(m.dbAdapter.GetMigrationSql(), migration.Id)
159+
var mid uint64
160+
err := row.Scan(&mid)
161+
if err == sql.ErrNoRows {
162+
continue
163+
}
159164
if err != nil {
160165
log.Printf(
161166
"Error getting migration status for %s: %v",
@@ -164,20 +169,7 @@ func (m *Migrator) getMigrationStatuses() error {
164169
)
165170
return err
166171
}
167-
for rows.Next() {
168-
var status int
169-
err := rows.Scan(&status)
170-
if err != nil {
171-
log.Printf("Error getting migration status: %v", err)
172-
return err
173-
}
174-
migration.Status = status
175-
log.Printf(
176-
"Migration %s found with status: %v",
177-
migration.Name,
178-
status,
179-
)
180-
}
172+
migration.Status = Active
181173
}
182174
return nil
183175
}
@@ -230,8 +222,6 @@ func (m *Migrator) Migrate() error {
230222
_, err = transaction.Exec(
231223
m.dbAdapter.MigrationLogInsertSql(),
232224
migration.Id,
233-
migration.Name,
234-
Active,
235225
)
236226
if err != nil {
237227
log.Printf("Error logging migration: %v", err)
@@ -288,8 +278,7 @@ func (m *Migrator) Rollback() error {
288278

289279
// Change the status in the migrations table.
290280
_, err = transaction.Exec(
291-
m.dbAdapter.MigrationLogUpdateSql(),
292-
Inactive,
281+
m.dbAdapter.MigrationLogDeleteSql(),
293282
lastMigration.Id,
294283
)
295284
if err != nil {

gomigrate_test.go

+14-18
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,22 @@ func TestCreatingMigratorWhenTableExists(t *testing.T) {
5858
if err != nil {
5959
t.Error(err)
6060
}
61-
_, err = db.Exec(adapter.MigrationLogInsertSql(), 123, "my_test", Active)
61+
_, err = db.Exec(adapter.MigrationLogInsertSql(), 123)
6262
if err != nil {
6363
t.Error(err)
6464
}
6565

6666
GetMigrator("test1")
6767

6868
// Check that our row is still present.
69-
row := db.QueryRow("select name, status from gomigrate")
70-
var name string
71-
var status int
72-
err = row.Scan(&name, &status)
69+
row := db.QueryRow("select migration_id from gomigrate")
70+
var id uint64
71+
err = row.Scan(&id)
7372
if err != nil {
7473
t.Error(err)
7574
}
76-
if name != "my_test" {
77-
t.Error("Invalid name found in database")
78-
}
79-
if status != Active {
80-
t.Error("Invalid status found in database")
75+
if id != 123 {
76+
t.Error("Invalid id found in database")
8177
}
8278
cleanup()
8379
}
@@ -103,8 +99,8 @@ func TestMigrationAndRollback(t *testing.T) {
10399
}
104100
// Ensure that the migrate status is correct.
105101
row = db.QueryRow(
106-
adapter.MigrationStatusSql(),
107-
"test",
102+
adapter.GetMigrationSql(),
103+
1,
108104
)
109105
var status int
110106
if err := row.Scan(&status); err != nil {
@@ -128,16 +124,16 @@ func TestMigrationAndRollback(t *testing.T) {
128124
t.Errorf("Migration table should be deleted")
129125
}
130126

131-
// Ensure that the migrate status is correct.
127+
// Ensure that the migration log is missing.
132128
row = db.QueryRow(
133-
adapter.MigrationStatusSql(),
134-
"test",
129+
adapter.GetMigrationSql(),
130+
1,
135131
)
136-
if err := row.Scan(&status); err != nil {
132+
if err := row.Scan(&status); err != sql.ErrNoRows {
137133
t.Error(err)
138134
}
139-
if status != Inactive || m.migrations[1].Status != Inactive {
140-
t.Errorf("Invalid status for migration, %v", status)
135+
if m.migrations[1].Status != Inactive {
136+
t.Errorf("Invalid status for migration, %v", m.migrations[1].Status)
141137
}
142138

143139
cleanup()

migration.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ const (
1111
// Holds configuration information for a given migration.
1212
type Migration struct {
1313
DownPath string
14+
Id uint64
1415
Name string
1516
Status int
1617
UpPath string
17-
18-
// The file system identifier, not the database id.
19-
Id uint64
2018
}
2119

2220
// Performs a basic validation of a migration.

0 commit comments

Comments
 (0)