add database migrations
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

This commit is contained in:
mitsuha_s 2022-08-21 16:03:00 +03:00
parent 90a26dd1c8
commit 8766e36aa6
3 changed files with 28 additions and 21 deletions

View file

@ -4,6 +4,8 @@ from psycopg2.extensions import connection
from psycopg2.extras import DictCursor, DictRow from psycopg2.extras import DictCursor, DictRow
from exceptions import DisplayableException from exceptions import DisplayableException
from rss import FeedItem from rss import FeedItem
from yoyo import read_migrations
from yoyo import get_backend
class Database: class Database:
@ -15,7 +17,7 @@ class Database:
self.log.debug('Database.__init__(DSN=\'%s\')', dsn) self.log.debug('Database.__init__(DSN=\'%s\')', dsn)
self.conn: connection = psycopg2.connect(dsn) self.conn: connection = psycopg2.connect(dsn)
self.cur: DictCursor = self.conn.cursor(cursor_factory=DictCursor) self.cur: DictCursor = self.conn.cursor(cursor_factory=DictCursor)
self.__init_schema() self.__migration(dsn)
def add_user(self, telegram_id: int) -> int: def add_user(self, telegram_id: int) -> int:
"""Add a user's telegram id to the database and return its database id.""" """Add a user's telegram id to the database and return its database id."""
@ -156,26 +158,12 @@ class Database:
'INSERT INTO feeds_last_items (feed_id, url, guid) VALUES (%s, %s, %s)', new_items) 'INSERT INTO feeds_last_items (feed_id, url, guid) VALUES (%s, %s, %s)', new_items)
self.conn.commit() self.conn.commit()
def __init_schema(self) -> None: def __migration(self, dsn: str) -> None:
self.log.debug('__init_schema()') backend = get_backend(dsn)
self.cur.execute( migrations = read_migrations('./migrations')
'CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, telegram_id INTEGER NOT NULL UNIQUE)'
) with backend.lock():
self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id SERIAL PRIMARY KEY, url TEXT NOT NULL UNIQUE)') backend.apply_migrations(backend.to_apply(migrations))
self.cur.execute(
'CREATE TABLE IF NOT EXISTS subscriptions ('
' user_id INTEGER REFERENCES users,'
' feed_id INTEGER REFERENCES feeds,'
' UNIQUE (user_id, feed_id)'
')'
)
self.cur.execute(
'CREATE TABLE IF NOT EXISTS feeds_last_items ('
' feed_id INTEGER REFERENCES feeds ON DELETE CASCADE,'
' url TEXT NOT NULL,'
' guid TEXT'
')'
)
@staticmethod @staticmethod
def __dictrow_to_dict_list(rows: list[DictRow]) -> list[dict]: def __dictrow_to_dict_list(rows: list[DictRow]) -> list[dict]:

View file

@ -0,0 +1,18 @@
from yoyo import step
steps = [
step('CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, telegram_id INTEGER NOT NULL UNIQUE)'),
step('CREATE TABLE IF NOT EXISTS feeds (id SERIAL PRIMARY KEY, url TEXT NOT NULL UNIQUE)'),
step('CREATE TABLE IF NOT EXISTS subscriptions ('
' user_id INTEGER REFERENCES users,'
' feed_id INTEGER REFERENCES feeds,'
' UNIQUE (user_id, feed_id)'
')'
),
step('CREATE TABLE IF NOT EXISTS feeds_last_items ('
' feed_id INTEGER REFERENCES feeds ON DELETE CASCADE,'
' url TEXT NOT NULL,'
' guid TEXT'
')'
)
]

View file

@ -13,3 +13,4 @@ six==1.16.0
urllib3==1.26.9 urllib3==1.26.9
validators==0.19.0 validators==0.19.0
webencodings==0.5.1 webencodings==0.5.1
yoyo-migrations==7.3.2