switched to fully self contained docker
This commit is contained in:
+24
-22
@@ -12,44 +12,35 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dsn := os.Getenv("TAPM_DATABASE_DSN")
|
||||
if dsn == "" {
|
||||
log.Fatal("TAPM_DATABASE_DSN is required")
|
||||
dsn = "file:/data/tapm.db?_pragma=busy_timeout(5000)&_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)"
|
||||
}
|
||||
directory := os.Getenv("TAPM_MIGRATIONS_DIR")
|
||||
if directory == "" {
|
||||
directory = "/app/migrations"
|
||||
}
|
||||
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetMaxOpenConns(1)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
log.Fatalf("database: %v", err)
|
||||
}
|
||||
|
||||
var locked int
|
||||
if err := db.QueryRowContext(ctx, `SELECT GET_LOCK('tapm_schema_migrations', 30)`).Scan(&locked); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if locked != 1 {
|
||||
log.Fatal("could not acquire migration lock")
|
||||
}
|
||||
defer db.ExecContext(context.Background(), `SELECT RELEASE_LOCK('tapm_schema_migrations')`)
|
||||
|
||||
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version BIGINT UNSIGNED NOT NULL PRIMARY KEY,
|
||||
applied_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
|
||||
) ENGINE=InnoDB`); err != nil {
|
||||
version INTEGER NOT NULL PRIMARY KEY,
|
||||
applied_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)`); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -71,24 +62,35 @@ func main() {
|
||||
log.Fatalf("migration %q: %v", entry.Name(), err)
|
||||
}
|
||||
var applied int
|
||||
if err := db.QueryRowContext(
|
||||
ctx,
|
||||
`SELECT COUNT(*) FROM schema_migrations WHERE version = ?`,
|
||||
version,
|
||||
if err := db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM schema_migrations WHERE version = ?`, version,
|
||||
).Scan(&applied); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if applied != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
body, err := os.ReadFile(filepath.Join(directory, entry.Name()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, string(body)); err != nil {
|
||||
tx, err := db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, string(body)); err != nil {
|
||||
_ = tx.Rollback()
|
||||
log.Fatalf("apply %s: %v", entry.Name(), err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO schema_migrations (version) VALUES (?)`, version,
|
||||
); err != nil {
|
||||
_ = tx.Rollback()
|
||||
log.Fatalf("record %s: %v", entry.Name(), err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Fatalf("commit %s: %v", entry.Name(), err)
|
||||
}
|
||||
fmt.Printf("applied migration %s\n", entry.Name())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user