@@ -72,87 +72,118 @@ def init_database() -> None:
7272
7373def create_search_requests_table () -> None :
7474 query = """
75- CEATE TABLE IF NOT EXISTS search_requests (
76- id BIGSERIAL PRIMARY KEY,
77- user_id BIGINT NOT NULL REFERENCES app_users(id) ON DELETE CASCADE,
78- keyword TEXT NOT NULL,
79- language VARCHAR(10) NOT NULL DEFAULT 'ru',
80- limit_count INTEGER NOT NULL CHECK (limit_count > 0),
81- page_size INTEGER NOT NULL CHECK (page_size > 0),
82- status VARCHAR(20) NOT NULL, CHECK (status IN ('queued', 'running', 'success', 'failed')),
83- error_text TEXT,
84- created_at TIMESTAMP NOT NULL DEFAULT NOW(),
85- stated_at TIMESTAMP,
86- finished_at TIMESTAMP
75+ CREATE TABLE IF NOT EXISTS search_requests (
76+ id BIGSERIAL PRIMARY KEY,
77+ user_id BIGINT NOT NULL REFERENCES app_users(id) ON DELETE CASCADE,
78+ keyword TEXT NOT NULL,
79+ language VARCHAR(10) NOT NULL DEFAULT 'ru',
80+ limit_count INTEGER NOT NULL CHECK (limit_count > 0),
81+ page_size INTEGER NOT NULL CHECK (page_size > 0),
82+ status VARCHAR(20) NOT NULL CHECK (status IN ('queued', 'running', 'success', 'failed')),
83+ error_text TEXT,
84+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),
85+ started_at TIMESTAMP,
86+ finished_at TIMESTAMP
8787 );
8888 """
89- with get_cursor (settings .db_search_requests ) as (conn , cur ):
89+ index_list = ["""
90+ CREATE INDEX IF NOT EXISTS idx_search_requests_user_id
91+ ON search_requests(user_id)
92+ """ ,
93+ """
94+ CREATE INDEX IF NOT EXISTS idx_search_requests_status
95+ ON search_requests(status)
96+ """ ]
97+ with get_cursor (settings .db_news ) as (conn , cur ):
9098 cur .execute (query )
99+ for index_query in index_list :
100+ cur .execute (index_query )
101+
91102 conn .commit ()
103+
92104
93105def create_articles_table () -> None :
94106 query = """
95- CEATE TABLE IF NOT EXISTS articles (
96- id BIGSERIAL PRIMARY KEY,
97- url TEXT NOT NULL UNIQUE,
98- source_name TEXT,
99- author TEXT,
100- title TEXT NOT NULL,
101- description TEXT,
102- published_at TIMESTAMP NOT NULL,
103- created_at TIMESTAMP NOT NULL DEFAULT NOW()
107+ CREATE TABLE IF NOT EXISTS articles (
108+ id BIGSERIAL PRIMARY KEY,
109+ url TEXT NOT NULL UNIQUE,
110+ source_name TEXT,
111+ author TEXT,
112+ title TEXT NOT NULL,
113+ description TEXT,
114+ published_at TIMESTAMP NOT NULL,
115+ created_at TIMESTAMP NOT NULL DEFAULT NOW()
104116 );
105117 """
106- with get_cursor (settings .db_articles ) as (conn , cur ):
118+ index = """
119+ CREATE INDEX IF NOT EXISTS idx_articles_published_at
120+ ON articles(published_at DESC)
121+ """
122+ with get_cursor (settings .db_news ) as (conn , cur ):
107123 cur .execute (query )
124+ cur .execute (index )
108125 conn .commit ()
109126
110127def create_user_news_table () -> None :
111128 query = """
112- CEATE TABLE IF NOT EXISTS user_news (
113- id BIGSERIAL PRIMARY KEY,
114- user_id BIGINT NOT NULL REFERENCES app_users(id) ON DELETE CASCADE,
115- search_request_id BIGINT NOT NULL REFERENCES search_requests(id) ON DELETE CASCADE,
116- article_id BIGINT NOT NULL REFERENCES articles(id) ON DELETE CASCADE,
117- keyword TEXT NOT NULL,
118- fetched_at TIMESTAMP NOT NULL DEFAULT NOW(),
119- UNIQUE (user_id, article_id, search_request_id)
129+ CREATE TABLE IF NOT EXISTS user_news (
130+ id BIGSERIAL PRIMARY KEY,
131+ user_id BIGINT NOT NULL REFERENCES app_users(id) ON DELETE CASCADE,
132+ search_request_id BIGINT NOT NULL REFERENCES search_requests(id) ON DELETE CASCADE,
133+ article_id BIGINT NOT NULL REFERENCES articles(id) ON DELETE CASCADE,
134+ keyword TEXT NOT NULL,
135+ fetched_at TIMESTAMP NOT NULL DEFAULT NOW(),
136+ UNIQUE (user_id, article_id, search_request_id)
120137 );
121138 """
122- with get_cursor (settings .db_user_news ) as (conn , cur ):
139+ index_list = ["""
140+ CREATE INDEX IF NOT EXISTS idx_user_news_user_id
141+ ON user_news(user_id)
142+ """ ,
143+ """
144+ CREATE INDEX IF NOT EXISTS idx_user_news_search_request_id
145+ ON user_news(search_request_id)
146+ """ ,
147+ """
148+ CREATE INDEX IF NOT EXISTS idx_user_news_article_id
149+ ON user_news(article_id)
150+ """ ]
151+ with get_cursor (settings .db_news ) as (conn , cur ):
123152 cur .execute (query )
153+ for index_query in index_list :
154+ cur .execute (index_query )
124155 conn .commit ()
125156
126157def create_request_stats_table () -> None :
127158 query = """
128- CEATE TABLE IF NOT EXISTS request_stats (
129- id BIGSERIAL PRIMARY KEY,
130- search_request_id BIGINT NOT NULL REFERENCES search_requests(id) ON DELETE CASCADE,
131- income_articles INTEGER NOT NULL DEFAULT 0,
132- accepted_articles INTEGER NOT NULL DEFAULT 0,
133- rejected_articles INTEGER NOT NULL DEFAULT 0,
134- reasons_counts JSONB NOUT NULL DEFAULT '{}'::jsonb,
135- prime_reasons JSONB NOUT NULL DEFAULT '{}'::jsonb,
136- created_at TIMESTAMP NOT NULL DEFAULT NOW()
159+ CREATE TABLE IF NOT EXISTS request_stats (
160+ id BIGSERIAL PRIMARY KEY,
161+ search_request_id BIGINT NOT NULL UNIQUE REFERENCES search_requests(id) ON DELETE CASCADE,
162+ income_articles INTEGER NOT NULL DEFAULT 0,
163+ accepted_articles INTEGER NOT NULL DEFAULT 0,
164+ rejected_articles INTEGER NOT NULL DEFAULT 0,
165+ reasons_counts JSONB NOT NULL DEFAULT '{}'::jsonb,
166+ prime_reasons JSONB NOT NULL DEFAULT '{}'::jsonb,
167+ created_at TIMESTAMP NOT NULL DEFAULT NOW()
137168 );
138169 """
139- with get_cursor (settings .db_request_stats ) as (conn , cur ):
170+ with get_cursor (settings .db_news ) as (conn , cur ):
140171 cur .execute (query )
141172 conn .commit ()
142173
143174def create_app_users_table () -> None :
144175 query = """
145- CEATE TABLE IF NOT EXISTS app_users (
146- id BIGSERIAL PRIMARY KEY,
147- google_sub TEXT NOT NULL UNIQOE ,
148- email TEXT NOT NULL UNIQUE,
149- name TEXT,
150- image_url TEXT,
151- created_at TIMESTAMP NOT NULL DEFAULT NOW(),
152- last_login_at TIMESTAMP NOT NULL DEFAULT NOW()
153- );
176+ CREATE TABLE IF NOT EXISTS app_users (
177+ id BIGSERIAL PRIMARY KEY,
178+ google_sub TEXT NOT NULL UNIQUE ,
179+ email TEXT NOT NULL UNIQUE,
180+ name TEXT,
181+ image_url TEXT,
182+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),
183+ last_login_at TIMESTAMP NOT NULL DEFAULT NOW()
184+ );
154185 """
155- with get_cursor (settings .db_app_users ) as (conn , cur ):
186+ with get_cursor (settings .db_news ) as (conn , cur ):
156187 cur .execute (query )
157188 conn .commit ()
158189
0 commit comments