cms-template/storage/sqlite/sqlite.go

76 lines
1.5 KiB
Go
Raw Normal View History

// go:build sqlite
package sqlite
import (
"database/sql"
"embed"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source/iofs"
)
type SqliteStorage struct {
db *sql.DB
fs embed.FS
}
func NewStorageSqlite(fs embed.FS) func(*sql.DB) (interface{}, error) {
return func(db *sql.DB) (interface{}, error) {
return &SqliteStorage{db: db, fs: fs}, nil
}
}
func (s *SqliteStorage) MigrateUp() error {
driver, err := sqlite.WithInstance(s.db, &sqlite.Config{
MigrationsTable: sqlite.DefaultMigrationsTable,
DatabaseName: "template",
})
if err != nil {
return err
}
source, err := iofs.New(s.fs, "migrations/sqlite")
if err != nil {
return err
}
// TODO: pass own logger
m, err := migrate.NewWithInstance("fs", source, "template", driver)
if err != nil {
return err
}
if err = m.Up(); err != nil && err != migrate.ErrNoChange {
return err
}
return nil
}
func (s *SqliteStorage) MigrateDown() error {
driver, err := sqlite.WithInstance(s.db, &sqlite.Config{
MigrationsTable: sqlite.DefaultMigrationsTable,
DatabaseName: "template",
})
if err != nil {
return err
}
source, err := iofs.New(s.fs, "migrations/sqlite")
if err != nil {
return err
}
// TODO: pass own logger
m, err := migrate.NewWithInstance("fs", source, "template", driver)
if err != nil {
return err
}
if err = m.Down(); err != nil && err != migrate.ErrNoChange {
return err
}
return nil
}