mast/db/migrations.go

34 lines
708 B
Go
Raw Normal View History

package mast
2024-08-03 18:06:43 -07:00
import (
"database/sql"
2024-09-18 18:47:29 -07:00
"embed"
"log"
2024-08-03 18:06:43 -07:00
)
2024-09-18 18:47:29 -07:00
//go:embed migrations/0001.sql
var migration0001 embed.FS
2024-08-03 18:06:43 -07:00
func RunMigrations(db *sql.DB) error {
// Load the cr-sqlite extension
// _, err := db.Exec("SELECT load_extension('./crsqlite.so', 'sqlite3_crsqlite_init')")
// if err != nil {
// return fmt.Errorf("failed to load cr-sqlite extension: %w", err)
// }
2024-08-03 18:06:43 -07:00
2024-09-18 18:47:29 -07:00
// Read the embedded SQL file
sqlData, err := migration0001.ReadFile("migrations/0001.sql")
if err != nil {
log.Fatalf("Failed to read embedded SQL file: %v", err)
}
sqlQuery := string(sqlData)
// Create the todos table and initialize it as a CRDT
2024-09-18 18:47:29 -07:00
_, err = db.Exec(sqlQuery)
if err != nil {
return err
}
2024-08-03 18:06:43 -07:00
return nil
2024-08-03 18:06:43 -07:00
}