This repository was archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinit_db.py
More file actions
56 lines (49 loc) · 1.68 KB
/
init_db.py
File metadata and controls
56 lines (49 loc) · 1.68 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
import os
import sqlite3
import tomllib
CONFIG_FILE = "config.toml"
DB_FILE = "blog.db"
if not os.path.exists(CONFIG_FILE):
print("Unable to find config file.")
print("Failed to create database.")
exit(1)
if not os.path.exists(DB_FILE):
print("No database file found, creating database...")
config = tomllib.loads(open(CONFIG_FILE, "r").read())
conn = sqlite3.connect(DB_FILE)
c = conn.cursor()
print("Initializing admin config......")
c.execute(
"""CREATE TABLE admin_config
(username text NOT NULL PRIMARY KEY, password text NOT NULL, token text);"""
)
c.execute(
"""INSERT INTO admin_config VALUES (\"%s\", \"%s\", "token");"""
% (
config["admin"]["username"],
hashlib.md5("admin".encode("utf-8")).hexdigest(),
)
)
print("Initializing articles table...")
c.execute(
"""CREATE TABLE articles
(id integer NOT NULL PRIMARY KEY autoincrement, title text NOT NULL, content text NOT NULL, tag text NOT NULL, datetime text NOT NULL);"""
)
c.execute("CREATE UNIQUE INDEX articles_id ON articles(id);")
c.execute(
"""CREATE TABLE pages
(id integer NOT NULL PRIMARY KEY, title text NOT NULL, content text NOT NULL);"""
)
print("Initializing tags table...")
c.execute(
"""CREATE TABLE tags
(id integer NOT NULL PRIMARY KEY autoincrement, name text NOT NULL, article_id integer NOT NULL);"""
)
conn.commit()
conn.close()
print("Database created successfully.")
else:
print("Skip creating database due to existing database file.")