Skip to content

Commit

Permalink
feat(db): add migrations logic and scripts for changes to services
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Feb 1, 2025
1 parent be1d8c3 commit 544e59b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
76 changes: 62 additions & 14 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

_ "github.com/tursodatabase/libsql-client-go/libsql"
Expand Down Expand Up @@ -48,9 +51,9 @@ func Init() error {
return fmt.Errorf("error creating tables: %v", err)
}

err = updateTables()
err = applyMigrations()
if err != nil {
return fmt.Errorf("error updating tables: %v", err)
return fmt.Errorf("error applying migrations: %v", err)
}

return nil
Expand Down Expand Up @@ -105,27 +108,72 @@ func createTables() error {
sent TEXT
);
`)

if err != nil {
return fmt.Errorf("error creating email_queue table: %v", err)
}

_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
applied TEXT
);
`)
if err != nil {
return fmt.Errorf("error creating migrations table: %v", err)
}

return nil
}

func updateTables() error {
_, err := db.Exec(`
ALTER TABLE dogs ADD COLUMN grouping INTEGER;
`)
if err != nil && !strings.Contains(err.Error(), "duplicate column name") {
return fmt.Errorf("error updating dogs table: %v", err)
func applyMigrations() error {
files, err := filepath.Glob(filepath.Join("migrations", "*.sql"))
if err != nil {
return fmt.Errorf("error reading migrations directory: %v", err)
}

_, err = db.Exec(`
UPDATE dogs SET grouping = 0 WHERE grouping IS NULL
`)
if err != nil {
return fmt.Errorf("error updating dogs table: %v", err)
sort.Strings(files)

for _, file := range files {
var count int
err := db.QueryRow("SELECT COUNT(*) FROM migrations WHERE name = ?",
filepath.Base(file)).Scan(&count)
if err != nil {
return fmt.Errorf("error checking migration status: %v", err)
}

if count > 0 {
continue
}

content, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("error reading migration file %s: %v", file, err)
}

tx, err := db.Begin()
if err != nil {
return fmt.Errorf("error beginning transaction: %v", err)
}

_, err = tx.Exec(string(content))
if err != nil {
tx.Rollback()
return fmt.Errorf("error executing migration %s: %v", file, err)
}

_, err = tx.Exec(
"INSERT INTO migrations (name, applied) VALUES (?, datetime('now'))",
filepath.Base(file))
if err != nil {
tx.Rollback()
return fmt.Errorf("error recording migration %s: %v", file, err)
}

err = tx.Commit()
if err != nil {
return fmt.Errorf("error committing migration %s: %v", file, err)
}
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions migrations/001_copy_services.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Insert into services and capture the mapping
INSERT INTO dog_services (dog_id, service, quantity, price)
SELECT id, service, quantity, price
FROM dogs;
3 changes: 3 additions & 0 deletions migrations/002_remove_dog_cols.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE dogs DROP COLUMN service;
ALTER TABLE dogs DROP COLUMN quantity;
ALTER TABLE dogs DROP COLUMN price;

0 comments on commit 544e59b

Please sign in to comment.