Update JSON data type matcher
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
|||||||
|
import type { BunMariaDBConfig } from "../../types";
|
||||||
|
export type ColumnInfoRow = {
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
comment?: string;
|
||||||
|
isNullable: boolean;
|
||||||
|
columnDefault: string | null;
|
||||||
|
extra: string;
|
||||||
|
};
|
||||||
|
export default function getTableColumnsGemini({ tableName, config, }: {
|
||||||
|
tableName: string;
|
||||||
|
config?: BunMariaDBConfig;
|
||||||
|
}): Promise<ColumnInfoRow[]>;
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
import { querySchemaRows } from "./run-schema-query";
|
||||||
|
import schemaCondition from "./schema-condition";
|
||||||
|
export default async function getTableColumnsGemini({ tableName, config, }) {
|
||||||
|
const schemaCond = schemaCondition(config);
|
||||||
|
const rows = await querySchemaRows({
|
||||||
|
query: `
|
||||||
|
SELECT
|
||||||
|
c.COLUMN_NAME,
|
||||||
|
c.COLUMN_TYPE,
|
||||||
|
c.COLUMN_COMMENT,
|
||||||
|
c.IS_NULLABLE,
|
||||||
|
c.COLUMN_DEFAULT,
|
||||||
|
c.EXTRA,
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.CHECK_CONSTRAINTS cc
|
||||||
|
WHERE cc.CONSTRAINT_SCHEMA = c.TABLE_SCHEMA
|
||||||
|
AND cc.TABLE_NAME = c.TABLE_NAME
|
||||||
|
AND cc.CHECK_CLAUSE LIKE CONCAT('%json_valid(\`', c.COLUMN_NAME, '\`)%')
|
||||||
|
) AS IS_JSON
|
||||||
|
FROM information_schema.COLUMNS c
|
||||||
|
WHERE ${schemaCond.where.replace(/\bTABLE_SCHEMA\b/g, "c.TABLE_SCHEMA")}
|
||||||
|
AND c.TABLE_NAME = ?
|
||||||
|
ORDER BY c.ORDINAL_POSITION
|
||||||
|
`,
|
||||||
|
values: [...schemaCond.values, tableName],
|
||||||
|
config,
|
||||||
|
});
|
||||||
|
return rows.map((row) => {
|
||||||
|
const isJson = Number(row.IS_JSON) === 1;
|
||||||
|
// Normalize longtext with a json_valid constraint to "json"
|
||||||
|
let resolvedType = row.COLUMN_TYPE;
|
||||||
|
if (isJson && row.COLUMN_TYPE.toLowerCase() === "longtext") {
|
||||||
|
resolvedType = "json";
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
name: row.COLUMN_NAME,
|
||||||
|
type: resolvedType,
|
||||||
|
comment: row.COLUMN_COMMENT,
|
||||||
|
isNullable: String(row.IS_NULLABLE).toUpperCase() === "YES",
|
||||||
|
columnDefault: row.COLUMN_DEFAULT,
|
||||||
|
extra: row.EXTRA || "",
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
Vendored
+2
-1
@@ -66,11 +66,12 @@ export default function mapDataType(field) {
|
|||||||
case "YEAR":
|
case "YEAR":
|
||||||
return "YEAR";
|
return "YEAR";
|
||||||
case "UUID":
|
case "UUID":
|
||||||
return "CHAR(36)"; // MariaDB does not have a native UUID type
|
return "UUID";
|
||||||
case "JSON":
|
case "JSON":
|
||||||
return "JSON";
|
return "JSON";
|
||||||
case "INET6":
|
case "INET6":
|
||||||
return "INET6";
|
return "INET6";
|
||||||
|
case "BOOL":
|
||||||
case "BOOLEAN":
|
case "BOOLEAN":
|
||||||
return "TINYINT(1)";
|
return "TINYINT(1)";
|
||||||
case "ENUM": {
|
case "ENUM": {
|
||||||
|
|||||||
Vendored
+7
-3
@@ -1,6 +1,7 @@
|
|||||||
import buildColumnDefinition, { fieldRequiresNotNull, } from "./build-column-definition";
|
import buildColumnDefinition, { fieldRequiresNotNull, } from "./build-column-definition";
|
||||||
import createTable from "./create-table";
|
import createTable from "./create-table";
|
||||||
import getTableColumns, {} from "./get-table-columns";
|
import getTableColumns, {} from "./get-table-columns";
|
||||||
|
import getTableColumnsGemini from "./get-table-columns-gemnini";
|
||||||
import isVectorField from "./is-vector-field";
|
import isVectorField from "./is-vector-field";
|
||||||
import mapDataType from "./map-data-types";
|
import mapDataType from "./map-data-types";
|
||||||
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
||||||
@@ -110,7 +111,9 @@ function columnAttributesDiverged(live, field) {
|
|||||||
if (wantsOnUpdate && hasOnUpdate) {
|
if (wantsOnUpdate && hasOnUpdate) {
|
||||||
const liveOnUpdate = normalizeDefault((live.extra.match(/on update\s+(.+)/i) || [])[1] || "");
|
const liveOnUpdate = normalizeDefault((live.extra.match(/on update\s+(.+)/i) || [])[1] || "");
|
||||||
const expectedOu = normalizeDefault(wantsOnUpdate);
|
const expectedOu = normalizeDefault(wantsOnUpdate);
|
||||||
if (liveOnUpdate && expectedOu && !defaultsMatch(liveOnUpdate, expectedOu)) {
|
if (liveOnUpdate &&
|
||||||
|
expectedOu &&
|
||||||
|
!defaultsMatch(liveOnUpdate, expectedOu)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,7 +188,7 @@ async function recreateColumn({ tableName, field, config, }) {
|
|||||||
await addColumn({ tableName, field, config });
|
await addColumn({ tableName, field, config });
|
||||||
}
|
}
|
||||||
export default async function updateTable({ table, config, }) {
|
export default async function updateTable({ table, config, }) {
|
||||||
const existingColumns = await getTableColumns({
|
const existingColumns = await getTableColumnsGemini({
|
||||||
tableName: table.tableName,
|
tableName: table.tableName,
|
||||||
config,
|
config,
|
||||||
});
|
});
|
||||||
@@ -207,7 +210,8 @@ export default async function updateTable({ table, config, }) {
|
|||||||
fieldsToAdd.push(field);
|
fieldsToAdd.push(field);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let typeDiverged = !columnTypesMatch(liveField.type, mapDataType(field));
|
let mapped_data_type = mapDataType(field);
|
||||||
|
let typeDiverged = !columnTypesMatch(liveField.type, mapped_data_type);
|
||||||
if (isVectorField(field)) {
|
if (isVectorField(field)) {
|
||||||
typeDiverged = vectorTypeDiverged(liveField.type, liveField.comment || "", field);
|
typeDiverged = vectorTypeDiverged(liveField.type, liveField.comment || "", field);
|
||||||
if (typeDiverged) {
|
if (typeDiverged) {
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@moduletrace/bun-mariadb",
|
"name": "@moduletrace/bun-mariadb",
|
||||||
"version": "1.0.12",
|
"version": "1.0.13",
|
||||||
"description": "Schema-driven MariaDB manager for Bun",
|
"description": "Schema-driven MariaDB manager for Bun",
|
||||||
"author": "Benjamin Toby",
|
"author": "Benjamin Toby",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import type { BunMariaDBConfig } from "../../types";
|
||||||
|
import { querySchemaRows } from "./run-schema-query";
|
||||||
|
import schemaCondition from "./schema-condition";
|
||||||
|
|
||||||
|
export type ColumnInfoRow = {
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
comment?: string;
|
||||||
|
isNullable: boolean;
|
||||||
|
columnDefault: string | null;
|
||||||
|
extra: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function getTableColumnsGemini({
|
||||||
|
tableName,
|
||||||
|
config,
|
||||||
|
}: {
|
||||||
|
tableName: string;
|
||||||
|
config?: BunMariaDBConfig;
|
||||||
|
}): Promise<ColumnInfoRow[]> {
|
||||||
|
const schemaCond = schemaCondition(config);
|
||||||
|
const rows = await querySchemaRows<{
|
||||||
|
COLUMN_NAME: string;
|
||||||
|
COLUMN_TYPE: string;
|
||||||
|
COLUMN_COMMENT: string;
|
||||||
|
IS_NULLABLE: string;
|
||||||
|
COLUMN_DEFAULT: string | null;
|
||||||
|
EXTRA: string;
|
||||||
|
IS_JSON: number | boolean;
|
||||||
|
}>({
|
||||||
|
query: `
|
||||||
|
SELECT
|
||||||
|
c.COLUMN_NAME,
|
||||||
|
c.COLUMN_TYPE,
|
||||||
|
c.COLUMN_COMMENT,
|
||||||
|
c.IS_NULLABLE,
|
||||||
|
c.COLUMN_DEFAULT,
|
||||||
|
c.EXTRA,
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.CHECK_CONSTRAINTS cc
|
||||||
|
WHERE cc.CONSTRAINT_SCHEMA = c.TABLE_SCHEMA
|
||||||
|
AND cc.TABLE_NAME = c.TABLE_NAME
|
||||||
|
AND cc.CHECK_CLAUSE LIKE CONCAT('%json_valid(\`', c.COLUMN_NAME, '\`)%')
|
||||||
|
) AS IS_JSON
|
||||||
|
FROM information_schema.COLUMNS c
|
||||||
|
WHERE ${schemaCond.where.replace(/\bTABLE_SCHEMA\b/g, "c.TABLE_SCHEMA")}
|
||||||
|
AND c.TABLE_NAME = ?
|
||||||
|
ORDER BY c.ORDINAL_POSITION
|
||||||
|
`,
|
||||||
|
values: [...schemaCond.values, tableName],
|
||||||
|
config,
|
||||||
|
});
|
||||||
|
|
||||||
|
return rows.map((row) => {
|
||||||
|
const isJson = Number(row.IS_JSON) === 1;
|
||||||
|
|
||||||
|
// Normalize longtext with a json_valid constraint to "json"
|
||||||
|
let resolvedType = row.COLUMN_TYPE;
|
||||||
|
if (isJson && row.COLUMN_TYPE.toLowerCase() === "longtext") {
|
||||||
|
resolvedType = "json";
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: row.COLUMN_NAME,
|
||||||
|
type: resolvedType,
|
||||||
|
comment: row.COLUMN_COMMENT,
|
||||||
|
isNullable: String(row.IS_NULLABLE).toUpperCase() === "YES",
|
||||||
|
columnDefault: row.COLUMN_DEFAULT,
|
||||||
|
extra: row.EXTRA || "",
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -72,11 +72,12 @@ export default function mapDataType(
|
|||||||
case "YEAR":
|
case "YEAR":
|
||||||
return "YEAR";
|
return "YEAR";
|
||||||
case "UUID":
|
case "UUID":
|
||||||
return "CHAR(36)"; // MariaDB does not have a native UUID type
|
return "UUID";
|
||||||
case "JSON":
|
case "JSON":
|
||||||
return "JSON";
|
return "JSON";
|
||||||
case "INET6":
|
case "INET6":
|
||||||
return "INET6";
|
return "INET6";
|
||||||
|
case "BOOL":
|
||||||
case "BOOLEAN":
|
case "BOOLEAN":
|
||||||
return "TINYINT(1)";
|
return "TINYINT(1)";
|
||||||
case "ENUM": {
|
case "ENUM": {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import buildColumnDefinition, {
|
|||||||
} from "./build-column-definition";
|
} from "./build-column-definition";
|
||||||
import createTable from "./create-table";
|
import createTable from "./create-table";
|
||||||
import getTableColumns, { type ColumnInfoRow } from "./get-table-columns";
|
import getTableColumns, { type ColumnInfoRow } from "./get-table-columns";
|
||||||
|
import getTableColumnsGemini from "./get-table-columns-gemnini";
|
||||||
import isVectorField from "./is-vector-field";
|
import isVectorField from "./is-vector-field";
|
||||||
import mapDataType from "./map-data-types";
|
import mapDataType from "./map-data-types";
|
||||||
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
||||||
@@ -137,7 +138,11 @@ function columnAttributesDiverged(
|
|||||||
(live.extra.match(/on update\s+(.+)/i) || [])[1] || "",
|
(live.extra.match(/on update\s+(.+)/i) || [])[1] || "",
|
||||||
);
|
);
|
||||||
const expectedOu = normalizeDefault(wantsOnUpdate);
|
const expectedOu = normalizeDefault(wantsOnUpdate);
|
||||||
if (liveOnUpdate && expectedOu && !defaultsMatch(liveOnUpdate, expectedOu)) {
|
if (
|
||||||
|
liveOnUpdate &&
|
||||||
|
expectedOu &&
|
||||||
|
!defaultsMatch(liveOnUpdate, expectedOu)
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -273,7 +278,7 @@ export default async function updateTable({
|
|||||||
table: BUN_MARIADB_TableSchemaType;
|
table: BUN_MARIADB_TableSchemaType;
|
||||||
config?: BunMariaDBConfig;
|
config?: BunMariaDBConfig;
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
const existingColumns = await getTableColumns({
|
const existingColumns = await getTableColumnsGemini({
|
||||||
tableName: table.tableName,
|
tableName: table.tableName,
|
||||||
config,
|
config,
|
||||||
});
|
});
|
||||||
@@ -303,9 +308,11 @@ export default async function updateTable({
|
|||||||
if (!liveField) {
|
if (!liveField) {
|
||||||
fieldsToAdd.push(field);
|
fieldsToAdd.push(field);
|
||||||
} else {
|
} else {
|
||||||
|
let mapped_data_type = mapDataType(field);
|
||||||
|
|
||||||
let typeDiverged = !columnTypesMatch(
|
let typeDiverged = !columnTypesMatch(
|
||||||
liveField.type,
|
liveField.type,
|
||||||
mapDataType(field),
|
mapped_data_type,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isVectorField(field)) {
|
if (isVectorField(field)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user