Skip to content

Commit 243aa65

Browse files
committed
Fix for missed migration
1 parent 89e98a1 commit 243aa65

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package datastore
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
7+
"github.com/pressly/goose"
8+
)
9+
10+
func init() {
11+
goose.AddMigration(Up20240818042100, nil)
12+
}
13+
14+
func CheckIfMigrationExists(db *sql.Tx, migration string, args ...interface{}) (bool, error) {
15+
query := "SELECT 1 FROM goose_db_version WHERE version_id = $1 LIMIT 1"
16+
var exists bool
17+
err := db.QueryRow(query, migration).Scan(&exists)
18+
if err != nil && err != sql.ErrNoRows {
19+
return false, err
20+
}
21+
return err != sql.ErrNoRows, nil
22+
}
23+
24+
func Up20240818042100(txn *sql.Tx) error {
25+
// When upgrading from 4.4.x, the migration 20201201163100 was not applied because 20210201110000 was already present.
26+
exists, err := CheckIfMigrationExists(txn, "20201201163100")
27+
if err != nil {
28+
return err
29+
}
30+
31+
if !exists {
32+
log.Printf("Migration 20201201163100 has not been applied. Adding missing entries.")
33+
createCertColumn := "ALTER TABLE cnsis ADD ca_cert TEXT"
34+
_, err := txn.Exec(createCertColumn)
35+
if err != nil {
36+
return err
37+
}
38+
39+
createEnableColumn := "ALTER TABLE tokens ADD enabled BOOLEAN NOT NULL DEFAULT TRUE"
40+
_, err = txn.Exec(createEnableColumn)
41+
if err != nil {
42+
return err
43+
}
44+
} else {
45+
// Migration has already been applied
46+
log.Printf("Migration 20201201163100 has been applied.")
47+
return nil
48+
}
49+
return nil
50+
}

0 commit comments

Comments
 (0)