UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 7.63 kB
{"version":3,"file":"modify-table-query.mjs","names":["constants"],"sources":["../../../../src/libs/collection/migration/modify-table-query.ts"],"sourcesContent":["import type { AlterTableColumnAlteringBuilder } from \"kysely\";\nimport constants from \"../../../constants/constants.js\";\nimport logger from \"../../../libs/logger/index.js\";\nimport type { ServiceFn } from \"../../../types.js\";\nimport { copy } from \"../../i18n/index.js\";\nimport { addColumn, dropColumn, modifyColumn } from \"./column-builder.js\";\nimport { addIndex, dropIndex } from \"./index-builder.js\";\nimport type { TableMigration } from \"./types.js\";\n\n/**\n * Executes table modifications, handling databases with and without multiple ALTER TABLE support\n */\nconst modifyTableQuery: ServiceFn<\n\t[\n\t\t{\n\t\t\tmigration: TableMigration;\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\ttry {\n\t\tconst supportsMultipleAlter =\n\t\t\tcontext.config.db.supports(\"multipleAlterTables\") ?? false;\n\t\tlet altered = false;\n\n\t\tawait Promise.all(\n\t\t\tdata.migration.indexOperations\n\t\t\t\t.filter((operation) => operation.type === \"remove\")\n\t\t\t\t.map((operation) =>\n\t\t\t\t\tdropIndex(context, data.migration.tableName, operation.index.name),\n\t\t\t\t),\n\t\t);\n\n\t\t//* for db that support multiple ALTER TABLE operations (postgres)\n\t\tif (supportsMultipleAlter) {\n\t\t\tlet query = context.db.client.schema.alterTable(\n\t\t\t\tdata.migration.tableName,\n\t\t\t) as unknown as AlterTableColumnAlteringBuilder;\n\n\t\t\tfor (const operation of data.migration.columnOperations) {\n\t\t\t\tswitch (operation.type) {\n\t\t\t\t\tcase \"add\":\n\t\t\t\t\t\tquery = addColumn(query, operation, context.config.db);\n\t\t\t\t\t\taltered = true;\n\t\t\t\t\t\tlogger.debug({\n\t\t\t\t\t\t\tmessage: `Operation of type 'add' ran on column '${operation.column.name}' for table '${data.migration.tableName}'`,\n\t\t\t\t\t\t\tscope: constants.logScopes.migrations,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"modify\":\n\t\t\t\t\t\tquery = modifyColumn(query, operation, context.config.db);\n\t\t\t\t\t\taltered = true;\n\t\t\t\t\t\tlogger.debug({\n\t\t\t\t\t\t\tmessage: `Operation of type 'modify' ran on column '${operation.column.name}' for table '${data.migration.tableName}'`,\n\t\t\t\t\t\t\tscope: constants.logScopes.migrations,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"remove\":\n\t\t\t\t\t\tquery = dropColumn(query, operation);\n\t\t\t\t\t\taltered = true;\n\t\t\t\t\t\tlogger.debug({\n\t\t\t\t\t\t\tmessage: `Operation of type 'remove' ran on column '${operation.columnName}' for table '${data.migration.tableName}'`,\n\t\t\t\t\t\t\tscope: constants.logScopes.migrations,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (altered) {\n\t\t\t\tawait query.execute();\n\t\t\t}\n\n\t\t\tawait Promise.all(\n\t\t\t\tdata.migration.indexOperations\n\t\t\t\t\t.filter((operation) => operation.type === \"add\")\n\t\t\t\t\t.map((operation) =>\n\t\t\t\t\t\taddIndex(context, data.migration.tableName, operation.index),\n\t\t\t\t\t),\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tdata: undefined,\n\t\t\t\terror: undefined,\n\t\t\t};\n\t\t}\n\n\t\t//* for dbs that dont support multiple ALTER TABLE ops (sqlite)\n\t\tconst queries = [];\n\t\tfor (const operation of data.migration.columnOperations) {\n\t\t\tlet query = context.db.client.schema.alterTable(\n\t\t\t\tdata.migration.tableName,\n\t\t\t) as unknown as AlterTableColumnAlteringBuilder;\n\n\t\t\tswitch (operation.type) {\n\t\t\t\tcase \"add\":\n\t\t\t\t\tquery = addColumn(query, operation, context.config.db);\n\t\t\t\t\taltered = true;\n\t\t\t\t\tlogger.debug({\n\t\t\t\t\t\tmessage: `Operation of type 'add' ran on column '${operation.column.name}' for table '${data.migration.tableName}'`,\n\t\t\t\t\t\tscope: constants.logScopes.migrations,\n\t\t\t\t\t});\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"modify\":\n\t\t\t\t\tquery = modifyColumn(query, operation, context.config.db);\n\t\t\t\t\taltered = true;\n\t\t\t\t\tlogger.debug({\n\t\t\t\t\t\tmessage: `Operation of type 'modify' ran on column '${operation.column.name}' for table '${data.migration.tableName}'`,\n\t\t\t\t\t\tscope: constants.logScopes.migrations,\n\t\t\t\t\t});\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"remove\":\n\t\t\t\t\tquery = dropColumn(query, operation);\n\t\t\t\t\taltered = true;\n\t\t\t\t\tlogger.debug({\n\t\t\t\t\t\tmessage: `Operation of type 'remove' ran on column '${operation.columnName}' for table '${data.migration.tableName}'`,\n\t\t\t\t\t\tscope: constants.logScopes.migrations,\n\t\t\t\t\t});\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (altered) {\n\t\t\t\tqueries.push(query.execute());\n\t\t\t\taltered = false;\n\t\t\t}\n\t\t}\n\n\t\tawait Promise.all(queries);\n\n\t\tawait Promise.all(\n\t\t\tdata.migration.indexOperations\n\t\t\t\t.filter((operation) => operation.type === \"add\")\n\t\t\t\t.map((operation) =>\n\t\t\t\t\taddIndex(context, data.migration.tableName, operation.index),\n\t\t\t\t),\n\t\t);\n\n\t\treturn {\n\t\t\tdata: undefined,\n\t\t\terror: undefined,\n\t\t};\n\t} catch (err) {\n\t\treturn {\n\t\t\tdata: undefined,\n\t\t\terror: {\n\t\t\t\tmessage: copy(\n\t\t\t\t\t\"server:core.collections.migration.table.modify.failed.message\",\n\t\t\t\t\t{\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\ttableName: data.migration.tableName,\n\t\t\t\t\t\t\terrorMessage: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t},\n\t\t};\n\t}\n};\n\n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