fix: export ddl index order (#1028)

* fix: export ddl index order

* fix
This commit is contained in:
Guy Ben-Aharon
2025-12-21 13:16:27 +02:00
committed by GitHub
parent 0af777584f
commit 2d666bab0f
8 changed files with 26 additions and 11 deletions

View File

@@ -228,7 +228,8 @@ export function exportMSSQL({
? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName}\nON ${tableName} (${indexFields.join(', ')});`
: '';
})
.filter(Boolean);
.filter(Boolean)
.sort((a, b) => a.localeCompare(b)); // Sort for consistent output
return validIndexes.length > 0
? `\n-- Indexes\n${validIndexes.join('\n')}`

View File

@@ -418,7 +418,8 @@ export function exportMySQL({
? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName} (${indexFieldsWithPrefix.join(', ')});`
: '';
})
.filter(Boolean);
.filter(Boolean)
.sort((a, b) => a.localeCompare(b)); // Sort for consistent output
return validIndexes.length > 0
? `\n-- Indexes\n${validIndexes.join('\n')}`

View File

@@ -421,7 +421,8 @@ export function exportPostgreSQL({
? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName}${index.type && index.type !== 'btree' ? ` USING ${index.type.toUpperCase()}` : ''} (${indexFieldNames.join(', ')});`
: '';
})
.filter(Boolean);
.filter(Boolean)
.sort((a, b) => a.localeCompare(b)); // Sort for consistent output
return validIndexes.length > 0
? `\n-- Indexes\n${validIndexes.join('\n')}`

View File

@@ -444,7 +444,8 @@ export function exportSQLite({
? `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX IF NOT EXISTS "${safeIndexName}"\nON ${tableName} (${indexFieldNames.join(', ')});`
: '';
})
.filter(Boolean);
.filter(Boolean)
.sort((a, b) => a.localeCompare(b)); // Sort for consistent output
return validIndexes.length > 0
? `\n-- Indexes\n${validIndexes.join('\n')}`

View File

@@ -540,7 +540,9 @@ export const exportBaseSQL = ({
}
});
// Generate SQL for indexes
// Generate SQL for indexes - collect, then sort by the full CREATE INDEX statement
const indexStatements: string[] = [];
table.indexes.forEach((index) => {
// Skip the primary key index (it's already handled as a constraint)
if (index.isPrimaryKey) {
@@ -582,9 +584,18 @@ export const exportBaseSQL = ({
const indexName = needsQuoting
? `"${rawIndexName}"`
: rawIndexName;
sqlScript += `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName} (${fieldNames});\n`;
indexStatements.push(
`CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${indexName} ON ${tableName} (${fieldNames});`
);
}
});
// Sort index statements alphabetically for consistent, deterministic output
indexStatements.sort((a, b) => a.localeCompare(b));
sqlScript += indexStatements.join('\n');
if (indexStatements.length > 0) {
sqlScript += '\n';
}
});
if (nonViewTables.length > 0 && (relationships?.length ?? 0) > 0) {

View File

@@ -2973,9 +2973,9 @@ Table "data"."Products_20" {
"field_32" text
Indexes {
geom [name: "idx_Products_20_3"]
updated_at [unique, name: "idx_Products_20_1"]
updated_at [unique, name: "idx_Products_20_2"]
geom [name: "idx_Products_20_3"]
}
}
@@ -6449,8 +6449,8 @@ Table "admin"."Transactions_2" {
"amount" nvarchar(500)
Indexes {
updated_at [unique, name: "idx_Transactions_2_1"]
geom [name: "idx_Transactions_2_2"]
updated_at [unique, name: "idx_Transactions_2_1"]
}
}
@@ -6802,8 +6802,8 @@ Table "admin"."Transactions_14" {
"field_65" date
Indexes {
updated_at [unique, name: "idx_Transactions_14_1"]
geom [name: "idx_Transactions_14_2"]
updated_at [unique, name: "idx_Transactions_14_1"]
}
}

View File

@@ -12,8 +12,8 @@ Table "pokemon"."pokemon_abilities" {
Indexes {
(pok_id, slot) [pk]
is_hidden [name: "pokemon_abilities_pokemon_ix_pokemon_abilities_is_hidden"]
abil_id [name: "pokemon_abilities_pokemon_abil_id"]
is_hidden [name: "pokemon_abilities_pokemon_ix_pokemon_abilities_is_hidden"]
}
}

View File

@@ -12,8 +12,8 @@ Table "pokemon"."pokemon_abilities" {
Indexes {
(pok_id, slot) [pk]
is_hidden [name: "pokemon_abilities_pokemon_ix_pokemon_abilities_is_hidden"]
abil_id [name: "pokemon_abilities_pokemon_abil_id"]
is_hidden [name: "pokemon_abilities_pokemon_ix_pokemon_abilities_is_hidden"]
}
}