Skip to content

Commit

Permalink
HARMONY-1894: Renamed labels tables to better reflect the relationship
Browse files Browse the repository at this point in the history
to the jobs table and some other cleanup
  • Loading branch information
indiejames committed Sep 26, 2024
1 parent c6991c3 commit 0f40f72
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
4 changes: 4 additions & 0 deletions bin/stop-harmony-and-services
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
8 changes: 4 additions & 4 deletions db/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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` (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
*/
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();
t.timestamp('createdAt').notNullable();
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))
`);
Expand All @@ -25,5 +26,5 @@ exports.up = function (knex) {
*/
exports.down = function(knex) {
return knex.schema
.dropTable('user_labels');
.dropTable('labels');
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.createTable('labels', (t) => {
return knex.schema.createTable('jobs_labels', (t) => {
t.increments('id')
.primary();

Expand All @@ -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();
Expand All @@ -25,6 +25,7 @@ exports.up = function (knex) {
.notNullable();

t.index(['job_id']);
t.index(['label_id']);
});
};

Expand All @@ -34,5 +35,5 @@ exports.up = function (knex) {
*/
exports.down = function(knex) {
return knex.schema
.dropTable('labels');
.dropTable('jobs_labels');
};
24 changes: 13 additions & 11 deletions services/harmony/app/models/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export async function getLabelsForJob(
transaction: Transaction,
jobID: string,
): Promise<string[]> {
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;

Expand All @@ -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);
}
}

0 comments on commit 0f40f72

Please sign in to comment.