diff --git a/bin/stop-harmony-and-services b/bin/stop-harmony-and-services index 292914cc9..6fb8d192e 100755 --- a/bin/stop-harmony-and-services +++ b/bin/stop-harmony-and-services @@ -2,6 +2,10 @@ # This script will delete all of the kubernetes harmony resources. If you use this script # you will also delete all of your local harmony jobs since the database will be destroyed. +# this will only have an effect if the development servies are running - otherwise it does +# nothing +bin/stop-dev-services + current_context=$(kubectl config current-context) if [ "$current_context" != "colima" ] && [ "$current_context" != "docker-desktop" ] && [ "$current_context" != "minikube" ]; then echo 'ERROR: Attempting to use a non-local k8s context while deleting harmony resources.' diff --git a/db/db.sql b/db/db.sql index 7928e8589..681993497 100644 --- a/db/db.sql +++ b/db/db.sql @@ -46,7 +46,7 @@ CREATE TABLE `job_errors` ( FOREIGN KEY(jobID) REFERENCES jobs(jobID) ); -CREATE TABLE `user_labels` ( +CREATE TABLE `labels` ( `id` integer not null primary key autoincrement, `username` varchar(255) not null, `value` varchar(255) not null, @@ -55,14 +55,14 @@ CREATE TABLE `user_labels` ( UNIQUE(username, value) ); -CREATE TABLE `labels` ( +CREATE TABLE `jobs_labels` ( `id` integer not null primary key autoincrement, `job_id` char(36) not null, - `user_label_id` integer not null, + `label_id` integer not null, `createdAt` datetime not null, `updatedAt` datetime not null, - FOREIGN KEY(user_label_id) REFERENCES user_labels(id) FOREIGN KEY(job_id) REFERENCES jobs(jobID) + FOREIGN KEY(label_id) REFERENCES labels(id) ); CREATE TABLE `work_items` ( diff --git a/db/migrations/20240925182705_add_user_labels_table.js b/db/migrations/20240925182705_add_labels_table.js similarity index 84% rename from db/migrations/20240925182705_add_user_labels_table.js rename to db/migrations/20240925182705_add_labels_table.js index 50291cc04..b80f19093 100644 --- a/db/migrations/20240925182705_add_user_labels_table.js +++ b/db/migrations/20240925182705_add_labels_table.js @@ -4,7 +4,7 @@ */ exports.up = function (knex) { return knex.schema - .createTable('user_labels', (t) => { + .createTable('labels', (t) => { t.increments('id').primary(); t.string('username', 255).notNullable(); t.string('value', 255).notNullable(); @@ -12,8 +12,9 @@ exports.up = function (knex) { t.timestamp('updatedAt').notNullable(); t.unique(['username', 'value']); t.index(['username']); + t.index(['value']); }).raw(` - ALTER TABLE "user_labels" + ALTER TABLE "labels" ADD CONSTRAINT "lower_case_value" CHECK (value = lower(value)) `); @@ -25,5 +26,5 @@ exports.up = function (knex) { */ exports.down = function(knex) { return knex.schema - .dropTable('user_labels'); + .dropTable('labels'); }; diff --git a/db/migrations/20240925182722_add_labels_table.js b/db/migrations/20240925182722_add_jobs_labels_table.js similarity index 77% rename from db/migrations/20240925182722_add_labels_table.js rename to db/migrations/20240925182722_add_jobs_labels_table.js index da83eaa69..56726bde7 100644 --- a/db/migrations/20240925182722_add_labels_table.js +++ b/db/migrations/20240925182722_add_jobs_labels_table.js @@ -3,7 +3,7 @@ * @returns { Promise } */ exports.up = function (knex) { - return knex.schema.createTable('labels', (t) => { + return knex.schema.createTable('jobs_labels', (t) => { t.increments('id') .primary(); @@ -13,10 +13,10 @@ exports.up = function (knex) { .inTable('jobs') .onDelete('CASCADE'); - t.integer('user_label_id', 255) + t.integer('label_id') .notNullable() .references('id') - .inTable('user_labels'); + .inTable('labels'); t.timestamp('createdAt') .notNullable(); @@ -25,6 +25,7 @@ exports.up = function (knex) { .notNullable(); t.index(['job_id']); + t.index(['label_id']); }); }; @@ -34,5 +35,5 @@ exports.up = function (knex) { */ exports.down = function(knex) { return knex.schema - .dropTable('labels'); + .dropTable('jobs_labels'); }; diff --git a/services/harmony/app/models/label.ts b/services/harmony/app/models/label.ts index 9924bd673..3b331b44d 100644 --- a/services/harmony/app/models/label.ts +++ b/services/harmony/app/models/label.ts @@ -11,11 +11,11 @@ export async function getLabelsForJob( transaction: Transaction, jobID: string, ): Promise { - const query = transaction('labels') + const query = transaction('jobs_labels') .where({ job_id: jobID }) - .orderBy(['labels.id']) - .innerJoin('user_labels', 'labels.user_label_id', '=', 'user_labels.id') - .select(['user_labels.value']); + .orderBy(['jobs_labels.id']) + .innerJoin('labels', 'jobs_labels.label_id', '=', 'labels.id') + .select(['labels.value']); const rows = await query; @@ -41,28 +41,30 @@ export async function setLabelsForJob( if (!labels) return; // delete any labels that already exist for the job - await transaction('labels') + await transaction('jobs_labels') .where({ job_id: jobID }) .delete(); if (labels.length > 0) { const now = new Date(); const lowerCaseLabels = labels.map((label) => label.toLowerCase()); - const userLabelRows = lowerCaseLabels.map((label) => { + const labelRows = lowerCaseLabels.map((label) => { return { username, value: label, createdAt: now, updatedAt: now }; }); - const insertedRows = await transaction('user_labels') - .insert(userLabelRows) + // this will insert the labels - if a label already exists for a given user + // it will just update the `updatedAt` timestamp + const insertedRows = await transaction('labels') + .insert(labelRows) .returning('id') .onConflict(['username', 'value']) .merge(['updatedAt']); const ids = insertedRows.map((row) => row.id); - const labelRows = ids.map((id) => { - return { job_id: jobID, user_label_id: id, createdAt: now, updatedAt: now }; + const jobsLabelRows = ids.map((id) => { + return { job_id: jobID, label_id: id, createdAt: now, updatedAt: now }; }); - await transaction('labels').insert(labelRows); + await transaction('jobs_labels').insert(jobsLabelRows); } } \ No newline at end of file