forked from kabukky/journey
/
initialization.go
244 lines (239 loc) · 8.93 KB
/
initialization.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package database
import (
"database/sql"
"github.com/kabukky/journey/database/migration"
"github.com/kabukky/journey/filenames"
"github.com/kabukky/journey/helpers"
"github.com/kabukky/journey/structure"
_ "github.com/mattn/go-sqlite3"
"github.com/twinj/uuid"
"time"
)
// Handler for read access
var readDB *sql.DB
var stmtInitialization = `CREATE TABLE IF NOT EXISTS
posts (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
uuid varchar(36) NOT NULL,
title varchar(150) NOT NULL,
slug varchar(150) NOT NULL,
markdown text,
html text,
image text,
featured tinyint NOT NULL DEFAULT '0',
page tinyint NOT NULL DEFAULT '0',
status varchar(150) NOT NULL DEFAULT 'draft',
language varchar(6) NOT NULL DEFAULT 'en_US',
meta_title varchar(150),
meta_description varchar(200),
author_id integer NOT NULL,
created_at datetime NOT NULL,
created_by integer NOT NULL,
updated_at datetime,
updated_by integer,
published_at datetime,
published_by integer
);
CREATE TABLE IF NOT EXISTS
users (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
uuid varchar(36) NOT NULL,
name varchar(150) NOT NULL,
slug varchar(150) NOT NULL,
password varchar(60) NOT NULL,
email varchar(254) NOT NULL,
image text,
cover text,
bio varchar(200),
website text,
location text,
accessibility text,
status varchar(150) NOT NULL DEFAULT 'active',
language varchar(6) NOT NULL DEFAULT 'en_US',
meta_title varchar(150),
meta_description varchar(200),
last_login datetime,
created_at datetime NOT NULL,
created_by integer NOT NULL,
updated_at datetime,
updated_by integer
);
CREATE TABLE IF NOT EXISTS
tags (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
uuid varchar(36) NOT NULL,
name varchar(150) NOT NULL,
slug varchar(150) NOT NULL,
description varchar(200),
parent_id integer,
meta_title varchar(150),
meta_description varchar(200),
created_at datetime NOT NULL,
created_by integer NOT NULL,
updated_at datetime,
updated_by integer
);
CREATE TABLE IF NOT EXISTS
posts_tags (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
post_id integer NOT NULL,
tag_id integer NOT NULL
);
CREATE TABLE IF NOT EXISTS
settings (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
uuid varchar(36) NOT NULL,
key varchar(150) NOT NULL,
value text,
type varchar(150) NOT NULL DEFAULT 'core',
created_at datetime NOT NULL,
created_by integer NOT NULL,
updated_at datetime,
updated_by integer
);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (1, ?, 'title', 'My Blog', 'blog', ?, 1, ?, 1);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (2, ?, 'description', 'Just another Blog', 'blog', ?, 1, ?, 1);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (3, ?, 'email', '', 'blog', ?, 1, ?, 1);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (4, ?, 'logo', '/public/images/blog-logo.jpg', 'blog', ?, 1, ?, 1);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (5, ?, 'cover', '/public/images/blog-cover.jpg', 'blog', ?, 1, ?, 1);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (6, ?, 'postsPerPage', 5, 'blog', ?, 1, ?, 1);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (7, ?, 'activeTheme', 'promenade', 'theme', ?, 1, ?, 1);
INSERT OR IGNORE INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (8, ?, 'navigation', '[{"label":"Home", "url":"/"}]', 'blog', ?, 1, ?, 1);
CREATE TABLE IF NOT EXISTS
roles (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
uuid varchar(36) NOT NULL,
name varchar(150) NOT NULL,
description varchar(200),
created_at datetime NOT NULL,
created_by integer NOT NULL,
updated_at datetime,
updated_by integer
);
INSERT OR IGNORE INTO roles (id, uuid, name, description, created_at, created_by, updated_at, updated_by) VALUES (1, ?, 'Administrator', 'Administrators', ?, 1, ?, 1);
INSERT OR IGNORE INTO roles (id, uuid, name, description, created_at, created_by, updated_at, updated_by) VALUES (2, ?, 'Editor', 'Editors', ?, 1, ?, 1);
INSERT OR IGNORE INTO roles (id, uuid, name, description, created_at, created_by, updated_at, updated_by) VALUES (3, ?, 'Author', 'Authors', ?, 1, ?, 1);
INSERT OR IGNORE INTO roles (id, uuid, name, description, created_at, created_by, updated_at, updated_by) VALUES (4, ?, 'Owner', 'Blog Owner', ?, 1, ?, 1);
CREATE TABLE IF NOT EXISTS
roles_users (
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
role_id integer NOT NULL,
user_id integer NOT NULL
);
`
func Initialize() error {
// If journey.db does not exist, look for a Ghost database to convert
if !helpers.FileExists(filenames.DatabaseFilename) {
// Convert Ghost database if available (time format needs to change to be compatible with journey)
migration.Ghost()
}
// Open or create database file
var err error
readDB, err = sql.Open("sqlite3", filenames.DatabaseFilename)
if err != nil {
return err
}
readDB.SetMaxIdleConns(256) // TODO: is this enough?
err = readDB.Ping()
if err != nil {
return err
}
currentTime := time.Now()
_, err = readDB.Exec(stmtInitialization, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime)
// TODO: Is Commit()/Rollback() needed for DB.Exec()?
if err != nil {
return err
}
err = checkBlogSettings()
if err != nil {
return err
}
return nil
}
// Function to check and insert any missing blog settings into the database (settings could be missing if migrating from Ghost).
func checkBlogSettings() error {
tempBlog := structure.Blog{}
// Check for title
row := readDB.QueryRow(stmtRetrieveBlog, "title")
err := row.Scan(&tempBlog.Title)
if err != nil {
// Insert title
err = insertSettingString("title", "My Blog", "blog", time.Now(), 1)
if err != nil {
return err
}
}
// Check for description
row = readDB.QueryRow(stmtRetrieveBlog, "description")
err = row.Scan(&tempBlog.Description)
if err != nil {
// Insert description
err = insertSettingString("description", "Just another Blog", "blog", time.Now(), 1)
if err != nil {
return err
}
}
// Check for email
var email []byte
row = readDB.QueryRow(stmtRetrieveBlog, "email")
err = row.Scan(&email)
if err != nil {
// Insert email
err = insertSettingString("email", "", "blog", time.Now(), 1)
if err != nil {
return err
}
}
// Check for logo
row = readDB.QueryRow(stmtRetrieveBlog, "logo")
err = row.Scan(&tempBlog.Logo)
if err != nil {
// Insert logo
err = insertSettingString("logo", "/public/images/blog-logo.jpg", "blog", time.Now(), 1)
if err != nil {
return err
}
}
// Check for cover
row = readDB.QueryRow(stmtRetrieveBlog, "cover")
err = row.Scan(&tempBlog.Cover)
if err != nil {
// Insert cover
err = insertSettingString("cover", "/public/images/blog-cover.jpg", "blog", time.Now(), 1)
if err != nil {
return err
}
}
// Check for postsPerPage
row = readDB.QueryRow(stmtRetrieveBlog, "postsPerPage")
err = row.Scan(&tempBlog.PostsPerPage)
if err != nil {
// Insert postsPerPage
err = insertSettingInt64("postsPerPage", 5, "blog", time.Now(), 1)
if err != nil {
return err
}
}
// Check for activeTheme
row = readDB.QueryRow(stmtRetrieveBlog, "activeTheme")
err = row.Scan(&tempBlog.ActiveTheme)
if err != nil {
// Insert activeTheme
err = insertSettingString("activeTheme", "promenade", "theme", time.Now(), 1)
if err != nil {
return err
}
}
// Check for navigation
var navigation []byte
row = readDB.QueryRow(stmtRetrieveBlog, "navigation")
err = row.Scan(&navigation)
if err != nil {
// Insert navigation
err = insertSettingString("navigation", "[{\"label\":\"Home\", \"url\":\"/\"}]", "blog", time.Now(), 1)
if err != nil {
return err
}
}
return nil
}