backup-notify/database/migrate.go

43 lines
716 B
Go
Raw Normal View History

2024-04-21 21:46:40 -04:00
package database
import (
"log"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type Migrator func(*sql.DB) (*sql.DB, error)
func MigrateBackups(dbConn *sql.DB) (*sql.DB, error) {
log.Println("migrating backups table")
_, err := dbConn.Exec(`CREATE TABLE IF NOT EXISTS backups (
hostname TEXT NOT NULL,
received_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`)
if err != nil {
return dbConn, err
}
return dbConn, nil
}
func Migrate(dbConn *sql.DB) (*sql.DB, error) {
log.Println("migrating database")
migrations := []Migrator{
MigrateBackups,
}
for _, migration := range migrations {
dbConn, err := migration(dbConn)
if err != nil {
return dbConn, err
}
}
return dbConn, nil
}