Skip to content

Commit

Permalink
Merge pull request #30 from Mr-Bean-NIP-Project/29-support-decimals-i…
Browse files Browse the repository at this point in the history
…n-number-columns

[#29] add support for decimals in quantity, update migration script
  • Loading branch information
Choo-Xing-Yu authored May 6, 2023
2 parents 6fb302a + 2abc2cf commit 2d592d7
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Init1682756631395 implements MigrationInterface {
name = 'Init1682756631395'
export class Init1683337224166 implements MigrationInterface {
name = 'Init1683337224166'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "serving_size" integer NOT NULL, "serving_unit" varchar CHECK( "serving_unit" IN ('g','ml','mg','kcal') ) NOT NULL, "serving_per_package" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_22cc43e9a74d7498546e9a63e77" UNIQUE ("name"))`);
await queryRunner.query(`CREATE TABLE "material_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "material_id" integer NOT NULL, "product_id" integer NOT NULL, "material_quantity" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`CREATE TABLE "product_sub_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "parent_id" integer NOT NULL, "child_id" integer NOT NULL, "quantity" decimal(10,2) NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968" ON "product_sub_product" ("child_id", "parent_id") `);
await queryRunner.query(`CREATE TABLE "product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "serving_size" decimal(10,2) NOT NULL, "serving_unit" varchar CHECK( "serving_unit" IN ('g','ml','mg','kcal') ) NOT NULL, "serving_per_package" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_22cc43e9a74d7498546e9a63e77" UNIQUE ("name"))`);
await queryRunner.query(`CREATE TABLE "material_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "material_id" integer NOT NULL, "product_id" integer NOT NULL, "material_quantity" decimal(10,2) NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_3388e717d2105465c59edcf13b" ON "material_product" ("material_id", "product_id") `);
await queryRunner.query(`CREATE TABLE "supplier" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_05290e39dd1ef4fbbcfe329f7bd" UNIQUE ("name"))`);
await queryRunner.query(`CREATE TABLE "material" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "energy" varchar NOT NULL DEFAULT ('0'), "protein" varchar NOT NULL DEFAULT ('0'), "total_fat" varchar NOT NULL DEFAULT ('0'), "saturated_fat" varchar NOT NULL DEFAULT ('0'), "trans_fat" varchar NOT NULL DEFAULT ('0'), "cholesterol" varchar NOT NULL DEFAULT ('0'), "carbohydrate" varchar NOT NULL DEFAULT ('0'), "sugars" varchar NOT NULL DEFAULT ('0'), "dietary_fibre" varchar NOT NULL DEFAULT ('0'), "sodium" varchar NOT NULL DEFAULT ('0'), "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), "supplierId" integer, CONSTRAINT "UQ_f7e289dde0c4fdfba5bee2de40b" UNIQUE ("name"))`);
await queryRunner.query(`CREATE TABLE "product_sub_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "parent_id" integer NOT NULL, "child_id" integer NOT NULL, "quantity" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`DROP INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968"`);
await queryRunner.query(`CREATE TABLE "temporary_product_sub_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "parent_id" integer NOT NULL, "child_id" integer NOT NULL, "quantity" decimal(10,2) NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_a857e2eab179d3033cd7e688f57" FOREIGN KEY ("parent_id") REFERENCES "product" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_e7d536e5d360d61ebd24b2a597e" FOREIGN KEY ("child_id") REFERENCES "product" ("id") ON DELETE CASCADE ON UPDATE CASCADE)`);
await queryRunner.query(`INSERT INTO "temporary_product_sub_product"("id", "parent_id", "child_id", "quantity", "created_at", "updated_at") SELECT "id", "parent_id", "child_id", "quantity", "created_at", "updated_at" FROM "product_sub_product"`);
await queryRunner.query(`DROP TABLE "product_sub_product"`);
await queryRunner.query(`ALTER TABLE "temporary_product_sub_product" RENAME TO "product_sub_product"`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968" ON "product_sub_product" ("child_id", "parent_id") `);
await queryRunner.query(`DROP INDEX "IDX_3388e717d2105465c59edcf13b"`);
await queryRunner.query(`CREATE TABLE "temporary_material_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "material_id" integer NOT NULL, "product_id" integer NOT NULL, "material_quantity" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_b4f92677ba1ae798c1971908412" FOREIGN KEY ("material_id") REFERENCES "material" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_317e70d8f17cc0c17a74b0f3a46" FOREIGN KEY ("product_id") REFERENCES "product" ("id") ON DELETE CASCADE ON UPDATE CASCADE)`);
await queryRunner.query(`CREATE TABLE "temporary_material_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "material_id" integer NOT NULL, "product_id" integer NOT NULL, "material_quantity" decimal(10,2) NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_b4f92677ba1ae798c1971908412" FOREIGN KEY ("material_id") REFERENCES "material" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_317e70d8f17cc0c17a74b0f3a46" FOREIGN KEY ("product_id") REFERENCES "product" ("id") ON DELETE CASCADE ON UPDATE CASCADE)`);
await queryRunner.query(`INSERT INTO "temporary_material_product"("id", "material_id", "product_id", "material_quantity", "created_at", "updated_at") SELECT "id", "material_id", "product_id", "material_quantity", "created_at", "updated_at" FROM "material_product"`);
await queryRunner.query(`DROP TABLE "material_product"`);
await queryRunner.query(`ALTER TABLE "temporary_material_product" RENAME TO "material_product"`);
Expand All @@ -21,38 +27,32 @@ export class Init1682756631395 implements MigrationInterface {
await queryRunner.query(`INSERT INTO "temporary_material"("id", "name", "energy", "protein", "total_fat", "saturated_fat", "trans_fat", "cholesterol", "carbohydrate", "sugars", "dietary_fibre", "sodium", "created_at", "updated_at", "supplierId") SELECT "id", "name", "energy", "protein", "total_fat", "saturated_fat", "trans_fat", "cholesterol", "carbohydrate", "sugars", "dietary_fibre", "sodium", "created_at", "updated_at", "supplierId" FROM "material"`);
await queryRunner.query(`DROP TABLE "material"`);
await queryRunner.query(`ALTER TABLE "temporary_material" RENAME TO "material"`);
await queryRunner.query(`DROP INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968"`);
await queryRunner.query(`CREATE TABLE "temporary_product_sub_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "parent_id" integer NOT NULL, "child_id" integer NOT NULL, "quantity" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "FK_a857e2eab179d3033cd7e688f57" FOREIGN KEY ("parent_id") REFERENCES "product" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_e7d536e5d360d61ebd24b2a597e" FOREIGN KEY ("child_id") REFERENCES "product" ("id") ON DELETE CASCADE ON UPDATE CASCADE)`);
await queryRunner.query(`INSERT INTO "temporary_product_sub_product"("id", "parent_id", "child_id", "quantity", "created_at", "updated_at") SELECT "id", "parent_id", "child_id", "quantity", "created_at", "updated_at" FROM "product_sub_product"`);
await queryRunner.query(`DROP TABLE "product_sub_product"`);
await queryRunner.query(`ALTER TABLE "temporary_product_sub_product" RENAME TO "product_sub_product"`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968" ON "product_sub_product" ("child_id", "parent_id") `);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968"`);
await queryRunner.query(`ALTER TABLE "product_sub_product" RENAME TO "temporary_product_sub_product"`);
await queryRunner.query(`CREATE TABLE "product_sub_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "parent_id" integer NOT NULL, "child_id" integer NOT NULL, "quantity" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`INSERT INTO "product_sub_product"("id", "parent_id", "child_id", "quantity", "created_at", "updated_at") SELECT "id", "parent_id", "child_id", "quantity", "created_at", "updated_at" FROM "temporary_product_sub_product"`);
await queryRunner.query(`DROP TABLE "temporary_product_sub_product"`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968" ON "product_sub_product" ("child_id", "parent_id") `);
await queryRunner.query(`ALTER TABLE "material" RENAME TO "temporary_material"`);
await queryRunner.query(`CREATE TABLE "material" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "energy" varchar NOT NULL DEFAULT ('0'), "protein" varchar NOT NULL DEFAULT ('0'), "total_fat" varchar NOT NULL DEFAULT ('0'), "saturated_fat" varchar NOT NULL DEFAULT ('0'), "trans_fat" varchar NOT NULL DEFAULT ('0'), "cholesterol" varchar NOT NULL DEFAULT ('0'), "carbohydrate" varchar NOT NULL DEFAULT ('0'), "sugars" varchar NOT NULL DEFAULT ('0'), "dietary_fibre" varchar NOT NULL DEFAULT ('0'), "sodium" varchar NOT NULL DEFAULT ('0'), "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), "supplierId" integer, CONSTRAINT "UQ_f7e289dde0c4fdfba5bee2de40b" UNIQUE ("name"))`);
await queryRunner.query(`INSERT INTO "material"("id", "name", "energy", "protein", "total_fat", "saturated_fat", "trans_fat", "cholesterol", "carbohydrate", "sugars", "dietary_fibre", "sodium", "created_at", "updated_at", "supplierId") SELECT "id", "name", "energy", "protein", "total_fat", "saturated_fat", "trans_fat", "cholesterol", "carbohydrate", "sugars", "dietary_fibre", "sodium", "created_at", "updated_at", "supplierId" FROM "temporary_material"`);
await queryRunner.query(`DROP TABLE "temporary_material"`);
await queryRunner.query(`DROP INDEX "IDX_3388e717d2105465c59edcf13b"`);
await queryRunner.query(`ALTER TABLE "material_product" RENAME TO "temporary_material_product"`);
await queryRunner.query(`CREATE TABLE "material_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "material_id" integer NOT NULL, "product_id" integer NOT NULL, "material_quantity" integer NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`CREATE TABLE "material_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "material_id" integer NOT NULL, "product_id" integer NOT NULL, "material_quantity" decimal(10,2) NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`INSERT INTO "material_product"("id", "material_id", "product_id", "material_quantity", "created_at", "updated_at") SELECT "id", "material_id", "product_id", "material_quantity", "created_at", "updated_at" FROM "temporary_material_product"`);
await queryRunner.query(`DROP TABLE "temporary_material_product"`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_3388e717d2105465c59edcf13b" ON "material_product" ("material_id", "product_id") `);
await queryRunner.query(`DROP INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968"`);
await queryRunner.query(`DROP TABLE "product_sub_product"`);
await queryRunner.query(`ALTER TABLE "product_sub_product" RENAME TO "temporary_product_sub_product"`);
await queryRunner.query(`CREATE TABLE "product_sub_product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "parent_id" integer NOT NULL, "child_id" integer NOT NULL, "quantity" decimal(10,2) NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
await queryRunner.query(`INSERT INTO "product_sub_product"("id", "parent_id", "child_id", "quantity", "created_at", "updated_at") SELECT "id", "parent_id", "child_id", "quantity", "created_at", "updated_at" FROM "temporary_product_sub_product"`);
await queryRunner.query(`DROP TABLE "temporary_product_sub_product"`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968" ON "product_sub_product" ("child_id", "parent_id") `);
await queryRunner.query(`DROP TABLE "material"`);
await queryRunner.query(`DROP TABLE "supplier"`);
await queryRunner.query(`DROP INDEX "IDX_3388e717d2105465c59edcf13b"`);
await queryRunner.query(`DROP TABLE "material_product"`);
await queryRunner.query(`DROP TABLE "product"`);
await queryRunner.query(`DROP INDEX "IDX_de9e9ce9cfd69a1fd0d57f7968"`);
await queryRunner.query(`DROP TABLE "product_sub_product"`);
}

}
2 changes: 1 addition & 1 deletion src/product/entities/material_product.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MaterialProduct {
@Column()
product_id?: number;

@Column()
@Column({ type: 'decimal', precision: 10, scale: 2 })
material_quantity: number;

@CreateDateColumn()
Expand Down
2 changes: 1 addition & 1 deletion src/product/entities/product.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Product {
@Column()
name: string;

@Column()
@Column({ type: 'decimal', precision: 10, scale: 2 })
serving_size: number;

@Column({
Expand Down
2 changes: 1 addition & 1 deletion src/product/entities/product_sub_product.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ProductSubProduct {
@Column()
child_id?: number;

@Column()
@Column({ type: 'decimal', precision: 10, scale: 2 })
quantity: number;

@CreateDateColumn()
Expand Down
Loading

0 comments on commit 2d592d7

Please sign in to comment.