Files
ltlnotifyer/db/create.py
2022-02-11 18:45:56 +03:00

30 lines
882 B
Python

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()