From 305e94406778c3cffb64cedf334b2a5984919119 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Tue, 29 Oct 2024 18:20:23 +1100 Subject: [PATCH] chore: fix more timestamps fields, stored enum for source_type for deployment and task --- docker-compose.local-dev-mysql.yaml | 1 - .../20241231000000_source_type_enums.js | 22 ++++++++++++++ ...=> 20241231000100_mysql8_compatibility.js} | 30 +++++++++++++++---- services/api/src/models/environment.ts | 2 +- .../api/src/resources/deployment/resolvers.ts | 3 ++ .../src/resources/environment/resolvers.ts | 1 - services/api/src/resources/task/helpers.ts | 3 ++ 7 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 services/api/database/migrations/20241231000000_source_type_enums.js rename services/api/database/migrations/{20241231000000_mysql8_compatibility.js => 20241231000100_mysql8_compatibility.js} (72%) diff --git a/docker-compose.local-dev-mysql.yaml b/docker-compose.local-dev-mysql.yaml index d60c44cedc..c9ebbf26eb 100644 --- a/docker-compose.local-dev-mysql.yaml +++ b/docker-compose.local-dev-mysql.yaml @@ -6,7 +6,6 @@ volumes: services: api-db: image: uselagoon/mysql-8.0:latest - command: mysqld --sql_mode="" environment: - MYSQL_USER=api - MYSQL_PASSWORD=api diff --git a/services/api/database/migrations/20241231000000_source_type_enums.js b/services/api/database/migrations/20241231000000_source_type_enums.js new file mode 100644 index 0000000000..ef2afea9bb --- /dev/null +++ b/services/api/database/migrations/20241231000000_source_type_enums.js @@ -0,0 +1,22 @@ +exports.up = function(knex) { + // fix the way the enum values are stored to be lowercase for mysql strict + return Promise.all([ + knex('deployment') + .where('source_type', '=', 'API') + .update('source_type', 'api'), + knex('deployment') + .where('source_type', '=', 'WEBHOOK') + .update('source_type', 'webhook'), + knex('task') + .where('source_type', '=', 'API') + .update('source_type', 'api'), + ]); + }; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function(knex) { +return knex.schema; +}; \ No newline at end of file diff --git a/services/api/database/migrations/20241231000000_mysql8_compatibility.js b/services/api/database/migrations/20241231000100_mysql8_compatibility.js similarity index 72% rename from services/api/database/migrations/20241231000000_mysql8_compatibility.js rename to services/api/database/migrations/20241231000100_mysql8_compatibility.js index 1c054cf778..5efa4e3e00 100644 --- a/services/api/database/migrations/20241231000000_mysql8_compatibility.js +++ b/services/api/database/migrations/20241231000100_mysql8_compatibility.js @@ -8,10 +8,15 @@ exports.up = async function(knex) { table.specificType('description', 'text').notNullable().alter(); table.string('group_name', 300).alter(); table.specificType('command', 'text').alter(); - table.timestamp('deleted').notNullable().alter(); + table.timestamp('deleted').alter(); + }) + .alterTable('deployment', function (table) { + table.timestamp('created').notNullable().defaultTo(knex.fn.now()).alter(); + table.timestamp('started').alter(); + table.timestamp('completed').alter(); }) .alterTable('environment', function (table) { - table.timestamp('deleted').notNullable().alter(); + table.timestamp('deleted').alter(); }) .alterTable('environment_fact', function (table) { table.specificType('description', 'text').alter(); @@ -22,17 +27,22 @@ exports.up = async function(knex) { table.string('lagoon_service', 100).defaultTo('').alter(); table.specificType('description', 'text').alter(); table.string('version', 100).defaultTo('').alter(); - table.timestamp('deleted').notNullable().alter(); + table.timestamp('deleted').alter(); }) .alterTable('organization', function (table) { table.specificType('description', 'text').notNullable().alter(); }) .alterTable('s3_file', function (table) { table.timestamp('created').notNullable().defaultTo(knex.fn.now()).alter(); - table.timestamp('deleted').notNullable().alter(); + table.timestamp('deleted').alter(); + }) + .alterTable('task', function (table) { + table.timestamp('created').notNullable().defaultTo(knex.fn.now()).alter(); + table.timestamp('started').alter(); + table.timestamp('completed').alter(); }) .alterTable('workflow', function (table) { - table.timestamp('deleted').notNullable().alter(); + table.timestamp('deleted').alter(); }) }; @@ -50,6 +60,11 @@ exports.down = async function(knex) { table.string('group_name', 300).alter(); table.specificType('command', 'text').defaultTo('').alter(); }) + .alterTable('deployment', function (table) { + table.datetime('created').notNullable().defaultTo(knex.fn.now()).alter(); + table.datetime('started').alter(); + table.datetime('completed').alter(); + }) .alterTable('environment_fact', function (table) { table.specificType('description', 'text').defaultTo('').alter(); table.specificType('category', 'text').defaultTo('').alter(); @@ -67,6 +82,11 @@ exports.down = async function(knex) { table.datetime('created').notNullable().defaultTo(knex.fn.now()).alter(); table.datetime('deleted').notNullable().alter(); }) + .alterTable('task', function (table) { + table.datetime('created').notNullable().defaultTo(knex.fn.now()).alter(); + table.datetime('started').alter(); + table.datetime('completed').alter(); + }) .alterTable('organization', function (table) { table.specificType('description', 'text').notNullable().defaultTo('').alter(); }) diff --git a/services/api/src/models/environment.ts b/services/api/src/models/environment.ts index ba2588e655..10de050f16 100644 --- a/services/api/src/models/environment.ts +++ b/services/api/src/models/environment.ts @@ -13,7 +13,7 @@ export interface Environment { openshiftProjectName?: string; // varchar(100) COLLATE utf8_bin DEFAULT NULL, updated?: string; // timestamp NOT NULL DEFAULT current_timestamp(), created?: string; // timestamp NOT NULL DEFAULT current_timestamp(), - deleted?: string; // timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', + deleted?: string; route?: string; // varchar(300) COLLATE utf8_bin DEFAULT NULL, routes?: string; // text COLLATE utf8_bin DEFAULT NULL, autoIdle?: Boolean; // int(1) NOT NULL DEFAULT 1, diff --git a/services/api/src/resources/deployment/resolvers.ts b/services/api/src/resources/deployment/resolvers.ts index 28908dbc9c..28d815518f 100644 --- a/services/api/src/resources/deployment/resolvers.ts +++ b/services/api/src/resources/deployment/resolvers.ts @@ -343,6 +343,9 @@ export const addDeployment: ResolverFn = async ( if (!sourceType) { sourceType = "API" } + if (sourceType) { + sourceType.toLocaleLowerCase(); + } const { insertId } = await query( sqlClientPool, Sql.insertDeployment({ diff --git a/services/api/src/resources/environment/resolvers.ts b/services/api/src/resources/environment/resolvers.ts index 202abfebd6..ef9d4ef859 100644 --- a/services/api/src/resources/environment/resolvers.ts +++ b/services/api/src/resources/environment/resolvers.ts @@ -380,7 +380,6 @@ export const addOrUpdateEnvironment: ResolverFn = async ( const inputDefaults = { deployHeadRef: null, deployTitle: null, - deleted: 0, }; const insertData = R.pick([ diff --git a/services/api/src/resources/task/helpers.ts b/services/api/src/resources/task/helpers.ts index db28a8c71e..62d8820efe 100644 --- a/services/api/src/resources/task/helpers.ts +++ b/services/api/src/resources/task/helpers.ts @@ -73,6 +73,9 @@ export const Helpers = (sqlClientPool: Pool, hasPermission, adminScopes) => { sourceUser: string; sourceType: string; }) => { + if (sourceType) { + sourceType.toLocaleLowerCase(); + } const { insertId } = await query( sqlClientPool, Sql.insertTask({