2 * DB migration from v01 to v02 (postgres)
7 CREATE TABLE tb_month_stats
9 pk_month_stats BIGSERIAL PRIMARY KEY,
10 fk_user INTEGER NOT NULL,
11 stats_date DATE NOT NULL,
14 last_activity_time TIMESTAMP NOT NULL,
15 last_cash_add dm_money,
16 last_cash_add_time TIMESTAMP NOT NULL,
17 passive_time INTEGER NOT NULL,
20 REFERENCES tb_users (pk_user)
22 UNIQUE (fk_user, stats_date)
25 CREATE TABLE tb_month_stats_traffic
27 pk_month_stat_traffic BIGSERIAL PRIMARY KEY,
28 fk_month_stats BIGINT NOT NULL,
29 dir_num SMALLINT NOT NULL,
30 download BIGINT NOT NULL,
31 upload BIGINT NOT NULL,
33 FOREIGN KEY (fk_month_stats)
34 REFERENCES tb_month_stats (pk_month_stats)
36 UNIQUE (fk_month_stats, dir_num)
39 INSERT INTO tb_month_stats
52 '1970-01-01 00:00:00'::TIMESTAMP WITHOUT TIME ZONE,
54 '1970-01-01 00:00:00'::TIMESTAMP WITHOUT TIME ZONE,
57 WHERE date_trunc('month', stats_date) < date_trunc('month', 'now'::DATE)
61 INSERT INTO tb_month_stats_traffic
66 SELECT s.pk_month_stats,
70 FROM tb_stats_traffic AS t
71 LEFT JOIN tb_month_stats AS s
72 ON s.fk_user = t.fk_user AND
73 s.stats_date = t.stats_date
74 WHERE date_trunc('month', t.stats_date) < date_trunc('month', 'now'::DATE);
76 DROP FUNCTION sp_add_stats_traffic ( dm_name, DATE, SMALLINT, BIGINT, BIGINT );
78 CREATE FUNCTION sp_add_stats_traffic (_login dm_name,
87 SELECT pk_user INTO _pk_user
91 IF _pk_user IS NULL THEN
92 RAISE EXCEPTION 'User % not found', _login;
96 UPDATE tb_stats_traffic SET
99 WHERE fk_user = _pk_user AND
103 INSERT INTO tb_stats_traffic
119 DELETE FROM tb_stats_traffic WHERE date_trunc('month', stats_date) < date_trunc('month', 'now'::DATE);
120 ALTER TABLE tb_stats_traffic DROP stats_date;
121 ALTER TABLE tb_stats_traffic ADD UNIQUE (fk_user, dir_num);
124 CREATE FUNCTION sp_add_month_stats (_login dm_name,
128 _last_activity_time TIMESTAMP,
129 _last_cash_add dm_money,
130 _last_cash_add_time TIMESTAMP,
131 _passive_time INTEGER)
136 _pk_month_stats BIGINT;
138 SELECT pk_user INTO _pk_user
142 IF _pk_user IS NULL THEN
143 RAISE EXCEPTION 'User % not found', _login;
147 INSERT INTO tb_month_stats
166 SELECT CURRVAL('tb_month_stats_pk_month_stats_seq') INTO _pk_month_stats;
168 RETURN _pk_month_stats;
172 CREATE FUNCTION sp_add_month_stats_traffic (_pk_month_stats BIGINT,
179 INSERT INTO tb_month_stats_traffic
194 UPDATE tb_info SET version = 7;