43 lines
716 B
Go
43 lines
716 B
Go
|
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
|
||
|
}
|