From e8b2730aacf4884731783e90681a9904df28021c Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Fri, 11 Feb 2022 18:45:56 +0300 Subject: [PATCH] added create.py in db --- .gitignore | 1 + config.py | 2 -- db/__init__.py | 10 ++++++++++ db/create.py | 29 +++++++++++++++++++++++++++++ start.py | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 db/create.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63bc908 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/res/db/database.db diff --git a/config.py b/config.py index 6ff2657..3ac6da9 100644 --- a/config.py +++ b/config.py @@ -23,7 +23,6 @@ class ConfigError(Exception): _teletoken = getenv('LTLNOTIFIER_TELETOKEN') -print(_teletoken[9]) # Check api token if not _teletoken: raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN not set.') @@ -31,7 +30,6 @@ if len(_teletoken) != 45 or _teletoken[9] != ':' or not _teletoken[:9].isdigit() raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN incorrect.') _bot_owner = getenv('LTLNOTIFIER_BOT_OWNER') -print(_bot_owner) # Check bot owner if not _bot_owner: raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER not set.') diff --git a/db/__init__.py b/db/__init__.py index e69de29..f3c89e6 100644 --- a/db/__init__.py +++ b/db/__init__.py @@ -0,0 +1,10 @@ +import aiosqlite + +from .create import create_tables_if_not_exists + + +DB_PATH = 'res/db/' +DB_FILENAME = 'database.db' + + +create_tables_if_not_exists(f'{DB_PATH}{DB_FILENAME}') diff --git a/db/create.py b/db/create.py new file mode 100644 index 0000000..879fe51 --- /dev/null +++ b/db/create.py @@ -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() diff --git a/start.py b/start.py index 3753179..7e88d3c 100644 --- a/start.py +++ b/start.py @@ -3,6 +3,7 @@ import logging from aiohttp import web from config import config +import db from web import app