added create.py in db
This commit is contained in:
29
db/create.py
Normal file
29
db/create.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import sqlite3
|
||||
|
||||
CREATE_SERVICES_TABLE = '''CREATE TABLE IF NOT EXISTS services (
|
||||
service_id INTEGER PRIMARY KEY,
|
||||
token TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT DEFAULT "Нет описания.",
|
||||
representation INTEGER DEFAULT 0,
|
||||
by_request INTEGER DEFAULT 0
|
||||
);'''
|
||||
|
||||
CREATE_NOTIFICATIONS_TABLE = '''CREATE TABLE IF NOT EXISTS notifications (
|
||||
notification_id INTEGER PRIMARY KEY,
|
||||
service_id INTEGER NOT NULL,
|
||||
timestamp TEXT NOT NULL,
|
||||
content_json TEXT NOT NULL,
|
||||
FOREIGN KEY (service_id) REFERENCES services(service_id)
|
||||
ON UPDATE CASCADE ON DELETE CASCADE
|
||||
);'''
|
||||
|
||||
|
||||
def create_tables_if_not_exists(dbfile: str) -> None:
|
||||
connection = sqlite3.connect(dbfile)
|
||||
cursor = connection.cursor()
|
||||
cursor.execute('PRAGMA foreign_keys=0')
|
||||
cursor.execute(CREATE_SERVICES_TABLE)
|
||||
cursor.execute(CREATE_NOTIFICATIONS_TABLE)
|
||||
connection.commit()
|
||||
connection.close()
|
||||
Reference in New Issue
Block a user