update: db initialization

This commit is contained in:
boojack 2022-05-01 11:06:29 +08:00
parent eae858e20b
commit 406109b091
7 changed files with 32 additions and 37 deletions

View File

@ -28,15 +28,11 @@ Making sure that you are in charge of your data and more customizations.
- 📑 Experience excellent interaction logic; - 📑 Experience excellent interaction logic;
- ⚡️ Quick privatization deployment; - ⚡️ Quick privatization deployment;
## ⚓️ Deploy Guide with Docker ## ⚓️ Deploy with Docker
1. download the [initialized db file](https://github.com/justmemos/memos/raw/main/resources/memos_release.db): ```docker
docker run --name memos --publish 8080:8080 --volume ~/path/to/your/data/:/var/opt/memos -e mode=release neosmemo/memos:dev
2. pull and run docker image: ```
```docker
docker run --name memos --publish 8080:8080 --volume ~/path/to/your/data/:/var/opt/memos -e mode=release -e data=/var/opt/memos neosmemo/memos:dev
```
The default user account is `guest` with password `secret`. The default user account is `guest` with password `secret`.

View File

@ -14,7 +14,7 @@ type Profile struct {
// Port is the binding port for server. // Port is the binding port for server.
Port int `json:"port"` Port int `json:"port"`
// DSN points to where Memos stores its own data // DSN points to where Memos stores its own data
DSN string `json:"-"` DSN string `json:"dsn"`
} }
func checkDSN(dataDir string) (string, error) { func checkDSN(dataDir string) (string, error) {
@ -50,7 +50,10 @@ func GetProfile() Profile {
port = 8080 port = 8080
} }
data := os.Getenv("data") data := ""
if mode == "release" {
data = "/var/opt/memos"
}
dataDir, err := checkDSN(data) dataDir, err := checkDSN(data)
if err != nil { if err != nil {
@ -58,7 +61,7 @@ func GetProfile() Profile {
os.Exit(1) os.Exit(1)
} }
dsn := fmt.Sprintf("file:%s/memos_%s.db", dataDir, mode) dsn := fmt.Sprintf("%s/memos_%s.db", dataDir, mode)
return Profile{ return Profile{
Mode: mode, Mode: mode,

View File

@ -1,17 +0,0 @@
# Deploy `memos` with Docker
1. download the initialized db file:
```shell
mkdir ~/data
cd ~/data
wget --no-check-certificate https://github.com/justmemos/memos/blob/main/resources/memos-release.db?raw=true
```
2. pull and run docker image:
```docker
docker run --name memos --publish 8080:8080 --volume ~/path/to/your/data/:/var/opt/memos -e mode=release -e data=/var/opt/memos neosmemo/memos:dev
```
The default user account is `guest` with password `secret`.

View File

@ -56,7 +56,7 @@ func removeUserSession(c echo.Context) error {
func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.HandlerFunc { func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
// Skips auth // Skips auth
if common.HasPrefixes(c.Path(), "/api/auth") { if common.HasPrefixes(c.Path(), "/api/auth", "/api/ping") {
return next(c) return next(c)
} }

View File

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

View File

@ -1,8 +1,3 @@
DROP TABLE IF EXISTS `memo`;
DROP TABLE IF EXISTS `shortcut`;
DROP TABLE IF EXISTS `resource`;
DROP TABLE IF EXISTS `user`;
-- user -- user
CREATE TABLE user ( CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"memos/common" "memos/common"
"os"
"sort" "sort"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -42,18 +43,31 @@ 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 { sqlDB, err := sql.Open("sqlite3", db.DSN)
if err != nil {
return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.DSN, err) return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.DSN, err)
} }
if db.mode == "dev" { db.Db = sqlDB
// If mode is dev, then we will migrate and seed the database.
// If db file not exists, we should migrate and seed the database.
if _, err := os.Stat(db.DSN); errors.Is(err, os.ErrNotExist) {
if err := db.migrate(); err != nil { if err := db.migrate(); err != nil {
return fmt.Errorf("failed to migrate: %w", err) return fmt.Errorf("failed to migrate: %w", err)
} }
if err := db.seed(); err != nil { if err := db.seed(); err != nil {
return fmt.Errorf("failed to seed: %w", err) return fmt.Errorf("failed to seed: %w", err)
} }
} else {
// If db file exists and mode is dev, we should migrate and seed the database.
if db.mode == "dev" {
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)
}
}
} }
return err return err