|
| 1 | +import { createTableRaw, ScylloClient } from 'scyllo'; |
| 2 | + |
| 3 | +import { DeploymentV4, DeploymentV5 } from '../../types/Deployment.type'; |
| 4 | +import { log } from '../../util/logging'; |
| 5 | +import { Migration } from '../migrations'; |
| 6 | + |
| 7 | +const createTable = async ( |
| 8 | + database: ScylloClient<{ |
| 9 | + deployments: DeploymentV5; |
| 10 | + }>, |
| 11 | + table_name: string |
| 12 | +) => { |
| 13 | + const query = createTableRaw<{ deployments: DeploymentV5 }, 'deployments'>( |
| 14 | + 'signal', |
| 15 | + table_name as 'deployments', |
| 16 | + true, |
| 17 | + { |
| 18 | + app_id: { |
| 19 | + type: 'bigint', |
| 20 | + }, |
| 21 | + deploy_id: { |
| 22 | + type: 'bigint', |
| 23 | + }, |
| 24 | + cid: { |
| 25 | + type: 'text', |
| 26 | + }, |
| 27 | + context: { |
| 28 | + type: 'text', |
| 29 | + }, |
| 30 | + sid: { |
| 31 | + type: 'text', |
| 32 | + }, |
| 33 | + }, |
| 34 | + 'app_id', |
| 35 | + ['deploy_id'] |
| 36 | + ); |
| 37 | + |
| 38 | + log.debug(query.query); |
| 39 | + |
| 40 | + await database.query({ |
| 41 | + args: query.args, |
| 42 | + query: query.query, |
| 43 | + }); |
| 44 | + |
| 45 | + // await database.createIndex( |
| 46 | + // table_name as 'deployments', |
| 47 | + // table_name + '_by_app_id', |
| 48 | + // 'app_id' |
| 49 | + // ); |
| 50 | +}; |
| 51 | + |
| 52 | +export const rebuild_deployments: Migration<{ |
| 53 | + deployments: DeploymentV4; |
| 54 | + deployments_tmp: DeploymentV5; |
| 55 | +}> = async (database) => { |
| 56 | + log.debug('creating table'); |
| 57 | + await createTable(database, 'deployments_tmp'); |
| 58 | + |
| 59 | + log.debug('getting ol'); |
| 60 | + const deployments_from_old = await database.selectFrom('deployments', [ |
| 61 | + 'app_id', |
| 62 | + 'deploy_id', |
| 63 | + ]); |
| 64 | + |
| 65 | + for (const deployment of deployments_from_old) { |
| 66 | + const deploymentData = await database.selectOneFrom( |
| 67 | + 'deployments', |
| 68 | + '*', |
| 69 | + { app_id: deployment.app_id, deploy_id: deployment.deploy_id } |
| 70 | + ); |
| 71 | + |
| 72 | + await database.insertInto('deployments_tmp', { |
| 73 | + app_id: deploymentData?.app_id, |
| 74 | + cid: deploymentData?.cid, |
| 75 | + context: '', |
| 76 | + deploy_id: deploymentData?.deploy_id, |
| 77 | + sid: deploymentData?.sid, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + await database.dropTable('deployments'); |
| 82 | + |
| 83 | + await createTable(database, 'deployments'); |
| 84 | + |
| 85 | + const deployments_form_intermediary = await database.selectFrom( |
| 86 | + 'deployments_tmp', |
| 87 | + ['app_id', 'deploy_id'] |
| 88 | + ); |
| 89 | + |
| 90 | + for (const deployment of deployments_form_intermediary) { |
| 91 | + const deploymentData = await database.selectOneFrom( |
| 92 | + 'deployments_tmp', |
| 93 | + '*', |
| 94 | + { |
| 95 | + app_id: deployment.app_id, |
| 96 | + deploy_id: deployment.deploy_id, |
| 97 | + } |
| 98 | + ); |
| 99 | + |
| 100 | + await ( |
| 101 | + database as ScylloClient<{ deployments: DeploymentV5 }> |
| 102 | + ).insertInto('deployments', deploymentData as DeploymentV5); |
| 103 | + } |
| 104 | +}; |
0 commit comments