Skip to content

Commit

Permalink
Change create_at and updated_at to TIMESTAMP WITH TIME ZONE.
Browse files Browse the repository at this point in the history
  • Loading branch information
antjoseb27 committed May 11, 2024
1 parent e4e9de2 commit 699b4e1
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 45 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/bcrypt": "^5.0.2",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
Expand Down
157 changes: 157 additions & 0 deletions prisma/migrations/20240511135945_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Warnings:
- The `created_at` column on the `category_supplies` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `updated_at` column on the `category_supplies` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `created_at` column on the `sessions` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `updated_at` column on the `sessions` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `created_at` column on the `shelter_managers` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `updated_at` column on the `shelter_managers` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `created_at` column on the `shelter_supplies` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `updated_at` column on the `shelter_supplies` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `created_at` column on the `shelters` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `updated_at` column on the `shelters` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `created_at` column on the `supplies` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `updated_at` column on the `supplies` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `created_at` column on the `users` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `updated_at` column on the `users` table would be dropped and recreated. This will lead to data loss if there is data in the column.
*/
-- AlterTable
ALTER TABLE "category_supplies" ADD COLUMN "created_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "category_supplies"
SET "created_at_tmp" = CASE
WHEN "created_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "created_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "category_supplies" DROP COLUMN "created_at";
ALTER TABLE "category_supplies" RENAME COLUMN "created_at_tmp" TO "created_at";

-- AlterTable
ALTER TABLE "category_supplies" ADD COLUMN "updated_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "category_supplies"
SET "updated_at_tmp" = CASE
WHEN "updated_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "updated_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "category_supplies" DROP COLUMN "updated_at";
ALTER TABLE "category_supplies" RENAME COLUMN "updated_at_tmp" TO "updated_at";

-- AlterTable
ALTER TABLE "sessions" ADD COLUMN "created_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "sessions"
SET "created_at_tmp" = CASE
WHEN "created_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "created_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "sessions" DROP COLUMN "created_at";
ALTER TABLE "sessions" RENAME COLUMN "created_at_tmp" TO "created_at";

-- AlterTable
ALTER TABLE "sessions" ADD COLUMN "updated_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "sessions"
SET "updated_at_tmp" = CASE
WHEN "updated_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "updated_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "sessions" DROP COLUMN "updated_at";
ALTER TABLE "sessions" RENAME COLUMN "updated_at_tmp" TO "updated_at";

-- AlterTable
ALTER TABLE "shelter_managers" ADD COLUMN "created_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "shelter_managers"
SET "created_at_tmp" = CASE
WHEN "created_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "created_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "shelter_managers" DROP COLUMN "created_at";
ALTER TABLE "shelter_managers" RENAME COLUMN "created_at_tmp" TO "created_at";

-- AlterTable
ALTER TABLE "shelter_managers" ADD COLUMN "updated_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "shelter_managers"
SET "updated_at_tmp" = CASE
WHEN "updated_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "updated_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "shelter_managers" DROP COLUMN "updated_at";
ALTER TABLE "shelter_managers" RENAME COLUMN "updated_at_tmp" TO "updated_at";

-- AlterTable
ALTER TABLE "shelter_supplies" ADD COLUMN "created_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "shelter_supplies"
SET "created_at_tmp" = CASE
WHEN "created_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "created_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "shelter_supplies" DROP COLUMN "created_at";
ALTER TABLE "shelter_supplies" RENAME COLUMN "created_at_tmp" TO "created_at";


ALTER TABLE "shelter_supplies" ADD COLUMN "updated_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "shelter_supplies"
SET "updated_at_tmp" = CASE
WHEN "updated_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "updated_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "shelter_supplies" DROP COLUMN "updated_at";
ALTER TABLE "shelter_supplies" RENAME COLUMN "updated_at_tmp" TO "updated_at";

-- AlterTable
ALTER TABLE "shelters" ADD COLUMN "created_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "shelters"
SET "created_at_tmp" = CASE
WHEN "created_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "created_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "shelters" DROP COLUMN "created_at";
ALTER TABLE "shelters" RENAME COLUMN "created_at_tmp" TO "created_at";

-- AlterTable
ALTER TABLE "shelters" ADD COLUMN "updated_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "shelters"
SET "updated_at_tmp" = CASE
WHEN "updated_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "updated_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "shelters" DROP COLUMN "updated_at";
ALTER TABLE "shelters" RENAME COLUMN "updated_at_tmp" TO "updated_at";

-- AlterTable
ALTER TABLE "supplies" ADD COLUMN "created_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "supplies"
SET "created_at_tmp" = CASE
WHEN "created_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "created_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "supplies" DROP COLUMN "created_at";
ALTER TABLE "supplies" RENAME COLUMN "created_at_tmp" TO "created_at";

-- AlterTable
ALTER TABLE "supplies" ADD COLUMN "updated_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "supplies"
SET "updated_at_tmp" = CASE
WHEN "updated_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "updated_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "supplies" DROP COLUMN "updated_at";
ALTER TABLE "supplies" RENAME COLUMN "updated_at_tmp" TO "updated_at";

-- AlterTable
ALTER TABLE "users" ADD COLUMN "created_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "users"
SET "created_at_tmp" = CASE
WHEN "created_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "created_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "users" DROP COLUMN "created_at";
ALTER TABLE "users" RENAME COLUMN "created_at_tmp" TO "created_at";

