|
| 1 | +import { existsSync, readFileSync, rmSync, unlinkSync, writeFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import type { Column, MySqlSchemaV4, MySqlSchemaV5, Table } from '../../legacy/mysql-v5/mysqlSchema'; |
| 4 | +import type { Journal } from '../../utils'; |
| 5 | + |
| 6 | +export const upMysqlHandler = (out: string) => { |
| 7 | + // if there is meta folder - and there is a journal - it's version <8 |
| 8 | + const metaPath = join(out, 'meta'); |
| 9 | + const journalPath = join(metaPath, '_journal.json'); |
| 10 | + if (existsSync(metaPath) && existsSync(journalPath)) { |
| 11 | + const journal: Journal = JSON.parse(readFileSync(journalPath).toString()); |
| 12 | + if (Number(journal.version) < 8) { |
| 13 | + for (const entry of journal.entries) { |
| 14 | + const snapshotPrefix = entry.tag.split('_')[0]; |
| 15 | + const oldSnapshot = readFileSync(join(metaPath, `${snapshotPrefix}_snapshot.json`)); |
| 16 | + const oldSql = readFileSync(join(out, `${entry.tag}.sql`)); |
| 17 | + |
| 18 | + writeFileSync(join(out, `${entry.tag}/snapshot.json`), oldSnapshot); |
| 19 | + writeFileSync(join(out, `${entry.tag}/migration.sql`), oldSql); |
| 20 | + |
| 21 | + unlinkSync(join(out, `${entry.tag}.sql`)); |
| 22 | + } |
| 23 | + |
| 24 | + rmSync(metaPath); |
| 25 | + } |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +export const upMySqlHandlerV4toV5 = (obj: MySqlSchemaV4): MySqlSchemaV5 => { |
| 30 | + const mappedTables: Record<string, Table> = {}; |
| 31 | + |
| 32 | + for (const [key, table] of Object.entries(obj.tables)) { |
| 33 | + const mappedColumns: Record<string, Column> = {}; |
| 34 | + for (const [ckey, column] of Object.entries(table.columns)) { |
| 35 | + let newDefault: any = column.default; |
| 36 | + let newType: string = column.type; |
| 37 | + let newAutoIncrement: boolean | undefined = column.autoincrement; |
| 38 | + |
| 39 | + if (column.type.toLowerCase().startsWith('datetime')) { |
| 40 | + if (typeof column.default !== 'undefined') { |
| 41 | + if (column.default.startsWith("'") && column.default.endsWith("'")) { |
| 42 | + newDefault = `'${ |
| 43 | + column.default |
| 44 | + .substring(1, column.default.length - 1) |
| 45 | + .replace('T', ' ') |
| 46 | + .slice(0, 23) |
| 47 | + }'`; |
| 48 | + } else { |
| 49 | + newDefault = column.default.replace('T', ' ').slice(0, 23); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + newType = column.type.toLowerCase().replace('datetime (', 'datetime('); |
| 54 | + } else if (column.type.toLowerCase() === 'date') { |
| 55 | + if (typeof column.default !== 'undefined') { |
| 56 | + if (column.default.startsWith("'") && column.default.endsWith("'")) { |
| 57 | + newDefault = `'${ |
| 58 | + column.default |
| 59 | + .substring(1, column.default.length - 1) |
| 60 | + .split('T')[0] |
| 61 | + }'`; |
| 62 | + } else { |
| 63 | + newDefault = column.default.split('T')[0]; |
| 64 | + } |
| 65 | + } |
| 66 | + newType = column.type.toLowerCase().replace('date (', 'date('); |
| 67 | + } else if (column.type.toLowerCase().startsWith('timestamp')) { |
| 68 | + if (typeof column.default !== 'undefined') { |
| 69 | + if (column.default.startsWith("'") && column.default.endsWith("'")) { |
| 70 | + newDefault = `'${ |
| 71 | + column.default |
| 72 | + .substring(1, column.default.length - 1) |
| 73 | + .replace('T', ' ') |
| 74 | + .slice(0, 23) |
| 75 | + }'`; |
| 76 | + } else { |
| 77 | + newDefault = column.default.replace('T', ' ').slice(0, 23); |
| 78 | + } |
| 79 | + } |
| 80 | + newType = column.type |
| 81 | + .toLowerCase() |
| 82 | + .replace('timestamp (', 'timestamp('); |
| 83 | + } else if (column.type.toLowerCase().startsWith('time')) { |
| 84 | + newType = column.type.toLowerCase().replace('time (', 'time('); |
| 85 | + } else if (column.type.toLowerCase().startsWith('decimal')) { |
| 86 | + newType = column.type.toLowerCase().replace(', ', ','); |
| 87 | + } else if (column.type.toLowerCase().startsWith('enum')) { |
| 88 | + newType = column.type.toLowerCase(); |
| 89 | + } else if (column.type.toLowerCase().startsWith('serial')) { |
| 90 | + newAutoIncrement = true; |
| 91 | + } |
| 92 | + mappedColumns[ckey] = { |
| 93 | + ...column, |
| 94 | + default: newDefault, |
| 95 | + type: newType, |
| 96 | + autoincrement: newAutoIncrement, |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + mappedTables[key] = { |
| 101 | + ...table, |
| 102 | + columns: mappedColumns, |
| 103 | + compositePrimaryKeys: {}, |
| 104 | + uniqueConstraints: {}, |
| 105 | + checkConstraint: {}, |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + return { |
| 110 | + version: '5', |
| 111 | + dialect: obj.dialect, |
| 112 | + id: obj.id, |
| 113 | + prevId: obj.prevId, |
| 114 | + tables: mappedTables, |
| 115 | + schemas: obj.schemas, |
| 116 | + _meta: { |
| 117 | + schemas: {} as Record<string, string>, |
| 118 | + tables: {} as Record<string, string>, |
| 119 | + columns: {} as Record<string, string>, |
| 120 | + }, |
| 121 | + }; |
| 122 | +}; |
0 commit comments