forked from Mereithhh/van-nav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.db.go
147 lines (139 loc) · 3.11 KB
/
init.db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"database/sql"
"fmt"
)
func initDB() {
PathExistsOrCreate("./data")
// 创建数据库
db, _ = sql.Open("sqlite", "./data/nav.db")
// user 表
sql_create_table := `
CREATE TABLE IF NOT EXISTS nav_user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
password TEXT
);
`
_, err := db.Exec(sql_create_table)
checkErr(err)
// setting 表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_setting (
id INTEGER PRIMARY KEY AUTOINCREMENT,
favicon TEXT,
title TEXT,
logo192 TEXT,
logo512 TEXT,
hideAdmin BOOLEAN
);
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// 增加 logo192 字段
sql_add_logo192 := `
ALTER TABLE nav_setting ADD COLUMN logo192 TEXT;
`
db.Exec(sql_add_logo192)
// 增加 logo512 字段
sql_add_logo512 := `
ALTER TABLE nav_setting ADD COLUMN logo512 TEXT;
`
db.Exec(sql_add_logo512)
// 默认 tools 用的 表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
url TEXT,
logo TEXT,
catelog TEXT,
desc TEXT
);
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// tools数据表结构升级-20230327
sql_create_table = `
ALTER TABLE nav_table ADD COLUMN sort INTEGER;
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// 分类表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_catelog (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// 分类表表结构升级-20230327
sql_create_table = `
ALTER TABLE nav_catelog ADD COLUMN sort INTEGER;
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// api token 表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_api_token (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
value TEXT,
disabled INTEGER
);
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// img 表
sql_create_table = `
CREATE TABLE IF NOT EXISTS nav_img (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT,
value TEXT
);
`
_, err = db.Exec(sql_create_table)
checkErr(err)
// 如果不存在,就初始化用户
sql_get_user := `
SELECT * FROM nav_user;
`
rows, err := db.Query(sql_get_user)
checkErr(err)
if !rows.Next() {
sql_add_user := `
INSERT INTO nav_user (id, name, password)
VALUES (?, ?, ?);
`
stmt, err := db.Prepare(sql_add_user)
checkErr(err)
res, err := stmt.Exec(generateId(), "admin", "admin")
checkErr(err)
_, err = res.LastInsertId()
checkErr(err)
// fmt.Println(id)
}
rows.Close()
// 如果不存在设置,就初始化
sql_get_setting := `
SELECT * FROM nav_setting;
`
rows, err = db.Query(sql_get_setting)
checkErr(err)
if !rows.Next() {
sql_add_setting := `
INSERT INTO nav_setting (id, favicon, title, logo192, logo512, hideAdmin)
VALUES (?, ?, ?, ?, ?, ?);
`
stmt, err := db.Prepare(sql_add_setting)
checkErr(err)
res, err := stmt.Exec(0, "favicon.ico", "Van Nav", "logo192.png", "logo512.png", false)
checkErr(err)
_, err = res.LastInsertId()
checkErr(err)
// fmt.Println(id)
}
rows.Close()
fmt.Println("数据库初始化成功。。。")
}