Updates
This commit is contained in:
Vendored
+61
-37
@@ -2,6 +2,7 @@ import isVectorField from "./is-vector-field";
|
||||
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
||||
import runSchemaQuery, { querySchemaRows } from "./run-schema-query";
|
||||
import schemaCondition from "./schema-condition";
|
||||
import { grabDesiredUniqueConstraints } from "./sync-unique-constraints";
|
||||
function isVectorIndexDef(index, table) {
|
||||
if (index.indexType === "VECTOR")
|
||||
return true;
|
||||
@@ -20,6 +21,60 @@ function vectorDistanceMetric(index) {
|
||||
return "euclidean";
|
||||
return "euclidean";
|
||||
}
|
||||
/** Normalize schema index type vs information_schema.INDEX_TYPE */
|
||||
function indexTypesMatch(liveType, schemaIndex, table) {
|
||||
const live = (liveType || "").toUpperCase();
|
||||
const schemaType = schemaIndex.indexType?.toUpperCase();
|
||||
if (isVectorIndexDef(schemaIndex, table)) {
|
||||
// MariaDB may report VECTOR indexes as BTREE or VECTOR depending on version
|
||||
return live === "VECTOR" || live === "BTREE" || live === "";
|
||||
}
|
||||
if (!schemaType || schemaType === "BTREE") {
|
||||
// Default / unspecified → BTREE
|
||||
return live === "BTREE" || live === "";
|
||||
}
|
||||
if (schemaType === "FULLTEXT") {
|
||||
return live === "FULLTEXT";
|
||||
}
|
||||
if (schemaType === "SPATIAL") {
|
||||
return live === "SPATIAL";
|
||||
}
|
||||
if (schemaType === "HASH") {
|
||||
return live === "HASH";
|
||||
}
|
||||
return live === schemaType;
|
||||
}
|
||||
async function createIndex({ table, index, config, }) {
|
||||
if (!index.indexName ||
|
||||
!index.indexTableFields ||
|
||||
index.indexTableFields.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (isVectorIndexDef(index, table)) {
|
||||
console.log(`Creating Vector index: ${index.indexName}`);
|
||||
const targetField = MariaDBQuoteGen(index.indexTableFields[0]);
|
||||
const distanceMetric = vectorDistanceMetric(index);
|
||||
await runSchemaQuery({
|
||||
query: `ALTER TABLE ${MariaDBQuoteGen(table.tableName)} ADD VECTOR INDEX ${MariaDBQuoteGen(index.indexName)} (${targetField}) M=8 DISTANCE=${distanceMetric}`,
|
||||
config,
|
||||
});
|
||||
return;
|
||||
}
|
||||
console.log(`Creating standard index: ${index.indexName}`);
|
||||
const fields = index.indexTableFields
|
||||
.map((field) => MariaDBQuoteGen(field))
|
||||
.join(", ");
|
||||
const typeUpper = index.indexType?.toUpperCase();
|
||||
const isSpecialType = typeUpper === "FULLTEXT" || typeUpper === "SPATIAL";
|
||||
const indexPrefix = isSpecialType ? `${typeUpper} ` : "";
|
||||
const indexSuffix = !isSpecialType && (typeUpper === "BTREE" || typeUpper === "HASH")
|
||||
? ` USING ${typeUpper}`
|
||||
: "";
|
||||
await runSchemaQuery({
|
||||
query: `CREATE ${indexPrefix}INDEX ${MariaDBQuoteGen(index.indexName)} ON ${MariaDBQuoteGen(table.tableName)} (${fields})${indexSuffix}`,
|
||||
config,
|
||||
});
|
||||
}
|
||||
export default async function syncIndexes({ table, config, }) {
|
||||
const schemaCond = schemaCondition(config);
|
||||
const rows = await querySchemaRows({
|
||||
@@ -37,16 +92,8 @@ export default async function syncIndexes({ table, config, }) {
|
||||
config,
|
||||
});
|
||||
const protectedIndexNames = new Set(protectedConstraintRows.map((r) => r.CONSTRAINT_NAME));
|
||||
// Column-level UNIQUE creates an index often named after the column
|
||||
for (const field of table.fields || []) {
|
||||
if (field.unique && field.fieldName) {
|
||||
protectedIndexNames.add(field.fieldName);
|
||||
}
|
||||
}
|
||||
for (const constraint of table.uniqueConstraints || []) {
|
||||
if (constraint.constraintName) {
|
||||
protectedIndexNames.add(constraint.constraintName);
|
||||
}
|
||||
for (const constraint of grabDesiredUniqueConstraints(table)) {
|
||||
protectedIndexNames.add(constraint.name);
|
||||
}
|
||||
const existingIndexesMap = new Map();
|
||||
for (const row of rows) {
|
||||
@@ -70,6 +117,7 @@ export default async function syncIndexes({ table, config, }) {
|
||||
query: `DROP INDEX ${MariaDBQuoteGen(indexName)} ON ${MariaDBQuoteGen(table.tableName)}`,
|
||||
config,
|
||||
});
|
||||
existingIndexesMap.delete(indexName);
|
||||
}
|
||||
catch (err) {
|
||||
if (String(err?.message || "").includes("needed in a foreign key constraint")) {
|
||||
@@ -83,7 +131,8 @@ export default async function syncIndexes({ table, config, }) {
|
||||
const schemaColumns = schemaIndex.indexTableFields || [];
|
||||
const columnsMatch = details.columns.length === schemaColumns.length &&
|
||||
details.columns.every((col, idx) => col === schemaColumns[idx]);
|
||||
if (!columnsMatch) {
|
||||
const typeMatch = indexTypesMatch(details.type, schemaIndex, table);
|
||||
if (!columnsMatch || !typeMatch) {
|
||||
console.log(`Recreating changed index: ${indexName}`);
|
||||
await runSchemaQuery({
|
||||
query: `DROP INDEX ${MariaDBQuoteGen(indexName)} ON ${MariaDBQuoteGen(table.tableName)}`,
|
||||
@@ -100,32 +149,7 @@ export default async function syncIndexes({ table, config, }) {
|
||||
continue;
|
||||
}
|
||||
if (!existingIndexesMap.has(index.indexName)) {
|
||||
if (isVectorIndexDef(index, table)) {
|
||||
console.log(`Creating Vector index: ${index.indexName}`);
|
||||
const targetField = MariaDBQuoteGen(index.indexTableFields[0]);
|
||||
const distanceMetric = vectorDistanceMetric(index);
|
||||
await runSchemaQuery({
|
||||
query: `ALTER TABLE ${MariaDBQuoteGen(table.tableName)} ADD VECTOR INDEX ${MariaDBQuoteGen(index.indexName)} (${targetField}) M=8 DISTANCE=${distanceMetric}`,
|
||||
config,
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log(`Creating standard index: ${index.indexName}`);
|
||||
const fields = index.indexTableFields
|
||||
.map((field) => MariaDBQuoteGen(field))
|
||||
.join(", ");
|
||||
const typeUpper = index.indexType?.toUpperCase();
|
||||
const isSpecialType = typeUpper === "FULLTEXT" || typeUpper === "SPATIAL";
|
||||
const indexPrefix = isSpecialType ? `${typeUpper} ` : "";
|
||||
const indexSuffix = !isSpecialType &&
|
||||
(typeUpper === "BTREE" || typeUpper === "HASH")
|
||||
? ` USING ${typeUpper}`
|
||||
: "";
|
||||
await runSchemaQuery({
|
||||
query: `CREATE ${indexPrefix}INDEX ${MariaDBQuoteGen(index.indexName)} ON ${MariaDBQuoteGen(table.tableName)} (${fields})${indexSuffix}`,
|
||||
config,
|
||||
});
|
||||
}
|
||||
await createIndex({ table, index, config });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user