chore: update store testing

This commit is contained in:
Steven
2024-01-10 10:08:53 +08:00
parent 4bc2a0ff42
commit 38cd5fabee
17 changed files with 106 additions and 125 deletions

View File

@ -22,9 +22,6 @@ const (
//go:embed migration
var migrationFS embed.FS
//go:embed seed
var seedFS embed.FS
func (d *DB) Migrate(ctx context.Context) error {
if d.profile.IsDev() {
return d.nonProdMigrate(ctx)
@ -67,12 +64,6 @@ func (d *DB) nonProdMigrate(ctx context.Context) error {
return errors.Errorf("failed to exec SQL %s: %s", stmt, err)
}
// In demo mode, we should seed the database.
if d.profile.Mode == "demo" {
if err := d.Seed(ctx); err != nil {
return errors.Wrap(err, "failed to seed")
}
}
return nil
}
@ -156,43 +147,6 @@ func (d *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion str
return nil
}
func (d *DB) Seed(ctx context.Context) error {
filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed"))
if err != nil {
return errors.Wrap(err, "failed to read seed files")
}
sort.Strings(filenames)
// Loop over all seed files and execute them in order.
for _, filename := range filenames {
buf, err := seedFS.ReadFile(filename)
if err != nil {
return errors.Wrapf(err, "failed to read seed file, filename=%s", filename)
}
stmt := string(buf)
if err := d.execute(ctx, stmt); err != nil {
return errors.Wrapf(err, "seed error: %s", stmt)
}
}
return nil
}
// execute runs a single SQL statement within a transaction.
func (d *DB) execute(ctx context.Context, stmt string) error {
tx, err := d.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if _, err := tx.ExecContext(ctx, stmt); err != nil {
return errors.Wrap(err, "failed to execute statement")
}
return tx.Commit()
}
// minorDirRegexp is a regular expression for minor version directory.
var minorDirRegexp = regexp.MustCompile(`^migration/prod/[0-9]+\.[0-9]+$`)

View File

@ -1,9 +0,0 @@
DELETE FROM activity;
DELETE FROM shortcut;
DELETE FROM user_setting;
DELETE FROM user;
DELETE FROM workspace_setting;

View File

@ -4,6 +4,8 @@ import (
"database/sql"
"github.com/pkg/errors"
// SQLite driver.
_ "modernc.org/sqlite"
"github.com/yourselfhosted/slash/server/profile"
"github.com/yourselfhosted/slash/store"

View File

@ -14,7 +14,6 @@ type Driver interface {
Close() error
Migrate(ctx context.Context) error
Seed(ctx context.Context) error
// MigrationHistory model related methods.
UpsertMigrationHistory(ctx context.Context, upsert *UpsertMigrationHistory) (*MigrationHistory, error)