added create.py in db

This commit is contained in:
Alexey Sokolov
2022-02-11 18:45:56 +03:00
parent 0c83713a17
commit e8b2730aac
5 changed files with 41 additions and 2 deletions

29
db/create.py Normal file
View 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()