Changes between Version 6 and Version 7 of DmlScript


Ignore:
Timestamp:
05/20/26 09:57:40 (6 days ago)
Author:
231141
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DmlScript

    v6 v7  
    11051105
    11061106
     1107-- =========================================================
     1108-- HELPER TABLES
     1109-- =========================================================
     1110
     1111CREATE TABLE message_template_client (
     1112    category TEXT,
     1113    text TEXT
     1114);
     1115
     1116CREATE TABLE message_template_worker (
     1117    category TEXT,
     1118    text TEXT
     1119);
     1120
     1121--add category_id from Category
     1122ALTER TABLE message_template_client
     1123    ADD COLUMN category_id INT;
     1124
     1125UPDATE message_template_client mtc
     1126SET category_id = c.id
     1127FROM Category c
     1128WHERE c.category_name = mtc.category;
     1129
     1130
     1131ALTER TABLE message_template_worker
     1132    ADD COLUMN category_id INT;
     1133
     1134UPDATE message_template_worker mtc
     1135SET category_id = c.id
     1136FROM Category c
     1137WHERE c.category_name = mtc.category;
     1138
     1139
     1140
     1141
     1142
     1143-- =========================================================
     1144-- REQUIRED INDEXES
     1145-- =========================================================
     1146
     1147CREATE INDEX IF NOT EXISTS idx_task_created_at
     1148    ON Task(created_at);
     1149
     1150CREATE INDEX IF NOT EXISTS idx_template_worker_category_id
     1151    ON message_template_worker(category_id);
     1152
     1153CREATE INDEX IF NOT EXISTS idx_template_client_category_id
     1154    ON message_template_client(category_id);
     1155
     1156
     1157-- =========================================================
     1158-- GENERATE MESSAGES
     1159DO $$
     1160DECLARE
     1161    v_batch_start TIMESTAMP := TIMESTAMP '2017-01-01';
     1162    v_batch_end   TIMESTAMP;
     1163
     1164    v_finish      TIMESTAMP := TIMESTAMP '2026-04-30';
     1165
     1166    v_tables_ready BOOLEAN := FALSE;
     1167BEGIN
     1168
     1169    LOOP
     1170
     1171        v_batch_end := v_batch_start + INTERVAL '1 week';
     1172
     1173        EXIT WHEN v_batch_start >= v_finish;
     1174
     1175        RAISE NOTICE '=== BATCH % -> % ===',
     1176            v_batch_start,
     1177            v_batch_end;
     1178
     1179        -- =====================================================
     1180        -- 1. CREATE / RESET TEMP TABLES
     1181        -- =====================================================
     1182
     1183        IF NOT v_tables_ready THEN
     1184
     1185            CREATE TEMP TABLE tmp_batch_tasks (
     1186                task_id INT,
     1187                created_at TIMESTAMP,
     1188                updated_at TIMESTAMP,
     1189                worker_user_id INT,
     1190                client_user_id INT,
     1191                category_id INT
     1192            ) ON COMMIT PRESERVE ROWS;
     1193
     1194            CREATE INDEX idx_tmp_batch_task_id
     1195                ON tmp_batch_tasks(task_id);
     1196
     1197            CREATE TEMP TABLE tmp_messages_to_insert (
     1198                task_id INT,
     1199                sender_id INT,
     1200                created_at TIMESTAMP,
     1201                category_id INT
     1202            ) ON COMMIT PRESERVE ROWS;
     1203
     1204            CREATE INDEX idx_tmp_messages_task_id
     1205                ON tmp_messages_to_insert(task_id);
     1206
     1207            v_tables_ready := TRUE;
     1208
     1209        ELSE
     1210
     1211            TRUNCATE TABLE tmp_batch_tasks;
     1212            TRUNCATE TABLE tmp_messages_to_insert;
     1213
     1214        END IF;
     1215
     1216        -- =====================================================
     1217        -- 2. LOAD TASK DATA
     1218        -- =====================================================
     1219
     1220        INSERT INTO tmp_batch_tasks (
     1221            task_id,
     1222            created_at,
     1223            updated_at,
     1224            worker_user_id,
     1225            client_user_id,
     1226            category_id
     1227        )
     1228        SELECT
     1229            t.id,
     1230            t.created_at,
     1231            t.updated_at,
     1232            w.user_id,
     1233            c.user_id,
     1234            tr.category_id
     1235
     1236        FROM Task t
     1237
     1238        JOIN Offer o
     1239            ON o.id = t.offer_id
     1240
     1241        JOIN Worker w
     1242            ON w.id = o.worker_id
     1243
     1244        JOIN TaskRequest tr
     1245            ON tr.id = o.task_request_id
     1246
     1247        JOIN Client c
     1248            ON c.id = tr.client_id
     1249
     1250        WHERE t.created_at >= v_batch_start
     1251          AND t.created_at <  v_batch_end;
     1252
     1253        RAISE NOTICE 'Tasks loaded: %',
     1254            (SELECT COUNT(*) FROM tmp_batch_tasks);
     1255
     1256        -- =====================================================
     1257        -- 3. GENERATE MESSAGE METADATA
     1258        -- =====================================================
     1259
     1260        INSERT INTO tmp_messages_to_insert (
     1261            task_id,
     1262            sender_id,
     1263            created_at,
     1264            category_id
     1265        )
     1266        SELECT
     1267            bt.task_id,
     1268
     1269            CASE
     1270                WHEN random() < 0.5
     1271                    THEN bt.client_user_id
     1272                ELSE bt.worker_user_id
     1273            END AS sender_id,
     1274
     1275            bt.created_at
     1276            + (
     1277                random() *
     1278                (bt.updated_at - bt.created_at)
     1279            ) AS created_at,
     1280
     1281            bt.category_id
     1282
     1283        FROM tmp_batch_tasks bt
     1284
     1285        JOIN LATERAL generate_series(
     1286            1,
     1287            floor(random() * 4)::int + 3
     1288        ) gs ON true;
     1289
     1290        RAISE NOTICE 'Messages generated: %',
     1291            (SELECT COUNT(*) FROM tmp_messages_to_insert);
     1292
     1293        -- =====================================================
     1294        -- 4. INSERT INTO MESSAGE
     1295        -- =====================================================
     1296
     1297        INSERT INTO Message (
     1298            text,
     1299            created_at,
     1300            task_id,
     1301            sender_id
     1302        )
     1303        SELECT
     1304            COALESCE(
     1305                CASE
     1306                    WHEN tmi.sender_id = bt.worker_user_id
     1307                        THEN mtw.text
     1308                    ELSE mtc.text
     1309                END,
     1310                'Default message'
     1311            ) AS text,
     1312
     1313            tmi.created_at,
     1314
     1315            tmi.task_id,
     1316
     1317            tmi.sender_id
     1318
     1319        FROM tmp_messages_to_insert tmi
     1320
     1321        JOIN tmp_batch_tasks bt
     1322            ON bt.task_id = tmi.task_id
     1323
     1324        LEFT JOIN LATERAL (
     1325    SELECT text
     1326    FROM message_template_worker mtw
     1327    WHERE mtw.category_id = tmi.category_id
     1328    OFFSET floor(
     1329        random() * (
     1330            SELECT count(*)
     1331            FROM message_template_worker x
     1332            WHERE x.category_id = tmi.category_id
     1333        )
     1334    )
     1335    LIMIT 1
     1336) mtw
     1337            ON tmi.sender_id = bt.worker_user_id
     1338
     1339        LEFT JOIN LATERAL (
     1340    SELECT text
     1341    FROM message_template_client mtc
     1342    WHERE mtc.category_id = tmi.category_id
     1343    OFFSET floor(
     1344        random() * (
     1345            SELECT count(*)
     1346            FROM message_template_client x
     1347            WHERE x.category_id = tmi.category_id
     1348        )
     1349    )
     1350    LIMIT 1
     1351) mtc
     1352            ON tmi.sender_id = bt.client_user_id;
     1353
     1354        RAISE NOTICE 'Inserted batch % -> %',
     1355            v_batch_start,
     1356            v_batch_end;
     1357
     1358        v_batch_start := v_batch_end;
     1359
     1360    END LOOP;
     1361
     1362    RAISE NOTICE 'ALL BATCHES FINISHED';
     1363
     1364END;
     1365$$;
     1366
     1367
     1368-- =========================================================
     1369-- CLEANUP
     1370-- =========================================================
     1371
     1372DROP TABLE IF EXISTS tmp_messages_to_insert;
     1373DROP TABLE IF EXISTS tmp_batch_tasks;
     1374
     1375DROP INDEX IF EXISTS idx_task_created_at;
     1376DROP INDEX IF EXISTS idx_template_worker_category_id;
     1377DROP INDEX IF EXISTS idx_template_client_category_id;
     1378
     1379
     1380
     1381-- =========================================================
     1382-- DROP THE HELPER TABLES
     1383-- =========================================================
     1384DROP TABLE IF EXISTS message_template_client ;
     1385DROP TABLE IF EXISTS message_template_worker;
     1386
     1387
     1388-- =========================================================
     1389-- DROP DUPLICATE MESSAGE PER TASK
     1390-- =========================================================
     1391DROP
     1392DELETE FROM Message m
     1393USING Message d
     1394WHERE m.id > d.id
     1395  AND m.task_id = d.task_id
     1396  AND m.text = d.text;
     1397
     1398
     1399
     1400
     1401
     1402
    11071403}}}