|
| 1 | +import { |
| 2 | + ClusterSubmissionV2, |
| 3 | + ClusterV2, |
| 4 | + ClusterV3, |
| 5 | + GeneratorV1, |
| 6 | + ProblemV4, |
| 7 | + ProblemV5, |
| 8 | + TestcaseSubmissionV3, |
| 9 | + TestcaseV3, |
| 10 | + TestcaseV4, |
| 11 | +} from "@kontestis/models"; |
| 12 | +import { Migration } from "scyllo"; |
| 13 | + |
| 14 | +import { Globals } from "../../globals"; |
| 15 | +import { Logger } from "../../lib/logger"; |
| 16 | +import { generateSnowflake } from "../../lib/snowflake"; |
| 17 | +import { initS3, S3Client } from "../../s3/S3"; |
| 18 | +import { readBucketStream } from "../../utils/stream"; |
| 19 | +import { streamQuery } from "../streamQuery"; |
| 20 | + |
| 21 | +type MigrationType = { |
| 22 | + generators: GeneratorV1; |
| 23 | + testcases: TestcaseV3 & TestcaseV4; |
| 24 | + clusters: ClusterV2 & ClusterV3; |
| 25 | + problems: ProblemV5; |
| 26 | + testcase_submissions: TestcaseSubmissionV3; |
| 27 | + cluster_submissions: ClusterSubmissionV2; |
| 28 | +}; |
| 29 | + |
| 30 | +// eslint-disable-next-line sonarjs/cognitive-complexity |
| 31 | +export const migration_improve_generators: Migration<MigrationType> = async (database, log) => { |
| 32 | + await database.createTable( |
| 33 | + "generators", |
| 34 | + true, |
| 35 | + { |
| 36 | + id: { |
| 37 | + type: "bigint", |
| 38 | + }, |
| 39 | + user_id: { |
| 40 | + type: "bigint", |
| 41 | + }, |
| 42 | + contest_id: { |
| 43 | + type: "bigint", |
| 44 | + }, |
| 45 | + organisation_id: { |
| 46 | + type: "bigint", |
| 47 | + }, |
| 48 | + problem_id: { |
| 49 | + type: "bigint", |
| 50 | + }, |
| 51 | + code: { |
| 52 | + type: "text", |
| 53 | + }, |
| 54 | + name: { |
| 55 | + type: "text", |
| 56 | + }, |
| 57 | + language: { |
| 58 | + type: "text", |
| 59 | + }, |
| 60 | + }, |
| 61 | + "id" |
| 62 | + ); |
| 63 | + |
| 64 | + await database.createIndex("generators", "generators_by_user_id", "user_id"); |
| 65 | + await database.createIndex("generators", "generators_by_problem_id", "problem_id"); |
| 66 | + await database.createIndex("generators", "generators_by_contest_id", "contest_id"); |
| 67 | + await database.createIndex("generators", "generators_by_organisation_id", "organisation_id"); |
| 68 | + |
| 69 | + await database.raw("ALTER TABLE testcases ADD input_type text"); |
| 70 | + await database.raw("ALTER TABLE testcases ADD output_type text"); |
| 71 | + await database.raw("ALTER TABLE testcases ADD status text"); |
| 72 | + await database.raw("ALTER TABLE testcases ADD input_file text"); |
| 73 | + await database.raw("ALTER TABLE testcases ADD output_file text"); |
| 74 | + await database.raw("ALTER TABLE testcases ADD error text"); |
| 75 | + await database.raw("ALTER TABLE testcases ADD generator_input text"); |
| 76 | + await database.raw("ALTER TABLE testcases ADD generator_id bigint"); |
| 77 | + |
| 78 | + await database.raw("ALTER TABLE clusters ADD order_number bigint"); |
| 79 | + await database.raw("ALTER TABLE clusters ADD status text"); |
| 80 | + await database.raw("ALTER TABLE clusters ADD error text"); |
| 81 | + |
| 82 | + await database.raw("ALTER TABLE testcase_submissions ADD input_file text"); |
| 83 | + await database.raw("ALTER TABLE testcase_submissions ADD output_file text"); |
| 84 | + await database.raw("ALTER TABLE testcase_submissions ADD submission_output_file text"); |
| 85 | + |
| 86 | + const testcases = await streamQuery<TestcaseV3>(database.client, "SELECT * FROM testcases"); |
| 87 | + |
| 88 | + const clusters = await streamQuery<ClusterV2>(database.client, "SELECT * FROM clusters"); |
| 89 | + |
| 90 | + const problems = await streamQuery<Pick<ProblemV4, "id" | "title">>( |
| 91 | + database.client, |
| 92 | + "SELECT id, title FROM problems" |
| 93 | + ); |
| 94 | + |
| 95 | + const clustersById: Record<string, ClusterV2> = {}; |
| 96 | + |
| 97 | + for (const cluster of clusters) { |
| 98 | + clustersById[cluster.id.toString()] = cluster; |
| 99 | + } |
| 100 | + |
| 101 | + const problemsById: Record<string, Pick<ProblemV5, "title" | "id">> = {}; |
| 102 | + |
| 103 | + for (const problem of problems) { |
| 104 | + problemsById[problem.id.toString()] = problem; |
| 105 | + } |
| 106 | + |
| 107 | + await initS3(); |
| 108 | + |
| 109 | + for (const testcase of testcases) { |
| 110 | + Logger.info("Migrating testcase " + testcase.id); |
| 111 | + const cluster = clustersById[testcase.cluster_id.toString()]; |
| 112 | + |
| 113 | + if (!cluster) { |
| 114 | + Logger.error("Cluster not found for testcase " + testcase.id); |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + const problem = problemsById[cluster.problem_id.toString()]; |
| 119 | + |
| 120 | + if (!problem) { |
| 121 | + Logger.error("Problem not found for testcase " + testcase.id); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + const inputFilePath = `${problem.title}-${problem.id}/${ |
| 126 | + testcase.id |
| 127 | + }-${generateSnowflake()}.in`; |
| 128 | + |
| 129 | + await S3Client.putObject(Globals.s3.buckets.testcases, inputFilePath, testcase.input); |
| 130 | + |
| 131 | + await database.update( |
| 132 | + "testcases", |
| 133 | + { |
| 134 | + input_type: "manual", |
| 135 | + output_type: "auto", |
| 136 | + status: "not-ready", |
| 137 | + input_file: inputFilePath, |
| 138 | + }, |
| 139 | + { id: testcase.id } |
| 140 | + ); |
| 141 | + |
| 142 | + Logger.info("Migrated testcase " + testcase.id); |
| 143 | + } |
| 144 | + |
| 145 | + const clustersByProblemId: Record<string, ClusterV2[]> = {}; |
| 146 | + |
| 147 | + for (const cluster of clusters) { |
| 148 | + if (!clustersByProblemId[cluster.problem_id.toString()]) { |
| 149 | + clustersByProblemId[cluster.problem_id.toString()] = []; |
| 150 | + } |
| 151 | + |
| 152 | + clustersByProblemId[cluster.problem_id.toString()].push(cluster); |
| 153 | + } |
| 154 | + |
| 155 | + for (const clusters of Object.values(clustersByProblemId)) { |
| 156 | + clusters.sort((a, b) => Number(a.id - b.id)); |
| 157 | + |
| 158 | + for (const [index, cluster] of clusters.entries()) { |
| 159 | + await database.update("clusters", { order_number: BigInt(index) }, { id: cluster.id }); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + for (const cluster of clusters) { |
| 164 | + // Migrate to new generator format |
| 165 | + if (!cluster.generator) { |
| 166 | + await database.update("clusters", { status: "not-ready" }, { id: cluster.id }); |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + if (!cluster.generator_language || !cluster.generator_code) { |
| 171 | + Logger.error("Generator language or code not found for cluster " + cluster.id); |
| 172 | + await database.update( |
| 173 | + "clusters", |
| 174 | + { |
| 175 | + status: "generator-error", |
| 176 | + error: "Invalid generator state, no language and/or code (migration)", |
| 177 | + }, |
| 178 | + { id: cluster.id } |
| 179 | + ); |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + const generator: GeneratorV1 = { |
| 184 | + id: generateSnowflake(), |
| 185 | + problem_id: cluster.problem_id, |
| 186 | + name: `${problemsById[cluster.problem_id.toString()].title}-${cluster.id}`, |
| 187 | + code: cluster.generator_code ?? "", |
| 188 | + language: cluster.generator_language ?? "python", |
| 189 | + }; |
| 190 | + |
| 191 | + await database.insertInto("generators", generator); |
| 192 | + |
| 193 | + await database.update("clusters", { status: "not-ready" }, { id: cluster.id }); |
| 194 | + |
| 195 | + const batch = database.batch(); |
| 196 | + |
| 197 | + for (let index = 0; index < 10; index++) { |
| 198 | + const testcase: TestcaseV4 = { |
| 199 | + id: generateSnowflake(), |
| 200 | + generator_id: generator.id, |
| 201 | + status: "not-ready", |
| 202 | + cluster_id: cluster.id, |
| 203 | + output_type: "auto", |
| 204 | + input_type: "generator", |
| 205 | + generator_input: index.toString(), |
| 206 | + }; |
| 207 | + |
| 208 | + batch.insertInto("testcases", testcase); |
| 209 | + } |
| 210 | + await batch.execute(); |
| 211 | + } |
| 212 | + |
| 213 | + const testcaseSubmissions = await streamQuery<TestcaseSubmissionV3>( |
| 214 | + database.client, |
| 215 | + "SELECT * FROM testcase_submissions" |
| 216 | + ); |
| 217 | + |
| 218 | + const clusterSubmissionsById: Record<string, ClusterSubmissionV2> = {}; |
| 219 | + const clusterSubmissions = await streamQuery<ClusterSubmissionV2>( |
| 220 | + database.client, |
| 221 | + "SELECT * FROM cluster_submissions" |
| 222 | + ); |
| 223 | + |
| 224 | + for (const clusterSubmission of clusterSubmissions) { |
| 225 | + clusterSubmissionsById[clusterSubmission.id.toString()] = clusterSubmission; |
| 226 | + } |
| 227 | + |
| 228 | + for (const testcaseSubmission of testcaseSubmissions) { |
| 229 | + const clusterSubmission = |
| 230 | + clusterSubmissionsById[testcaseSubmission.cluster_submission_id.toString()]; |
| 231 | + |
| 232 | + if (!clusterSubmission) { |
| 233 | + Logger.error( |
| 234 | + "Cluster submission not found for testcase submission " + testcaseSubmission.id |
| 235 | + ); |
| 236 | + continue; |
| 237 | + } |
| 238 | + |
| 239 | + if (clusterSubmission.submission_id < 0n) { |
| 240 | + continue; |
| 241 | + } |
| 242 | + |
| 243 | + const prefix = `${clusterSubmission.submission_id}/${clusterSubmission.cluster_id}/`; |
| 244 | + |
| 245 | + const files = await readBucketStream( |
| 246 | + S3Client.listObjects(Globals.s3.buckets.submission_meta, prefix, true) |
| 247 | + ).catch((error) => { |
| 248 | + Logger.error( |
| 249 | + `Failed to read bucket stream for testcase submission: ${testcaseSubmission.id}`, |
| 250 | + error |
| 251 | + ); |
| 252 | + }); |
| 253 | + |
| 254 | + if (!files) continue; |
| 255 | + |
| 256 | + const fileNames = new Set<string>(files.map((it) => it.name).filter(Boolean) as string[]); |
| 257 | + |
| 258 | + if (fileNames.size === 0) continue; |
| 259 | + |
| 260 | + await database.update( |
| 261 | + "testcase_submissions", |
| 262 | + { |
| 263 | + input_file: fileNames.has(`${testcaseSubmission.testcase_id}.in`) |
| 264 | + ? `${prefix}${testcaseSubmission.testcase_id}.in` |
| 265 | + : undefined, |
| 266 | + output_file: fileNames.has(`${testcaseSubmission.testcase_id}.out`) |
| 267 | + ? `${prefix}${testcaseSubmission.testcase_id}.out` |
| 268 | + : undefined, |
| 269 | + submission_output_file: fileNames.has(`${testcaseSubmission.testcase_id}.sout`) |
| 270 | + ? `${prefix}${testcaseSubmission.testcase_id}.sout` |
| 271 | + : undefined, |
| 272 | + }, |
| 273 | + { id: testcaseSubmission.id } |
| 274 | + ); |
| 275 | + } |
| 276 | + |
| 277 | + await database.raw("ALTER TABLE testcases DROP input"); |
| 278 | + |
| 279 | + await database.raw("ALTER TABLE clusters DROP generator"); |
| 280 | + await database.raw("ALTER TABLE clusters DROP generator_language"); |
| 281 | + await database.raw("ALTER TABLE clusters DROP generator_code"); |
| 282 | + |
| 283 | + log("Done"); |
| 284 | +}; |
0 commit comments