ALTER TABLE "users" ADD COLUMN "updated_at_tmp" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
UPDATE "users"
SET "updated_at_tmp" = CASE
WHEN "updated_at" ~* '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$' THEN "updated_at"::timestamp
ELSE NOW()
END;
ALTER TABLE "users" DROP COLUMN "updated_at";
ALTER TABLE "users" RENAME COLUMN "updated_at_tmp" TO "updated_at";
32 changes: 16 additions & 16 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ model User {
password String
phone String @unique
accessLevel AccessLevel @default(value: User)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
createdAt DateTime? @default(value: now()) @map("created_at")
updatedAt DateTime? @map("updated_at")
sessions Session[]
shelterManagers ShelterManagers[]
Expand All @@ -37,8 +37,8 @@ model Session {
ip String?
userAgent String? @map("user_agent")
active Boolean @default(value: true)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
createdAt DateTime? @default(value: now()) @map("created_at")
updatedAt DateTime? @map("updated_at")
user User @relation(fields: [userId], references: [id])
Expand All @@ -48,8 +48,8 @@ model Session {
model SupplyCategory {
id String @id @default(uuid())
name String @unique
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
createdAt DateTime? @default(value: now()) @map("created_at")
updatedAt DateTime? @map("updated_at")
supplies Supply[]
Expand All @@ -61,8 +61,8 @@ model ShelterSupply {
supplyId String @map("supply_id")
priority Int @default(value: 0)
quantity Int?
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
createdAt DateTime? @default(value: now()) @map("created_at")
updatedAt DateTime? @map("updated_at")
shelter Shelter @relation(fields: [shelterId], references: [id])
supply Supply @relation(fields: [supplyId], references: [id])
Expand All @@ -75,8 +75,8 @@ model Supply {
id String @id @default(uuid())
supplyCategoryId String @map("supply_category_id")
name String
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
createdAt DateTime? @default(value: now()) @map("created_at")
updatedAt DateTime? @map("updated_at")
supplyCategory SupplyCategory @relation(fields: [supplyCategoryId], references: [id])
shelterSupplies ShelterSupply[]
Expand All @@ -97,8 +97,8 @@ model Shelter {
latitude Float?
longitude Float?
verified Boolean @default(value: false)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
createdAt DateTime? @default(value: now()) @map("created_at")
updatedAt DateTime? @map("updated_at")
shelterManagers ShelterManagers[]
shelterSupplies ShelterSupply[]
Expand All @@ -107,10 +107,10 @@ model Shelter {
}

model ShelterManagers {
shelterId String @map("shelter_id")
userId String @map("user_id")
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
shelterId String @map("shelter_id")
userId String @map("user_id")
createdAt DateTime? @default(value: now()) @map("created_at")
updatedAt DateTime? @map("updated_at")
user User @relation(fields: [userId], references: [id])
shelter Shelter @relation(fields: [shelterId], references: [id])
Expand Down
6 changes: 3 additions & 3 deletions src/sessions/sessions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SessionsService {
},
data: {
active: false,
updatedAt: new Date().toISOString(),
updatedAt: new Date(),
},
});

Expand All @@ -36,7 +36,7 @@ export class SessionsService {
userId: user.id,
ip,
userAgent,
createdAt: new Date().toISOString(),
createdAt: new Date(),
},
});

Expand Down Expand Up @@ -73,7 +73,7 @@ export class SessionsService {
userId,
},
data: {
updatedAt: new Date().toISOString(),
updatedAt: new Date(),
active: false,
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/shelter-managers/shelter-managers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ShelterManagersService {
data: {
shelterId,
userId,
createdAt: new Date().toISOString(),
createdAt: new Date(),
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/shelter-managers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import z from 'zod';
const ShelterManagerSchema = z.object({
shelterId: z.string(),
userId: z.string(),
createdAt: z.string(),
updatedAt: z.string().optional().nullable(),
createdAt: z.date(),
updatedAt: z.date().optional().nullable(),
});

const CreateShelterManagerSchema = ShelterManagerSchema.pick({
Expand Down
8 changes: 4 additions & 4 deletions src/shelter-supply/shelter-supply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class ShelterSupplyService {
prioritySum: {
increment: newPriority - oldPriority,
},
updatedAt: new Date().toISOString(),
updatedAt: new Date(),
},
});
}
Expand All @@ -41,7 +41,7 @@ export class ShelterSupplyService {
priority,
supplyId,
quantity: priority !== SupplyPriority.UnderControl ? quantity : null,
createdAt: new Date().toISOString(),
createdAt: new Date(),
},
});
}
Expand Down Expand Up @@ -74,7 +74,7 @@ export class ShelterSupplyService {
data: {
...data,
quantity: priority !== SupplyPriority.UnderControl ? quantity : null,
updatedAt: new Date().toISOString(),
updatedAt: new Date(),
},
});
}
Expand All @@ -91,7 +91,7 @@ export class ShelterSupplyService {
},
data: {
priority: SupplyPriority.UnderControl,
updatedAt: new Date().toISOString(),
updatedAt: new Date(),
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/shelter-supply/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const ShelterSupplySchema = z.object({
z.literal(SupplyPriority.Urgent),
]),
quantity: z.number().gt(0).nullable().optional(),
createdAt: z.string(),
updatedAt: z.string().nullable().optional(),
createdAt: z.date(),
updatedAt: z.date().nullable().optional(),
});

const CreateShelterSupplySchema = ShelterSupplySchema.pick({
Expand Down
6 changes: 3 additions & 3 deletions src/shelter/shelter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ShelterService {
await this.prismaService.shelter.create({
data: {
...payload,
createdAt: new Date().toISOString(),
createdAt: new Date(),
},
});
}
Expand All @@ -42,7 +42,7 @@ export class ShelterService {
},
data: {
...payload,
updatedAt: new Date().toISOString(),
updatedAt: new Date(),
},
});
}
Expand All @@ -55,7 +55,7 @@ export class ShelterService {
},
data: {
...payload,
updatedAt: new Date().toISOString(),
updatedAt: new Date(),
},
});
}
Expand Down
Loading

0 comments on commit 699b4e1

Please sign in to comment.