Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1082x 741x 741x 580x 580x 580x 580x 580x 335x 741x 161x 741x 1082x 341x 341x 341x 1x 1x 1x 1x 1x 1x 1x 1x 1x 335x 288x 335x 335x 1x 1x 901x 820x 421x 421x 421x 740x 740x 1x 1x 809x 809x 809x 809x 809x 809x 809x 809x 809x 809x 809x 37x 37x 809x 15x 15x 809x 19x 19x 809x 738x 809x 69x 69x 809x 809x 809x 809x 176x 176x 809x 334x 334x 334x 334x 334x 333x 333x 331x 334x 334x 809x 18x 18x 809x 1x 1x 809x 55x 55x 809x 85x 85x 809x 809x 809x 809x 809x 477x 809x 738x 738x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 22x 22x 22x 8x | import {type Knex} from '@feasibleone/blong';
import {readFileSync, readdirSync} from 'node:fs';
import {basename, join} from 'node:path';
import {Type, type TSchema} from 'typebox';
import {type IColumnSchema} from './types.ts';
export {type IColumnSchema};
export function propType(prop: IColumnSchema): string {
if ('type' in prop && prop.type) {
switch (prop.type) {
case 'string':
if (prop.format === 'date-time' || prop.format === 'datetime') return 'datetime';
if (prop.format === 'date') return 'date';
if (prop.format === 'time') return 'time';
if (prop.format === 'uuid') return 'uuid';
if (prop.format === 'uid') return 'uid';
return 'string';
default:
return prop.type;
}
} else if ('anyOf' in prop && Array.isArray(prop.anyOf)) {
const found = prop.anyOf.find(p => p.type && p.type !== 'null');
if (found) return propType(found);
}
return 'unknown';
}
/**
* The branch of a nullable property that carries its options.
*
* `stringNull` is a union of `null` and the string, so the `maxLength` a caller wrote sits on the
* union's branch and not on the property — which is why every nullable string column was created
* `varchar(255)` whatever its declaration said (`@@F-673`).
*/
export function propBranch(prop: IColumnSchema): IColumnSchema {
if (prop.maxLength != null) return prop;
const anyOf = (prop as {anyOf?: IColumnSchema[]}).anyOf;
return anyOf?.find(branch => branch?.maxLength != null) ?? prop;
}
export function propDefault(prop: IColumnSchema): unknown {
if (prop.default !== undefined) return prop.default;
else if ('anyOf' in prop && Array.isArray(prop.anyOf)) {
const found = prop.anyOf.find(p => p.default !== undefined);
if (found) return propDefault(found);
}
return undefined;
}
export function addColumn(
table: Knex.CreateTableBuilder,
columnName: string,
prop: IColumnSchema,
isNullable: boolean,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): any {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let column: any;
const defaultValue = propDefault(prop);
switch (defaultValue) {
case 'auto-increment':
column = table.bigIncrements(columnName);
return column;
case 'ulid':
column = table.binary(columnName, 16).primary();
return column;
case 'uuid':
column = table.uuid(columnName, {useBinaryUuid: true, primaryKey: true}).primary();
return column;
}
switch (propType(prop)) {
case 'datetime':
column = table.dateTime(columnName);
break;
case 'date':
column = table.date(columnName);
break;
case 'time':
column = table.time(columnName);
break;
case 'uuid':
column = table.uuid(columnName, {useBinaryUuid: true});
break;
case 'uid':
column = table.binary(columnName, 16);
break;
case 'string': {
const declared = propBranch(prop);
// A MySQL TEXT holds 65 535 bytes, so a declared length above that is
// a request for LONGTEXT — what a data-URL screenshot needs (`@@D-661`).
if (declared.maxLength != null && declared.maxLength > 65535)
column = table.text(columnName, 'longtext');
else if (declared.maxLength != null && declared.maxLength > 255)
column = table.text(columnName);
else column = table.string(columnName, declared.maxLength ?? 255);
break;
}
case 'bigint':
column = table.bigInteger(columnName).unsigned();
break;
case 'number':
column = table.double(columnName);
break;
case 'integer':
column = table.integer(columnName);
break;
case 'boolean':
column = table.boolean(columnName);
break;
case 'array':
case 'object':
column = table.json(columnName);
break;
default:
column = table.text(columnName);
break;
}
if (isNullable) column.nullable();
else column.notNullable();
if (defaultValue !== undefined) column.defaultTo(defaultValue);
return column;
}
/**
* The width a declared string actually gets, in bytes.
*
* The mapping in `addColumn` is three steps — a `varchar` up to 255, `text` up to 65 535, then
* `longtext` — and a sync that only adds columns never climbs them. This is what a comparison needs:
* a declared 8000 against a stored `varchar(255)` is a widening the table does not have (`@@F-672`).
*/
export const declaredWidth = (prop: IColumnSchema): number => {
const maxLength = propBranch(prop).maxLength ?? 255;
if (maxLength > 65535) return 4294967295;
if (maxLength > 255) return 65535;
return maxLength;
};
export function sqlTypeToTypebox(sqlType: string): TSchema {
switch (sqlType.toUpperCase()) {
case 'VARCHAR':
case 'CHAR':
case 'TEXT':
case 'MEDIUMTEXT':
case 'LONGTEXT':
case 'TINYTEXT':
case 'ENUM':
case 'SET':
return Type.String();
case 'INT':
case 'INTEGER':
case 'BIGINT':
case 'SMALLINT':
case 'TINYINT':
case 'MEDIUMINT':
return Type.Integer();
case 'DECIMAL':
case 'FLOAT':
case 'DOUBLE':
case 'NUMERIC':
case 'REAL':
return Type.Number();
case 'BOOLEAN':
case 'BOOL':
case 'BIT':
return Type.Boolean();
case 'DATE':
return Type.String({format: 'date'});
case 'DATETIME':
case 'TIMESTAMP':
return Type.String({format: 'date-time'});
case 'TIME':
return Type.String({format: 'time'});
case 'JSON':
return Type.Unknown();
default:
return Type.String();
}
}
export function snakeToCamel(str: string): string {
return str.replace(/([-_]\w)/g, g => g[1].toUpperCase());
}
export function capitalize(str: string): string {
// `$`-aware: a leading `$` placeholder (e.g. `$object`) must not prevent
// the first letter being capitalised — `$object` → `$Object`.
return str.replace(/^(\$*)([a-z])/, (_m, pre: string, c: string) => pre + c.toUpperCase());
}
/** Normalise a handler name for registry look-up: remove dots, lowercase. */
export function methodId(name: string): string {
return name.replace(/\./g, '').toLowerCase();
}
/**
* Collapse whitespace and uppercase SQL for content comparison.
* Line comments are stripped before collapsing.
*/
export function normalizeSQL(sql: string): string {
return sql
.replace(/--[^\n]*/g, '')
.replace(/\s+/g, ' ')
.trim()
.toUpperCase();
}
/**
* Extract the procedure body (BEGIN … END block) from a full CREATE PROCEDURE
* statement so it can be compared against MySQL's `ROUTINE_DEFINITION` column,
* which stores only the body. Returns the full SQL if no BEGIN is found.
*/
export function extractProcedureBody(sql: string): string {
const upper = sql.toUpperCase();
const beginIdx = upper.indexOf('BEGIN');
if (beginIdx === -1) return sql;
const endIdx = upper.lastIndexOf('END');
if (endIdx === -1) return sql;
return sql.slice(beginIdx, endIdx + 3);
}
/**
* Scan a directory for `.sql` files and return their base names and contents.
*/
export function readSqlFiles(dir: string): Array<{name: string; sql: string}> {
const files = readdirSync(dir).filter(f => f.endsWith('.sql'));
return files.map(f => ({
name: basename(f, '.sql'),
sql: readFileSync(join(dir, f), 'utf8'),
}));
}
|