Migrate to PostgreSQL #39

Merged
Miroslavsckaya merged 16 commits from migrate_to_postgresql into master 2022-07-13 22:53:54 +00:00
Showing only changes of commit 5d0a6843b6 - Show all commits

View file

@ -22,9 +22,9 @@ class Database:
def add_user(self, telegram_id: int) -> int:
"""Add a user's telegram id to the database and return its database id."""
self.log.debug('add_user(telegram_id=\'%s\')', telegram_id)
id = self.cur.execute('INSERT INTO users (telegram_id) VALUES (%s) RETURNING id', [telegram_id])
self.cur.execute('INSERT INTO users (telegram_id) VALUES (%s) RETURNING id', [telegram_id])
self.conn.commit()
return id
return self.cur.fetchone()[0]
def find_user(self, telegram_id: int) -> int | None:
"""Get a user's telegram id and return its database id."""
@ -38,9 +38,9 @@ class Database:
def add_feed(self, url: str) -> int:
"""Add a feed to the database and return its id."""
self.log.debug('add_feed(url=\'%s\')', url)
id = self.cur.execute('INSERT INTO feeds (url) VALUES (%s) RETURNING id', [url])
self.cur.execute('INSERT INTO feeds (url) VALUES (%s) RETURNING id', [url])
self.conn.commit()
return id
return self.cur.fetchone()[0]
def find_feed_by_url(self, url: str) -> int | None:
"""Find feed ID by url."""
@ -121,7 +121,7 @@ class Database:
subscribers = self.cur.fetchall()
return list(map(lambda x: x['telegram_id'], subscribers))
def find_feeds(self) -> list[psycopg2.extras.DictRow]:
def find_feeds(self) -> list[DictRow]:
"""Get a list of feeds."""
self.log.debug('find_feeds()')
self.cur.execute('SELECT * FROM feeds')