Some refactor. Some docstrings.

This commit is contained in:
Alexey Sokolov
2022-02-14 16:52:15 +03:00
parent e8b2730aac
commit 3cd506682f
3 changed files with 86 additions and 28 deletions

View File

@@ -1,5 +1,46 @@
"""
Create database file and tables if not exists.
::
+--------------+
|services | +---------------+
+--------------+ |notifications |
|service_id |<--------+ +---------------+
|token | | |notification_id|
|name | +-------|service_id |
|description | |timestamp |
|representation| |content_json |
|by_request | +---------------+
+--------------+
* services table
* **service_id:** service id
*(integer primary key)*.
* **token:** service token for access to api
*(text, not null)*.
* **name:** service name
*(text, not null)*.
* **description:** service description
*(text with default value)*.
* **representation:** how to display notification
*(integer with 0 as default)*.
* **by_request:** send notifications automatically or not
*(integer with 0 as default)*.
* notifications table
* **notification_id:** notification id
*(integer primary key)*.
* **service_id:** service id
*(integer, not null, references to*
*service_id in service table)*.
* **timestamp:** UTC timestamp, example: **"2022-02-14T13:45:15Z"**
*(text, not null)*.
* **content_json:** content in json_format
*(text, not null)*.
"""
import sqlite3
CREATE_SERVICES_TABLE = '''CREATE TABLE IF NOT EXISTS services (
service_id INTEGER PRIMARY KEY,
token TEXT NOT NULL,
@@ -20,6 +61,12 @@ ON UPDATE CASCADE ON DELETE CASCADE
def create_tables_if_not_exists(dbfile: str) -> None:
"""
Create database file and tables if not exists.
*See module docstring*.
:param dbfile: path to database file.
"""
connection = sqlite3.connect(dbfile)
cursor = connection.cursor()
cursor.execute('PRAGMA foreign_keys=0')