Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

update initial default database

stevend2084f869be7
diff --git a/resources/initial_db.sql b/resources/initial_db.sql
index 3bc24828a..ce85a4a56 100644
--- a/resources/initial_db.sql
+++ b/resources/initial_db.sql
@@ -1,6 +1,26 @@
-/*
- * Re-create tables and insert initial data
- */
+DROP TABLE IF EXISTS `queries`;
+CREATE TABLE `queries` (
+ `id` TEXT NOT NULL PRIMARY KEY,
+ `user_id` TEXT NOT NULL,
+ `title` TEXT NOT NULL,
+ `querystring` TEXT NOT NULL,
+ `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ `pinned_at` TEXT DEFAULT '',
+ FOREIGN KEY(`user_id`) REFERENCES `users`(`id`)
+);
+
+DROP TABLE IF EXISTS `memos`;
+CREATE TABLE `memos` (
+ `id` TEXT NOT NULL PRIMARY KEY,
+ `content` TEXT NOT NULL,
+ `user_id` TEXT NOT NULL,
+ `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ `deleted_at` TEXT DEFAULT '',
+ FOREIGN KEY(`user_id`) REFERENCES `users`(`id`)
+);
+
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` TEXT NOT NULL PRIMARY KEY,
@@ -15,34 +35,9 @@ CREATE TABLE `users` (
INSERT INTO `users`
(`id`, `username`, `password`)
VALUES
- ('0', 'admin', '123456'),
('1', 'guest', '123456');
-DROP TABLE IF EXISTS `memos`;
-CREATE TABLE `memos` (
- `id` TEXT NOT NULL PRIMARY KEY,
- `content` TEXT NOT NULL,
- `user_id` TEXT NOT NULL,
- `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
- `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
- `deleted_at` TEXT DEFAULT '',
- FOREIGN KEY(`user_id`) REFERENCES `users`(`id`)
-);
-
INSERT INTO `memos`
- (`id`, `content`, `user_id`, )
+ (`id`, `content`, `user_id`)
VALUES
- ('0', 'πŸ‘‹ Welcome to memos', '0'),
('1', 'πŸ‘‹ Welcome to memos', '1');
-
-DROP TABLE IF EXISTS `queries`;
-CREATE TABLE `queries` (
- `id` TEXT NOT NULL PRIMARY KEY,
- `user_id` TEXT NOT NULL,
- `title` TEXT NOT NULL,
- `querystring` TEXT NOT NULL,
- `created_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
- `updated_at` TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
- `pinned_at` TEXT DEFAULT '',
- FOREIGN KEY(`user_id`) REFERENCES `users`(`id`)
-);
diff --git a/resources/memos.db b/resources/memos.db
index 5f0e267e6..5c78111ea 100644
Binary files a/resources/memos.db and b/resources/memos.db differ
diff --git a/store/db.go b/store/db.go
index 2d9d596c2..4a00ae7b3 100644
--- a/store/db.go
+++ b/store/db.go
@@ -2,7 +2,9 @@ package store
import (
"database/sql"
+ "io/ioutil"
"os"
+ "path/filepath"
_ "github.com/mattn/go-sqlite3"
)
@@ -18,7 +20,6 @@ func InitDBConn() {
if _, err := os.Stat(dbFilePath); err != nil {
dbFilePath = "./resources/memos.db"
- resetDataInDefaultDatabase()
println("use the default database")
} else {
println("use the custom database")
@@ -32,6 +33,10 @@ func InitDBConn() {
DB = db
println("connect to sqlite succeed")
}
+
+ if dbFilePath == "./resources/memos.db" {
+ resetDataInDefaultDatabase()
+ }
}
func FormatDBError(err error) error {
@@ -46,5 +51,16 @@ func FormatDBError(err error) error {
}
func resetDataInDefaultDatabase() {
- // do nth
+ initialSQLFilePath := filepath.Join("resources", "initial_db.sql")
+ c, err := ioutil.ReadFile(initialSQLFilePath)
+
+ if err != nil {
+ // do nth
+ return
+ }
+
+ sql := string(c)
+ DB.Exec(sql)
+
+ println("Initial data succeed")
}