Skip to content

Commit

Permalink
feat(db): make percentage column NOT NULL and add DEFAULT 0 (#50)
Browse files Browse the repository at this point in the history
* 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.

* chore(db):migration script version change
  • Loading branch information
almog8k authored Dec 22, 2024
1 parent 349a291 commit 2690186
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/DAL/entity/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion src/DAL/entity/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
4 changes: 2 additions & 2 deletions src/DAL/migration/FullSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/DAL/migration/v.3.0.0.sql
Original file line number Diff line number Diff line change
@@ -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.';

0 comments on commit 2690186

Please sign in to comment.