diff --git a/store/seed/10001_schema.sql b/store/migration/10001__schema.sql similarity index 89% rename from store/seed/10001_schema.sql rename to store/migration/10001__schema.sql index a450ac4b..0234b942 100644 --- a/store/seed/10001_schema.sql +++ b/store/migration/10001__schema.sql @@ -1,3 +1,8 @@ +DROP TABLE IF EXISTS `memo`; +DROP TABLE IF EXISTS `shortcut`; +DROP TABLE IF EXISTS `resource`; +DROP TABLE IF EXISTS `user`; + -- user CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -115,25 +120,3 @@ SET WHERE rowid = old.rowid; END; - - -INSERT INTO - user ( - `id`, - `name`, - `open_id`, - `password_hash` - ) -VALUES - ( - 1, - 'guest', - 'guest_open_id', - -- "secret" - '$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK' - ); - -INSERT INTO memo - (`content`, `creator_id`) -VALUES - ('👋 Welcome to memos', 1); diff --git a/store/seed/10000__reset.sql b/store/seed/10000__reset.sql new file mode 100644 index 00000000..e1672d3a --- /dev/null +++ b/store/seed/10000__reset.sql @@ -0,0 +1,4 @@ +DELETE FROM resource; +DELETE FROM shortcut; +DELETE FROM memo; +DELETE FROM user; diff --git a/store/seed/10000_reset.sql b/store/seed/10000_reset.sql deleted file mode 100644 index d5eb25b2..00000000 --- a/store/seed/10000_reset.sql +++ /dev/null @@ -1,4 +0,0 @@ -DROP TABLE IF EXISTS `memo`; -DROP TABLE IF EXISTS `shortcut`; -DROP TABLE IF EXISTS `resource`; -DROP TABLE IF EXISTS `user`; diff --git a/store/seed/10001__user.sql b/store/seed/10001__user.sql new file mode 100644 index 00000000..94394e0e --- /dev/null +++ b/store/seed/10001__user.sql @@ -0,0 +1,15 @@ +INSERT INTO + user ( + `id`, + `name`, + `open_id`, + `password_hash` + ) +VALUES + ( + 101, + 'guest', + 'guest_open_id', + -- "secret" + '$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK' + ); diff --git a/store/seed/10002__memo.sql b/store/seed/10002__memo.sql new file mode 100644 index 00000000..7ef4f3d5 --- /dev/null +++ b/store/seed/10002__memo.sql @@ -0,0 +1,4 @@ +INSERT INTO memo + (`content`, `creator_id`) +VALUES + ('👋 Welcome to memos', 101); diff --git a/store/sqlite.go b/store/sqlite.go index 8405c5b5..4b01077a 100644 --- a/store/sqlite.go +++ b/store/sqlite.go @@ -11,6 +11,9 @@ import ( _ "github.com/mattn/go-sqlite3" ) +//go:embed migration +var migrationFS embed.FS + //go:embed seed var seedFS embed.FS @@ -44,7 +47,10 @@ func (db *DB) Open() (err error) { } if db.mode == "dev" { - // If mode is dev, then we will seed the database. + // 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) + } if err := db.seed(); err != nil { return fmt.Errorf("failed to seed: %w", err) } @@ -53,6 +59,23 @@ func (db *DB) Open() (err error) { return err } +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 +} + func (db *DB) seed() error { filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed")) if err != nil { @@ -63,23 +86,23 @@ func (db *DB) seed() error { // Loop over all seed files and execute them in order. for _, filename := range filenames { - if err := db.seedFile(filename); err != nil { + if err := db.executeFile(seedFS, filename); err != nil { return fmt.Errorf("seed error: name=%q err=%w", filename, err) } } return nil } -// seedFile runs a single seed file within a transaction. -func (db *DB) seedFile(name string) error { +// executeFile runs a single seed file within a transaction. +func (db *DB) executeFile(FS embed.FS, name string) error { tx, err := db.Db.Begin() if err != nil { return err } defer tx.Rollback() - // Read and execute migration file. - if buf, err := fs.ReadFile(seedFS, name); err != nil { + // Read and execute SQL file. + if buf, err := fs.ReadFile(FS, name); err != nil { return err } else if _, err := tx.Exec(string(buf)); err != nil { return err