fix: make schema bootstrap mysql safe
This commit is contained in:
@@ -46,6 +46,84 @@ export function createDbPool() {
|
||||
});
|
||||
}
|
||||
|
||||
export function splitSqlStatements(sql) {
|
||||
const statements = [];
|
||||
let current = '';
|
||||
let quote = null;
|
||||
let lineComment = false;
|
||||
let blockComment = false;
|
||||
|
||||
for (let i = 0; i < sql.length; i += 1) {
|
||||
const char = sql[i];
|
||||
const next = sql[i + 1];
|
||||
|
||||
if (lineComment) {
|
||||
if (char === '\n') {
|
||||
lineComment = false;
|
||||
current += '\n';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (blockComment) {
|
||||
if (char === '*' && next === '/') {
|
||||
blockComment = false;
|
||||
i += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (quote) {
|
||||
current += char;
|
||||
if (char === '\\' && (quote === '\'' || quote === '"') && next) {
|
||||
current += next;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (char === quote) {
|
||||
if (next === quote && quote !== '`') {
|
||||
current += next;
|
||||
i += 1;
|
||||
} else {
|
||||
quote = null;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '-' && next === '-') {
|
||||
lineComment = true;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '/' && next === '*') {
|
||||
blockComment = true;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '\'' || char === '"' || char === '`') {
|
||||
quote = char;
|
||||
current += char;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === ';') {
|
||||
const trimmed = current.trim();
|
||||
if (trimmed) statements.push(trimmed);
|
||||
current = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
current += char;
|
||||
}
|
||||
|
||||
const trimmed = current.trim();
|
||||
if (trimmed) statements.push(trimmed);
|
||||
return statements;
|
||||
}
|
||||
|
||||
async function columnExists(pool, table, column) {
|
||||
const [rows] = await pool.query(
|
||||
`SELECT 1 FROM information_schema.COLUMNS
|
||||
@@ -589,11 +667,8 @@ export async function migrateSchema(pool) {
|
||||
export async function initSchema(pool) {
|
||||
const schemaPath = path.join(__dirname, 'schema.sql');
|
||||
const sql = fs.readFileSync(schemaPath, 'utf8');
|
||||
for (const statement of sql.split(';')) {
|
||||
const trimmed = statement.trim();
|
||||
if (trimmed) {
|
||||
await pool.query(trimmed);
|
||||
}
|
||||
for (const statement of splitSqlStatements(sql)) {
|
||||
await pool.query(statement);
|
||||
}
|
||||
await migrateSchema(pool);
|
||||
await ensureDefaultSpaces(pool, {
|
||||
|
||||
Reference in New Issue
Block a user