Database migration #45
|
@ -3,10 +3,8 @@ import psycopg2
|
|||
from psycopg2.extensions import connection
|
||||
from psycopg2.extras import DictCursor, DictRow
|
||||
from exceptions import DisplayableException
|
||||
from yoyo import read_migrations, get_backend
|
||||
from rss import FeedItem
|
||||
from yoyo import read_migrations
|
||||
from yoyo import get_backend
|
||||
|
||||
|
||||
class Database:
|
||||
"""Implement interaction with the database."""
|
||||
|
@ -17,7 +15,7 @@ class Database:
|
|||
self.log.debug('Database.__init__(DSN=\'%s\')', dsn)
|
||||
self.conn: connection = psycopg2.connect(dsn)
|
||||
self.cur: DictCursor = self.conn.cursor(cursor_factory=DictCursor)
|
||||
self.__migration(dsn)
|
||||
self.__migrate(dsn)
|
||||
|
||||
def add_user(self, telegram_id: int) -> int:
|
||||
"""Add a user's telegram id to the database and return its database id."""
|
||||
|
@ -158,7 +156,7 @@ class Database:
|
|||
'INSERT INTO feeds_last_items (feed_id, url, guid) VALUES (%s, %s, %s)', new_items)
|
||||
self.conn.commit()
|
||||
|
||||
def __migration(self, dsn: str) -> None:
|
||||
def __migrate(self, dsn: str) -> None:
|
||||
backend = get_backend(dsn)
|
||||
skobkin marked this conversation as resolved
|
||||
migrations = read_migrations('./migrations')
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
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'
|
||||
')'
|
||||
)
|
||||
]
|
20
migrations/0000.initial_schema.py
Normal file
20
migrations/0000.initial_schema.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
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'
|
||||
')'
|
||||
)
|
||||
]
|
Loading…
Reference in a new issue
Catch and log some errors maybe? 🙂