From 9ab5543c5c8717034950d31d39f84048150e17c0 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Wed, 3 Dec 2025 11:45:32 +0100 Subject: [PATCH] Bugfix --- .../functions/dsql/sql/sql-generator.js | 1 - .../shell/createDbFromSchema/index.js | 2 +- .../package-shared/shell/utils/createTable.js | 2 +- .../package-shared/shell/utils/updateTable.js | 20 +++++++++++++++++ dist/package-shared/types/dsql.d.ts | 2 ++ dist/package-shared/types/index.d.ts | 3 +++ dist/package-shared/types/index.js | 6 ++++- .../functions/dsql/sql/sql-generator.ts | 2 -- .../shell/createDbFromSchema/index.ts | 5 +++-- package-shared/shell/utils/createTable.ts | 4 +++- package-shared/shell/utils/updateTable.ts | 22 +++++++++++++++++++ package-shared/types/dsql.ts | 2 ++ package-shared/types/index.ts | 7 ++++++ package.json | 2 +- 14 files changed, 70 insertions(+), 10 deletions(-) diff --git a/dist/package-shared/functions/dsql/sql/sql-generator.js b/dist/package-shared/functions/dsql/sql/sql-generator.js index 92e9e5b..465f913 100644 --- a/dist/package-shared/functions/dsql/sql/sql-generator.js +++ b/dist/package-shared/functions/dsql/sql/sql-generator.js @@ -287,7 +287,6 @@ function sqlGenerator({ tableName, genObject, dbFullName, count }) { orderFields.push(genObject.order.field); } orderSrt += ` ${orderFields.join(", ")} ${genObject.order.strategy}`; - console.log("orderSrt", orderSrt); queryString += ` ${orderSrt}`; } if ((genObject === null || genObject === void 0 ? void 0 : genObject.limit) && !count) diff --git a/dist/package-shared/shell/createDbFromSchema/index.js b/dist/package-shared/shell/createDbFromSchema/index.js index 632ed84..541f1af 100644 --- a/dist/package-shared/shell/createDbFromSchema/index.js +++ b/dist/package-shared/shell/createDbFromSchema/index.js @@ -95,7 +95,7 @@ function createDbFromSchema(_a) { }); if (!((_b = dbCheck === null || dbCheck === void 0 ? void 0 : dbCheck[0]) === null || _b === void 0 ? void 0 : _b.dbFullName)) { const newDatabase = yield (0, dbHandler_1.default)({ - query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`, + query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE ${database.collation || "utf8mb4_bin"}`, }); } const allTables = (yield (0, dbHandler_1.default)({ diff --git a/dist/package-shared/shell/utils/createTable.js b/dist/package-shared/shell/utils/createTable.js index d12a978..467c01c 100644 --- a/dist/package-shared/shell/utils/createTable.js +++ b/dist/package-shared/shell/utils/createTable.js @@ -48,7 +48,7 @@ function createTable(_a) { })(); createTableQueryArray.push(" " + fieldEntryText + comma); } - createTableQueryArray.push(`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`); + createTableQueryArray.push(`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=${(tableSchema === null || tableSchema === void 0 ? void 0 : tableSchema.collation) || "utf8mb4_bin"};`); const createTableQuery = createTableQueryArray.join("\n"); const newTable = yield (0, dbHandler_1.default)({ query: createTableQuery, diff --git a/dist/package-shared/shell/utils/updateTable.js b/dist/package-shared/shell/utils/updateTable.js index f46dea2..2db0b7c 100644 --- a/dist/package-shared/shell/utils/updateTable.js +++ b/dist/package-shared/shell/utils/updateTable.js @@ -63,6 +63,26 @@ function updateTable(_a) { if (!tableID && !isMain) { throw new Error("Recorded Table entry not found!"); } + /** + * Handle Table Default Collation + * + * @description Update Column Collation + */ + if (tableSchema.collation) { + try { + const existingCollation = (yield (0, dbHandler_1.default)({ + query: `SHOW TABLE STATUS LIKE '${tableName}'`, + config: { database: dbFullName }, + })); + const existingCollationStr = existingCollation === null || existingCollation === void 0 ? void 0 : existingCollation[0].Collation; + if (existingCollationStr !== tableSchema.collation) { + yield (0, dbHandler_1.default)({ + query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE ${tableSchema.collation}`, + }); + } + } + catch (error) { } + } /** * Handle Table updates * diff --git a/dist/package-shared/types/dsql.d.ts b/dist/package-shared/types/dsql.d.ts index 89120be..387e057 100644 --- a/dist/package-shared/types/dsql.d.ts +++ b/dist/package-shared/types/dsql.d.ts @@ -101,6 +101,7 @@ export type DSQL_DATASQUIREL_USER_DATABASES = { active_clone_parent_db?: string; active_clone_parent_db_id?: number; active_data?: number; + collation?: string; last_checked_date_code?: number; date_created?: string; date_created_code?: number; @@ -124,6 +125,7 @@ export type DSQL_DATASQUIREL_USER_DATABASE_TABLES = { child_table_parent_database_schema_id?: number; child_table_parent_table_schema_id?: number; active_data?: 0 | 1; + collation?: string; last_checked_date_code?: number; date_created?: string; date_created_code?: number; diff --git a/dist/package-shared/types/index.d.ts b/dist/package-shared/types/index.d.ts index 38113d9..2cb34a7 100644 --- a/dist/package-shared/types/index.d.ts +++ b/dist/package-shared/types/index.d.ts @@ -39,10 +39,12 @@ export interface DSQL_DatabaseSchemaType { childDatabase?: boolean; childDatabaseDbId?: string | number; updateData?: boolean; + collation?: (typeof MariaDBCollations)[number]; } export interface DSQL_ChildrenDatabaseObject { dbId?: string | number; } +export declare const MariaDBCollations: readonly ["utf8mb4_bin", "utf8mb4_unicode_520_ci"]; export interface DSQL_TableSchemaType { id?: string | number; tableName: string; @@ -55,6 +57,7 @@ export interface DSQL_TableSchemaType { childTableId?: string | number; tableNameOld?: string; childTableDbId?: string | number; + collation?: (typeof MariaDBCollations)[number]; } export interface DSQL_ChildrenTablesType { tableId?: string | number; diff --git a/dist/package-shared/types/index.js b/dist/package-shared/types/index.js index 1df035d..e78a276 100644 --- a/dist/package-shared/types/index.js +++ b/dist/package-shared/types/index.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.AIOptions = exports.OpsActions = exports.TimeParadigms = exports.UserAPIAuthActions = exports.UserAPIParadigms = exports.TargetMediaParadigms = exports.EnvKeys = exports.AppVersions = exports.APIParadigms = exports.UserSQLPermissions = exports.SQLPermissions = exports.InvitedUserSelectFields = exports.DelegatedUserSelectFields = exports.UserSelectFields = exports.IndexTypes = exports.DefaultSQLValuesLiteral = exports.CurrentlyEditedFieldActions = exports.FileMimeTypes = exports.VideoMimeTypes = exports.ImageMimeTypes = exports.MediaTypes = exports.DockerComposeServices = exports.DatasquirelWindowEvents = exports.WebSocketEvents = exports.QueueJobTypes = exports.SignUpParadigms = exports.UserTypes = exports.QueryFields = exports.DsqlCrudActions = exports.DataCrudRequestMethodsLowerCase = exports.DataCrudRequestMethods = exports.ServerQueryEqualities = exports.ServerQueryOperators = exports.TextFieldTypesArray = exports.UsersOmitedFields = void 0; +exports.AIOptions = exports.OpsActions = exports.TimeParadigms = exports.UserAPIAuthActions = exports.UserAPIParadigms = exports.TargetMediaParadigms = exports.EnvKeys = exports.AppVersions = exports.APIParadigms = exports.UserSQLPermissions = exports.SQLPermissions = exports.InvitedUserSelectFields = exports.DelegatedUserSelectFields = exports.UserSelectFields = exports.IndexTypes = exports.DefaultSQLValuesLiteral = exports.CurrentlyEditedFieldActions = exports.FileMimeTypes = exports.VideoMimeTypes = exports.ImageMimeTypes = exports.MediaTypes = exports.DockerComposeServices = exports.DatasquirelWindowEvents = exports.WebSocketEvents = exports.QueueJobTypes = exports.SignUpParadigms = exports.UserTypes = exports.QueryFields = exports.DsqlCrudActions = exports.DataCrudRequestMethodsLowerCase = exports.DataCrudRequestMethods = exports.ServerQueryEqualities = exports.ServerQueryOperators = exports.TextFieldTypesArray = exports.MariaDBCollations = exports.UsersOmitedFields = void 0; exports.UsersOmitedFields = [ "password", "social_id", @@ -12,6 +12,10 @@ exports.UsersOmitedFields = [ "date_updated_code", "date_updated_timestamp", ]; +exports.MariaDBCollations = [ + "utf8mb4_bin", + "utf8mb4_unicode_520_ci", +]; exports.TextFieldTypesArray = [ { title: "Plain Text", value: "plain" }, { title: "Rich Text", value: "richText" }, diff --git a/package-shared/functions/dsql/sql/sql-generator.ts b/package-shared/functions/dsql/sql/sql-generator.ts index 96908d3..82e9f89 100644 --- a/package-shared/functions/dsql/sql/sql-generator.ts +++ b/package-shared/functions/dsql/sql/sql-generator.ts @@ -386,8 +386,6 @@ export default function sqlGenerator< orderSrt += ` ${orderFields.join(", ")} ${genObject.order.strategy}`; - console.log("orderSrt", orderSrt); - queryString += ` ${orderSrt}`; } diff --git a/package-shared/shell/createDbFromSchema/index.ts b/package-shared/shell/createDbFromSchema/index.ts index b0fe4d0..f13b3cc 100644 --- a/package-shared/shell/createDbFromSchema/index.ts +++ b/package-shared/shell/createDbFromSchema/index.ts @@ -1,4 +1,3 @@ -import fs from "fs"; import createTable from "../utils/createTable"; import updateTable from "../utils/updateTable"; import { DSQL_DatabaseSchemaType } from "../../types"; @@ -74,7 +73,9 @@ export default async function createDbFromSchema({ if (!dbCheck?.[0]?.dbFullName) { const newDatabase = await dbHandler({ - query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`, + query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE ${ + database.collation || "utf8mb4_bin" + }`, }); } diff --git a/package-shared/shell/utils/createTable.ts b/package-shared/shell/utils/createTable.ts index e502fc0..b1b2f29 100644 --- a/package-shared/shell/utils/createTable.ts +++ b/package-shared/shell/utils/createTable.ts @@ -63,7 +63,9 @@ export default async function createTable({ } createTableQueryArray.push( - `) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;` + `) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=${ + tableSchema?.collation || "utf8mb4_bin" + };` ); const createTableQuery = createTableQueryArray.join("\n"); diff --git a/package-shared/shell/utils/updateTable.ts b/package-shared/shell/utils/updateTable.ts index e50a637..bc6a49b 100644 --- a/package-shared/shell/utils/updateTable.ts +++ b/package-shared/shell/utils/updateTable.ts @@ -92,6 +92,28 @@ export default async function updateTable({ throw new Error("Recorded Table entry not found!"); } + /** + * Handle Table Default Collation + * + * @description Update Column Collation + */ + if (tableSchema.collation) { + try { + const existingCollation = (await dbHandler({ + query: `SHOW TABLE STATUS LIKE '${tableName}'`, + config: { database: dbFullName }, + })) as any[]; + + const existingCollationStr = existingCollation?.[0].Collation; + + if (existingCollationStr !== tableSchema.collation) { + await dbHandler({ + query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE ${tableSchema.collation}`, + }); + } + } catch (error) {} + } + /** * Handle Table updates * diff --git a/package-shared/types/dsql.ts b/package-shared/types/dsql.ts index 3203950..ea153d1 100644 --- a/package-shared/types/dsql.ts +++ b/package-shared/types/dsql.ts @@ -128,6 +128,7 @@ export type DSQL_DATASQUIREL_USER_DATABASES = { active_clone_parent_db?: string; active_clone_parent_db_id?: number; active_data?: number; + collation?: string; last_checked_date_code?: number; date_created?: string; date_created_code?: number; @@ -152,6 +153,7 @@ export type DSQL_DATASQUIREL_USER_DATABASE_TABLES = { child_table_parent_database_schema_id?: number; child_table_parent_table_schema_id?: number; active_data?: 0 | 1; + collation?: string; last_checked_date_code?: number; date_created?: string; date_created_code?: number; diff --git a/package-shared/types/index.ts b/package-shared/types/index.ts index 19e890b..1f30b5a 100644 --- a/package-shared/types/index.ts +++ b/package-shared/types/index.ts @@ -80,12 +80,18 @@ export interface DSQL_DatabaseSchemaType { childDatabase?: boolean; childDatabaseDbId?: string | number; updateData?: boolean; + collation?: (typeof MariaDBCollations)[number]; } export interface DSQL_ChildrenDatabaseObject { dbId?: string | number; } +export const MariaDBCollations = [ + "utf8mb4_bin", + "utf8mb4_unicode_520_ci", +] as const; + export interface DSQL_TableSchemaType { id?: string | number; tableName: string; @@ -98,6 +104,7 @@ export interface DSQL_TableSchemaType { childTableId?: string | number; tableNameOld?: string; childTableDbId?: string | number; + collation?: (typeof MariaDBCollations)[number]; } export interface DSQL_ChildrenTablesType { diff --git a/package.json b/package.json index 2d15dbd..b54d242 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "5.4.9", + "version": "5.5.0", "description": "Cloud-based SQL data management tool", "main": "dist/index.js", "bin": {