cms-template/storage/sqlite/sqlite.go
Vasiliy Tolstov 973f9ac822
Some checks failed
build / test (push) Failing after 5s
build / lint (push) Failing after 6s
initial import
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-26 19:45:39 +03:00

76 lines
1.5 KiB
Go

// 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
}