refactor: Improve database initialization script by separating CREATE TABLE and CREATE INDEX statements, enhancing error handling, and providing clearer logging
This commit is contained in:
@@ -102,32 +102,84 @@ async function initDatabase() {
|
|||||||
console.log(`Reading schema from ${schemaPath}...`);
|
console.log(`Reading schema from ${schemaPath}...`);
|
||||||
const schema = readFileSync(schemaPath, "utf-8");
|
const schema = readFileSync(schemaPath, "utf-8");
|
||||||
|
|
||||||
// Split by semicolons and execute each statement
|
// Split by semicolons and filter out comments and empty statements
|
||||||
const statements = schema
|
const allStatements = schema
|
||||||
.split(";")
|
.split(";")
|
||||||
.map((s) => s.trim())
|
.map((s) => s.trim())
|
||||||
.filter((s) => s.length > 0 && !s.startsWith("--"));
|
.filter((s) => s.length > 0 && !s.startsWith("--"));
|
||||||
|
|
||||||
console.log(`Executing ${statements.length} SQL statements...`);
|
// Separate CREATE TABLE and CREATE INDEX statements
|
||||||
|
const tableStatements: string[] = [];
|
||||||
|
const indexStatements: string[] = [];
|
||||||
|
|
||||||
for (const statement of statements) {
|
for (const statement of allStatements) {
|
||||||
|
const upperStatement = statement.toUpperCase().trim();
|
||||||
|
if (upperStatement.startsWith("CREATE TABLE")) {
|
||||||
|
tableStatements.push(statement);
|
||||||
|
} else if (upperStatement.startsWith("CREATE INDEX")) {
|
||||||
|
indexStatements.push(statement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`Found ${tableStatements.length} table(s) and ${indexStatements.length} index(es) to create`
|
||||||
|
);
|
||||||
|
|
||||||
|
// First, create all tables
|
||||||
|
console.log("\nCreating tables...");
|
||||||
|
for (const statement of tableStatements) {
|
||||||
if (statement) {
|
if (statement) {
|
||||||
try {
|
try {
|
||||||
// Use postgres.unsafe() to execute raw SQL
|
await (sql as any).unsafe(statement + ";");
|
||||||
await (sql as any).unsafe(statement);
|
const tableName =
|
||||||
console.log(`✓ Executed: ${statement.substring(0, 50)}...`);
|
statement.match(
|
||||||
|
/CREATE TABLE\s+(?:IF NOT EXISTS\s+)?(\w+)/i
|
||||||
|
)?.[1] || "unknown";
|
||||||
|
console.log(`✓ Created table: ${tableName}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Check if it's a "already exists" error (which is OK)
|
|
||||||
const errorMessage = (error as Error).message;
|
const errorMessage = (error as Error).message;
|
||||||
if (
|
if (
|
||||||
errorMessage.includes("already exists") ||
|
errorMessage.includes("already exists") ||
|
||||||
errorMessage.includes("duplicate")
|
errorMessage.includes("duplicate")
|
||||||
) {
|
) {
|
||||||
console.log(
|
const tableName =
|
||||||
`⚠ Skipped (already exists): ${statement.substring(0, 50)}...`
|
statement.match(
|
||||||
);
|
/CREATE TABLE\s+(?:IF NOT EXISTS\s+)?(\w+)/i
|
||||||
|
)?.[1] || "unknown";
|
||||||
|
console.log(`⚠ Table already exists: ${tableName}`);
|
||||||
} else {
|
} else {
|
||||||
console.error(`✗ Error executing statement: ${errorMessage}`);
|
console.error(`✗ Error creating table: ${errorMessage}`);
|
||||||
|
console.error(` Statement: ${statement.substring(0, 100)}...`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then, create all indexes
|
||||||
|
console.log("\nCreating indexes...");
|
||||||
|
for (const statement of indexStatements) {
|
||||||
|
if (statement) {
|
||||||
|
try {
|
||||||
|
await (sql as any).unsafe(statement + ";");
|
||||||
|
const indexName =
|
||||||
|
statement.match(
|
||||||
|
/CREATE INDEX\s+(?:IF NOT EXISTS\s+)?(\w+)/i
|
||||||
|
)?.[1] || "unknown";
|
||||||
|
console.log(`✓ Created index: ${indexName}`);
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = (error as Error).message;
|
||||||
|
if (
|
||||||
|
errorMessage.includes("already exists") ||
|
||||||
|
errorMessage.includes("duplicate")
|
||||||
|
) {
|
||||||
|
const indexName =
|
||||||
|
statement.match(
|
||||||
|
/CREATE INDEX\s+(?:IF NOT EXISTS\s+)?(\w+)/i
|
||||||
|
)?.[1] || "unknown";
|
||||||
|
console.log(`⚠ Index already exists: ${indexName}`);
|
||||||
|
} else {
|
||||||
|
console.error(`✗ Error creating index: ${errorMessage}`);
|
||||||
console.error(` Statement: ${statement.substring(0, 100)}...`);
|
console.error(` Statement: ${statement.substring(0, 100)}...`);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user