2022-02-03 10:32:03 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"embed"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2022-03-28 18:38:12 +03:00
|
|
|
//go:embed migration
|
|
|
|
var migrationFS embed.FS
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
//go:embed seed
|
|
|
|
var seedFS embed.FS
|
|
|
|
|
|
|
|
type DB struct {
|
|
|
|
Db *sql.DB
|
|
|
|
|
2022-02-06 05:37:09 +03:00
|
|
|
// datasource name
|
2022-02-03 10:32:03 +03:00
|
|
|
DSN string
|
2022-02-06 05:37:09 +03:00
|
|
|
// mode: release or dev
|
|
|
|
mode string
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDB returns a new instance of DB associated with the given datasource name.
|
2022-02-06 05:37:09 +03:00
|
|
|
func NewDB(dsn string, mode string) *DB {
|
2022-02-03 10:32:03 +03:00
|
|
|
db := &DB{
|
2022-02-06 05:37:09 +03:00
|
|
|
DSN: dsn,
|
|
|
|
mode: mode,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Open() (err error) {
|
|
|
|
// Ensure a DSN is set before attempting to open the database.
|
|
|
|
if db.DSN == "" {
|
|
|
|
return fmt.Errorf("dsn required")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the database.
|
|
|
|
if db.Db, err = sql.Open("sqlite3", db.DSN); err != nil {
|
2022-02-06 05:37:09 +03:00
|
|
|
return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.DSN, err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2022-02-06 05:37:09 +03:00
|
|
|
if db.mode == "dev" {
|
2022-03-28 18:38:12 +03:00
|
|
|
// If mode is dev, then we will migrate and seed the database.
|
|
|
|
if err := db.migrate(); err != nil {
|
|
|
|
return fmt.Errorf("failed to migrate: %w", err)
|
|
|
|
}
|
2022-02-06 05:37:09 +03:00
|
|
|
if err := db.seed(); err != nil {
|
|
|
|
return fmt.Errorf("failed to seed: %w", err)
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-28 18:38:12 +03:00
|
|
|
func (db *DB) migrate() error {
|
|
|
|
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/*.sql", "migration"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(filenames)
|
|
|
|
|
|
|
|
// Loop over all migration files and execute them in order.
|
|
|
|
for _, filename := range filenames {
|
|
|
|
if err := db.executeFile(migrationFS, filename); err != nil {
|
|
|
|
return fmt.Errorf("migrate error: name=%q err=%w", filename, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
func (db *DB) seed() error {
|
|
|
|
filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(filenames)
|
|
|
|
|
|
|
|
// Loop over all seed files and execute them in order.
|
|
|
|
for _, filename := range filenames {
|
2022-03-28 18:38:12 +03:00
|
|
|
if err := db.executeFile(seedFS, filename); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return fmt.Errorf("seed error: name=%q err=%w", filename, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 18:38:12 +03:00
|
|
|
// executeFile runs a single seed file within a transaction.
|
|
|
|
func (db *DB) executeFile(FS embed.FS, name string) error {
|
2022-02-03 10:32:03 +03:00
|
|
|
tx, err := db.Db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2022-03-28 18:38:12 +03:00
|
|
|
// Read and execute SQL file.
|
|
|
|
if buf, err := fs.ReadFile(FS, name); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return err
|
|
|
|
} else if _, err := tx.Exec(string(buf)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
return errors.New("data not found")
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|