47 lines
843 B
Go
47 lines
843 B
Go
package database
|
|
|
|
import (
|
|
"log"
|
|
|
|
"database/sql"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
type Migrator func(*sql.DB) (*sql.DB, error)
|
|
|
|
func AddWhoisUpdates(dbConn *sql.DB) (*sql.DB, error) {
|
|
_, err := dbConn.Exec(`CREATE TABLE IF NOT EXISTS updates (
|
|
name TEXT,
|
|
time TIMESTAMP
|
|
);`)
|
|
if err != nil {
|
|
return dbConn, err
|
|
}
|
|
|
|
log.Println("creating time index on updates table")
|
|
_, err = dbConn.Exec(`CREATE INDEX IF NOT EXISTS idx_update_timestamp ON updates (time);`)
|
|
if err != nil {
|
|
return dbConn, err
|
|
}
|
|
|
|
return dbConn, nil
|
|
}
|
|
|
|
func Migrate(dbConn *sql.DB) (*sql.DB, error) {
|
|
log.Println("migrating database")
|
|
|
|
migrations := []Migrator{
|
|
AddWhoisUpdates,
|
|
}
|
|
|
|
for _, migration := range migrations {
|
|
dbConn, err := migration(dbConn)
|
|
if err != nil {
|
|
return dbConn, err
|
|
}
|
|
}
|
|
|
|
return dbConn, nil
|
|
}
|