mirror of
https://github.com/aykhans/slash-e.git
synced 2025-06-14 20:07:50 +00:00
chore: add demo mode
This commit is contained in:
@ -19,6 +19,9 @@ import (
|
||||
//go:embed migration
|
||||
var migrationFS embed.FS
|
||||
|
||||
//go:embed seed
|
||||
var seedFS embed.FS
|
||||
|
||||
type DB struct {
|
||||
profile *profile.Profile
|
||||
// sqlite db connection instance
|
||||
@ -119,6 +122,12 @@ func (db *DB) Open(ctx context.Context) (err error) {
|
||||
if err := db.applyLatestSchema(ctx); err != nil {
|
||||
return fmt.Errorf("failed to apply latest schema: %w", err)
|
||||
}
|
||||
// In demo mode, we should seed the database.
|
||||
if db.profile.Mode == "demo" {
|
||||
if err := db.seed(ctx); err != nil {
|
||||
return fmt.Errorf("failed to seed: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,6 +194,28 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (db *DB) seed(ctx context.Context) error {
|
||||
filenames, err := fs.Glob(seedFS, fmt.Sprintf("%s/*.sql", "seed"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read seed files, err: %w", err)
|
||||
}
|
||||
|
||||
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 fmt.Errorf("failed to read seed file, filename=%s err=%w", filename, err)
|
||||
}
|
||||
stmt := string(buf)
|
||||
if err := db.execute(ctx, stmt); err != nil {
|
||||
return fmt.Errorf("seed error: statement:%s err=%w", stmt, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// execute runs a single SQL statement within a transaction.
|
||||
func (db *DB) execute(ctx context.Context, stmt string) error {
|
||||
tx, err := db.DBInstance.Begin()
|
||||
|
9
store/db/seed/10000__reset.sql
Normal file
9
store/db/seed/10000__reset.sql
Normal file
@ -0,0 +1,9 @@
|
||||
DELETE FROM activity;
|
||||
|
||||
DELETE FROM shortcut;
|
||||
|
||||
DELETE FROM user_setting;
|
||||
|
||||
DELETE FROM user;
|
||||
|
||||
DELETE FROM workspace_setting;
|
35
store/db/seed/10001__user.sql
Normal file
35
store/db/seed/10001__user.sql
Normal file
@ -0,0 +1,35 @@
|
||||
INSERT INTO
|
||||
user (
|
||||
`id`,
|
||||
`role`,
|
||||
`email`,
|
||||
`nickname`,
|
||||
`password_hash`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
101,
|
||||
'ADMIN',
|
||||
'slash@stevenlgtm.com',
|
||||
'Slasher',
|
||||
-- raw password: secret
|
||||
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
user (
|
||||
`id`,
|
||||
`role`,
|
||||
`email`,
|
||||
`nickname`,
|
||||
`password_hash`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
102,
|
||||
'USER',
|
||||
'steven@usememos.com',
|
||||
'Steven',
|
||||
-- raw password: secret
|
||||
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
|
||||
);
|
67
store/db/seed/10002__shortcut.sql
Normal file
67
store/db/seed/10002__shortcut.sql
Normal file
@ -0,0 +1,67 @@
|
||||
INSERT INTO
|
||||
shortcut (
|
||||
`id`,
|
||||
`creator_id`,
|
||||
`name`,
|
||||
`link`,
|
||||
`visibility`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
1,
|
||||
101,
|
||||
'memos',
|
||||
'https://usememos.com',
|
||||
'PUBLIC'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
shortcut (
|
||||
`id`,
|
||||
`creator_id`,
|
||||
`name`,
|
||||
`link`,
|
||||
`visibility`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
2,
|
||||
101,
|
||||
'sqlchat',
|
||||
'https://www.sqlchat.ai',
|
||||
'WORKSPACE'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
shortcut (
|
||||
`id`,
|
||||
`creator_id`,
|
||||
`name`,
|
||||
`link`,
|
||||
`visibility`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
3,
|
||||
101,
|
||||
'schema-change',
|
||||
'https://www.bytebase.com/blog/how-to-handle-database-schema-change/#what-is-a-database-schema-change',
|
||||
'PUBLIC'
|
||||
);
|
||||
|
||||
INSERT INTO
|
||||
shortcut (
|
||||
`id`,
|
||||
`creator_id`,
|
||||
`name`,
|
||||
`link`,
|
||||
`visibility`
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
4,
|
||||
102,
|
||||
'stevenlgtm',
|
||||
'https://github.com/boojack',
|
||||
'PUBLIC'
|
||||
);
|
Reference in New Issue
Block a user