chore: update db initial

This commit is contained in:
email 2022-03-28 23:38:12 +08:00
parent 0fea2a86da
commit 3c06c68691
6 changed files with 57 additions and 32 deletions

View file

@ -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);

View file

@ -0,0 +1,4 @@
DELETE FROM resource;
DELETE FROM shortcut;
DELETE FROM memo;
DELETE FROM user;

View file

@ -1,4 +0,0 @@
DROP TABLE IF EXISTS `memo`;
DROP TABLE IF EXISTS `shortcut`;
DROP TABLE IF EXISTS `resource`;
DROP TABLE IF EXISTS `user`;

View file

@ -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'
);

View file

@ -0,0 +1,4 @@
INSERT INTO memo
(`content`, `creator_id`)
VALUES
('👋 Welcome to memos', 101);

View file

@ -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