@@ -161,7 +161,7 @@ def create_database_if_not_exists(db_name: str) -> None:
161161
162162
163163def init_database () -> None :
164- create_database_if_not_exists (settings .db_news )
164+ create_database_if_not_exists (settings .news_db )
165165
166166
167167def create_search_requests_table () -> None :
@@ -192,7 +192,7 @@ def create_search_requests_table() -> None:
192192 """ ,
193193 ]
194194
195- with get_cursor (settings .db_news ) as (conn , cur ):
195+ with get_cursor (settings .news_db ) as (conn , cur ):
196196 cur .execute (query )
197197 for index_query in index_list :
198198 cur .execute (index_query )
@@ -218,7 +218,7 @@ def create_articles_table() -> None:
218218 ON articles(published_at DESC)
219219 """
220220
221- with get_cursor (settings .db_news ) as (conn , cur ):
221+ with get_cursor (settings .news_db ) as (conn , cur ):
222222 cur .execute (query )
223223 cur .execute (index )
224224 conn .commit ()
@@ -252,7 +252,7 @@ def create_user_news_table() -> None:
252252 """ ,
253253 ]
254254
255- with get_cursor (settings .db_news ) as (conn , cur ):
255+ with get_cursor (settings .news_db ) as (conn , cur ):
256256 cur .execute (query )
257257 for index_query in index_list :
258258 cur .execute (index_query )
@@ -298,7 +298,7 @@ def create_request_stats_table() -> None:
298298 END $$;
299299 """
300300
301- with get_cursor (settings .db_news ) as (conn , cur ):
301+ with get_cursor (settings .news_db ) as (conn , cur ):
302302 cur .execute (query )
303303 cur .execute (trigger_function )
304304 cur .execute (trigger )
@@ -318,7 +318,7 @@ def create_app_users_table() -> None:
318318 )
319319 """
320320
321- with get_cursor (settings .db_news ) as (conn , cur ):
321+ with get_cursor (settings .news_db ) as (conn , cur ):
322322 cur .execute (query )
323323 conn .commit ()
324324
@@ -338,10 +338,53 @@ def create_news_tables() -> None:
338338 )
339339 """
340340
341- with get_cursor (settings .db_news ) as (conn , cur ):
341+ with get_cursor (settings .news_db ) as (conn , cur ):
342342 cur .execute (query )
343343 conn .commit ()
344344
345+ def create_users_keys_table () -> None :
346+ query = """
347+ CREATE TABLE IF NOT EXISTS users_keys(
348+ id BIGSERIAL PRIMARY KEY,
349+ user_id BIGINT NOT NULL REFERENCES app_users(id) ON DELETE CASCADE,
350+ service VARCHAR(50) NOT NULL,
351+ encrypted_key TEXT NOT NULL,
352+ iv TEXT NOT NULL,
353+ auth_tag TEXT NOT NULL,
354+ key_last4 VARCHAR(4) NOT NULL,
355+ uploaded_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
356+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
357+ UNIQUE (user_id, service)
358+ );
359+ """
360+ trigger_function = """
361+ CREATE OR REPLACE FUNCTION set_users_keys_updated_at()
362+ RETURNS TRIGGER AS $$
363+ BEGIN
364+ NEW.updated_at = NOW();
365+ RETURN NEW;
366+ END;
367+ $$ LANGUAGE plpgsql;
368+ """
369+ trigger = """
370+ DO $$
371+ BEGIN
372+ IF NOT EXISTS (
373+ SELECT 1 FROM pg_trigger WHERE tgname = 'trg_users_keys_updated_at'
374+ ) THEN
375+ CREATE TRIGGER trg_users_keys_updated_at
376+ BEFORE UPDATE ON users_keys
377+ FOR EACH ROW
378+ EXECUTE FUNCTION set_users_keys_updated_at();
379+ END IF;
380+ END $$;
381+ """
382+ with get_cursor (settings .news_db ) as (conn , cur ):
383+ cur .execute (query )
384+ cur .execute (trigger_function )
385+ cur .execute (trigger )
386+ conn .commit ()
387+
345388
346389def claim_next_search_request () -> dict | None :
347390 query = """
@@ -363,7 +406,7 @@ def claim_next_search_request() -> dict | None:
363406 RETURNING sr.id, sr.user_id, sr.keyword, sr.language, sr.limit_count, sr.page_size
364407 """
365408
366- with get_cursor (settings .db_news ) as (conn , cur ):
409+ with get_cursor (settings .news_db ) as (conn , cur ):
367410 cur .execute (query )
368411 row = cur .fetchone ()
369412 conn .commit ()
@@ -372,20 +415,20 @@ def claim_next_search_request() -> dict | None:
372415
373416def search_request_exists (search_request_id : int ) -> bool :
374417 query = "SELECT 1 FROM search_requests WHERE id = %s"
375- with get_cursor (settings .db_news , autocommit = True ) as (_ , cur ):
418+ with get_cursor (settings .news_db , autocommit = True ) as (_ , cur ):
376419 cur .execute (query , (search_request_id ,))
377420 return cur .fetchone () is not None
378421
379422
380423def app_user_exists (user_id : int ) -> bool :
381424 query = "SELECT 1 FROM app_users WHERE id = %s"
382- with get_cursor (settings .db_news , autocommit = True ) as (_ , cur ):
425+ with get_cursor (settings .news_db , autocommit = True ) as (_ , cur ):
383426 cur .execute (query , (user_id ,))
384427 return cur .fetchone () is not None
385428
386429
387430def search_request_belongs_to_user (search_request_id : int , user_id : int ) -> bool :
388431 query = "SELECT 1 FROM search_requests WHERE id = %s AND user_id = %s"
389- with get_cursor (settings .db_news , autocommit = True ) as (_ , cur ):
432+ with get_cursor (settings .news_db , autocommit = True ) as (_ , cur ):
390433 cur .execute (query , (search_request_id , user_id ))
391434 return cur .fetchone () is not None
0 commit comments