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

View File

@ -29,7 +29,7 @@ func Execute() {
} }
func (m *Main) Run() error { 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 { if err := db.Open(); err != nil {
return fmt.Errorf("cannot open db: %w", err) 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 { type DB struct {
Db *sql.DB Db *sql.DB
// Datasource name. // datasource name
DSN string DSN string
// mode: release or dev
mode string
} }
// NewDB returns a new instance of DB associated with the given datasource name. // 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{ db := &DB{
DSN: dsn, DSN: dsn,
mode: mode,
} }
return db return db
} }
@ -37,11 +40,14 @@ func (db *DB) Open() (err error) {
// Connect to the database. // Connect to the database.
if db.Db, err = sql.Open("sqlite3", db.DSN); err != nil { 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 err := db.seed(); err != nil { if db.mode == "dev" {
return fmt.Errorf("failed to seed: %w", err) // 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 return err