feat: db release mode

This commit is contained in:
email 2022-02-06 10:37:09 +08:00
parent c77996a891
commit ff5a1eebd7
5 changed files with 15 additions and 10 deletions

View File

@ -31,7 +31,7 @@ func checkDSN(dataDir string) (string, error) {
dataDir = strings.TrimRight(dataDir, "/")
if _, err := os.Stat(dataDir); err != nil {
error := fmt.Errorf("unable to access --data %s, %w", dataDir, err)
error := fmt.Errorf("unable to access -data %s, err %w", dataDir, err)
return "", error
}
@ -43,12 +43,11 @@ func GetProfile() Profile {
mode := flag.String("mode", "dev", "")
port := flag.Int("port", 8080, "")
data := flag.String("data", "", "")
flag.Parse()
dataDir, err := checkDSN(*data)
if err != nil {
fmt.Printf("%+v\n", err)
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
os.Exit(1)
}

View File

@ -29,7 +29,7 @@ func Execute() {
}
func (m *Main) Run() error {
db := store.NewDB(m.profile.dsn)
db := store.NewDB(m.profile.dsn, m.profile.mode)
if err := db.Open(); err != nil {
return fmt.Errorf("cannot open db: %w", err)

Binary file not shown.

BIN
resources/memos_release.db Normal file

Binary file not shown.

View File

@ -17,14 +17,17 @@ var seedFS embed.FS
type DB struct {
Db *sql.DB
// Datasource name.
// datasource name
DSN string
// mode: release or dev
mode string
}
// NewDB returns a new instance of DB associated with the given datasource name.
func NewDB(dsn string) *DB {
func NewDB(dsn string, mode string) *DB {
db := &DB{
DSN: dsn,
mode: mode,
}
return db
}
@ -37,12 +40,15 @@ func (db *DB) Open() (err error) {
// Connect to the database.
if db.Db, err = sql.Open("sqlite3", db.DSN); err != nil {
return err
return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.DSN, err)
}
if db.mode == "dev" {
// If mode is dev, then we will seed the database.
if err := db.seed(); err != nil {
return fmt.Errorf("failed to seed: %w", err)
}
}
return err
}