-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
db.py
62 lines (51 loc) · 2.1 KB
/
db.py
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
import os
import logging
import sqlite3
from werkzeug.security import generate_password_hash
database_path = "./data/topaz.sqlite"
def init_db():
"""
Create database and tables if it isn't present
"""
if os.path.exists(database_path):
# Check if table blogs has the field 'automatically_added' or not
# If not, add one
try:
db = get_db()
c = db.cursor()
# 1: True
# NULL, 0: False
c.execute('ALTER TABLE blogs ADD COLUMN automatically_added INTEGER')
except Exception:
logging.warning(
"[W0002] Column 'automatically_added' is already exists. "
"No need to add it."
)
db.commit()
db.close()
return
# Create database and tables
db = get_db()
c = db.cursor()
c.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT UNIQUE, password TEXT)")
c.execute("CREATE TABLE public_repos (id INTEGER PRIMARY KEY, primary_language TEXT, primary_language_color TEXT, "
"stars TEXT, title TEXT, description TEXT, readme TEXT, latest_commit TEXT, url TEXT, image_url TEXT, "
"timestamp TEXT, visible INTEGER)")
c.execute("CREATE TABLE blogs (id INTEGER PRIMARY KEY, title TEXT, description TEXT, "
"url TEXT, image_url TEXT, timestamp TEXT, automatically_added INTEGER, "
"file_type INTEGER, file_name TEXT)")
c.execute("CREATE TABLE publications (id INTEGER PRIMARY KEY, title TEXT, description TEXT, "
"url TEXT, image_url TEXT, timestamp TEXT, visible INTEGER)")
# Create admin
c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (os.getenv("USERNAME"),
generate_password_hash(os.getenv("PASSWORD"))))
db.commit()
db.close()
def get_db():
"""
Returns a database connection and sets output for be like dicts
Returns (db object): sqlite3 db object
"""
db = sqlite3.connect(database_path)
db.row_factory = sqlite3.Row
return db