From 45fe1076e7575115c1337b049128054f229750d0 Mon Sep 17 00:00:00 2001 From: almog8k Date: Sun, 22 Dec 2024 14:21:15 +0200 Subject: [PATCH 1/2] feat(db): make `percentage` column NOT NULL and add DEFAULT 0 BREAKING CHANGE: - The `percentage` column is now NOT NULL. This will cause failures in any code attempting to insert or update rows with a null value for `percentage`. - Existing data has been migrated to set null values to 0. - A DEFAULT constraint of 0 has been added, altering the behavior of inserts when `percentage` is not specified. Applications relying on nullable `percentage` values or handling null explicitly must be updated to comply with these changes. --- src/DAL/entity/job.ts | 2 +- src/DAL/entity/task.ts | 2 +- src/DAL/migration/FullSchema.sql | 4 ++-- src/DAL/migration/v.2.10.2.sql | 24 ++++++++++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/DAL/migration/v.2.10.2.sql diff --git a/src/DAL/entity/job.ts b/src/DAL/entity/job.ts index c7a9fdb..142e442 100644 --- a/src/DAL/entity/job.ts +++ b/src/DAL/entity/job.ts @@ -41,7 +41,7 @@ export class JobEntity { @Column({ type: 'enum', enum: OperationStatus, default: OperationStatus.PENDING, nullable: false }) public status: OperationStatus; - @Column('smallint', { nullable: true }) + @Column('smallint', { nullable: false, default: 0 }) public percentage: number; @Column('varchar', { default: '', nullable: false }) diff --git a/src/DAL/entity/task.ts b/src/DAL/entity/task.ts index 7c62cac..3f1f1ba 100644 --- a/src/DAL/entity/task.ts +++ b/src/DAL/entity/task.ts @@ -38,7 +38,7 @@ export class TaskEntity { @Column({ type: 'enum', enum: OperationStatus, default: OperationStatus.PENDING, nullable: false }) public status: OperationStatus; - @Column('smallint', { nullable: true }) + @Column('smallint', { nullable: false, default: 0 }) public percentage: number; @Column('varchar', { default: '', nullable: false }) diff --git a/src/DAL/migration/FullSchema.sql b/src/DAL/migration/FullSchema.sql index a12ed0d..3d7651d 100644 --- a/src/DAL/migration/FullSchema.sql +++ b/src/DAL/migration/FullSchema.sql @@ -19,7 +19,7 @@ CREATE TABLE "Job" "creationTime" timestamp with time zone NOT NULL DEFAULT now(), "updateTime" timestamp with time zone NOT NULL DEFAULT now(), "status" "operation_status_enum" NOT NULL DEFAULT 'Pending'::"operation_status_enum", - "percentage" smallint, + "percentage" smallint NOT NULL DEFAULT 0, "reason" text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text, "isCleaned" boolean NOT NULL DEFAULT false, "priority" int NOT NULL DEFAULT 1000, @@ -79,7 +79,7 @@ CREATE TABLE "Task" "creationTime" timestamp with time zone NOT NULL DEFAULT now(), "updateTime" timestamp with time zone NOT NULL DEFAULT now(), "status" "operation_status_enum" NOT NULL DEFAULT 'Pending'::"operation_status_enum", - "percentage" smallint, + "percentage" smallint NOT NULL DEFAULT 0, "reason" text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text, "attempts" integer NOT NULL DEFAULT 0, "jobId" uuid NOT NULL, diff --git a/src/DAL/migration/v.2.10.2.sql b/src/DAL/migration/v.2.10.2.sql new file mode 100644 index 0000000..f0138d5 --- /dev/null +++ b/src/DAL/migration/v.2.10.2.sql @@ -0,0 +1,24 @@ +SET search_path TO "JobManager", public; -- CHANGE SCHEMA NAME TO MATCH ENVIRONMENT + +-- Update existing null values to 0 in Job table +UPDATE "Job" +SET "percentage" = 0 +WHERE "percentage" IS NULL; + +-- Update existing null values to 0 in Task table +UPDATE "Task" +SET "percentage" = 0 +WHERE "percentage" IS NULL; + +-- Modify Job table column +ALTER TABLE "Job" +ALTER COLUMN "percentage" SET DEFAULT 0, +ALTER COLUMN "percentage" SET NOT NULL; + +-- Modify Task table column +ALTER TABLE "Task" +ALTER COLUMN "percentage" SET DEFAULT 0, +ALTER COLUMN "percentage" SET NOT NULL; + +COMMENT ON COLUMN "Job"."percentage" IS 'Percentage of job completion (0-100). Default is 0.'; +COMMENT ON COLUMN "Task"."percentage" IS 'Percentage of task completion (0-100). Default is 0.'; \ No newline at end of file From 879d1c06f242289ec5e9bb4996fb7f438735f126 Mon Sep 17 00:00:00 2001 From: almog8k Date: Sun, 22 Dec 2024 14:52:12 +0200 Subject: [PATCH 2/2] chore(db):migration script version change --- src/DAL/migration/{v.2.10.2.sql => v.3.0.0.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/DAL/migration/{v.2.10.2.sql => v.3.0.0.sql} (100%) diff --git a/src/DAL/migration/v.2.10.2.sql b/src/DAL/migration/v.3.0.0.sql similarity index 100% rename from src/DAL/migration/v.2.10.2.sql rename to src/DAL/migration/v.3.0.0.sql