recurrents

This commit is contained in:
xds
2025-11-17 15:02:47 +03:00
parent d0cae182b7
commit 12afd1f90e
48 changed files with 1479 additions and 120 deletions

View File

@@ -0,0 +1,21 @@
create table if not exists finance.bot_states
(
user_id integer primary key,
state_code varchar not null
);
create table if not exists finance.bot_states_data
(
id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
user_id integer not null,
data_code varchar(255) not null,
data_value varchar(255) not null,
CONSTRAINT pk_bot_states_data PRIMARY KEY (id)
);
ALTER TABLE finance.bot_states
ADD CONSTRAINT FK_STATE_ON_USER FOREIGN KEY (user_id) REFERENCES finance.users (id);
ALTER TABLE finance.bot_states
ADD CONSTRAINT FK_STATE_DATA_ON_USER FOREIGN KEY (user_id) REFERENCES finance.users (id);

View File

@@ -0,0 +1,3 @@
-- уникальное ограничение под ON CONFLICT (user_id, data_code)
ALTER TABLE finance.bot_states_data
ADD CONSTRAINT ux_bot_states_data_user_code UNIQUE (user_id, data_code);

View File

@@ -0,0 +1,3 @@
-- уникальное ограничение под ON CONFLICT (user_id, data_code)
ALTER TABLE finance.transactions
ALTER COLUMN category_id DROP NOT NULL;

View File

@@ -0,0 +1,19 @@
-- Очередь классификации категорий
CREATE TABLE IF NOT EXISTS finance.category_jobs (
id BIGSERIAL PRIMARY KEY,
tx_id INT NOT NULL UNIQUE, -- одна задача на транзакцию
status TEXT NOT NULL DEFAULT 'NEW', -- NEW | PROCESSING | DONE | FAILED
attempts INT NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
started_at TIMESTAMPTZ,
finished_at TIMESTAMPTZ,
last_error TEXT
);
-- Быстрые выборки по статусу
CREATE INDEX IF NOT EXISTS ix_category_jobs_status ON finance.category_jobs(status);
-- (опционально) защита от «зависших» задач
CREATE INDEX IF NOT EXISTS ix_category_jobs_processing_time
ON finance.category_jobs(started_at)
WHERE status = 'PROCESSING';

View File

@@ -0,0 +1,3 @@
alter table finance.transactions
add column tg_chat_id bigint null,
add column tg_message_id bigint null;

View File

@@ -0,0 +1,3 @@
alter table finance.category_jobs
add column tg_chat_id bigint null,
add column tg_message_id bigint null;

View File

@@ -0,0 +1,5 @@
ALTER TABLE finance.recurrent_operations
ADD CONSTRAINT recurrent_operations_pk PRIMARY KEY (id);
alter table finance.transactions
add column recurrent_id integer null,
ADD CONSTRAINT FK_RECURRENTS FOREIGN KEY (recurrent_id) REFERENCES finance.recurrent_operations (id);