fix(server): change the createdAt and modifiedAt to the correct type in database (#591)

* Added migration files

* Remove type casting in sql query
This commit is contained in:
Alex 2022-09-05 20:51:01 -05:00 committed by GitHub
parent 7f6837c751
commit b081eda76f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 13 deletions

View File

@ -43,7 +43,7 @@ export class AssetRepository implements IAssetRepository {
return await this.assetRepository return await this.assetRepository
.createQueryBuilder('asset') .createQueryBuilder('asset')
.where('asset.userId = :userId', { userId: userId }) .where('asset.userId = :userId', { userId: userId })
.andWhere(`date_trunc('month', "createdAt"::timestamptz) IN (:...buckets)`, { .andWhere(`date_trunc('month', "createdAt") IN (:...buckets)`, {
buckets: [...getAssetByTimeBucketDto.timeBucket], buckets: [...getAssetByTimeBucketDto.timeBucket],
}) })
.andWhere('asset.resizePath is not NULL') .andWhere('asset.resizePath is not NULL')
@ -58,19 +58,19 @@ export class AssetRepository implements IAssetRepository {
result = await this.assetRepository result = await this.assetRepository
.createQueryBuilder('asset') .createQueryBuilder('asset')
.select(`COUNT(asset.id)::int`, 'count') .select(`COUNT(asset.id)::int`, 'count')
.addSelect(`date_trunc('month', "createdAt"::timestamptz)`, 'timeBucket') .addSelect(`date_trunc('month', "createdAt")`, 'timeBucket')
.where('"userId" = :userId', { userId: userId }) .where('"userId" = :userId', { userId: userId })
.groupBy(`date_trunc('month', "createdAt"::timestamptz)`) .groupBy(`date_trunc('month', "createdAt")`)
.orderBy(`date_trunc('month', "createdAt"::timestamptz)`, 'DESC') .orderBy(`date_trunc('month', "createdAt")`, 'DESC')
.getRawMany(); .getRawMany();
} else if (timeBucket === TimeGroupEnum.Day) { } else if (timeBucket === TimeGroupEnum.Day) {
result = await this.assetRepository result = await this.assetRepository
.createQueryBuilder('asset') .createQueryBuilder('asset')
.select(`COUNT(asset.id)::int`, 'count') .select(`COUNT(asset.id)::int`, 'count')
.addSelect(`date_trunc('day', "createdAt"::timestamptz)`, 'timeBucket') .addSelect(`date_trunc('day', "createdAt")`, 'timeBucket')
.where('"userId" = :userId', { userId: userId }) .where('"userId" = :userId', { userId: userId })
.groupBy(`date_trunc('day', "createdAt"::timestamptz)`) .groupBy(`date_trunc('day', "createdAt")`)
.orderBy(`date_trunc('day', "createdAt"::timestamptz)`, 'DESC') .orderBy(`date_trunc('day', "createdAt")`, 'DESC')
.getRawMany(); .getRawMany();
} }
@ -212,15 +212,15 @@ export class AssetRepository implements IAssetRepository {
/** /**
* Get asset by checksum on the database * Get asset by checksum on the database
* @param userId * @param userId
* @param checksum * @param checksum
* *
*/ */
getAssetByChecksum(userId: string, checksum: Buffer): Promise<AssetEntity> { getAssetByChecksum(userId: string, checksum: Buffer): Promise<AssetEntity> {
return this.assetRepository.findOneOrFail({ return this.assetRepository.findOneOrFail({
where: { where: {
userId, userId,
checksum checksum,
}, },
relations: ['exifInfo'], relations: ['exifInfo'],
}); });

View File

@ -32,10 +32,10 @@ export class AssetEntity {
@Column({ type: 'varchar', nullable: true, default: '' }) @Column({ type: 'varchar', nullable: true, default: '' })
encodedVideoPath!: string; encodedVideoPath!: string;
@Column() @Column({ type: 'timestamptz' })
createdAt!: string; createdAt!: string;
@Column() @Column({ type: 'timestamptz' })
modifiedAt!: string; modifiedAt!: string;
@Column({ type: 'boolean', default: false }) @Column({ type: 'boolean', default: false })

View File

@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class FixTimestampDataTypeInAssetTable1662427365521 implements MigrationInterface {
name = 'FixTimestampDataTypeInAssetTable1662427365521';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "exif" ALTER COLUMN "exifTextSearchableColumn" SET NOT NULL`);
await queryRunner.query(
`ALTER TABLE "assets" ALTER COLUMN "createdAt" TYPE timestamptz USING "createdAt"::timestamptz`,
);
await queryRunner.query(
`ALTER TABLE "assets" ALTER COLUMN "modifiedAt" TYPE timestamptz USING "createdAt"::timestamptz`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "assets" ALTER COLUMN "createdAt" TYPE varchar USING "createdAt"::varchar`);
await queryRunner.query(`ALTER TABLE "assets" ALTER COLUMN "modifiedAt" TYPE varchar USING "createdAt"::varchar`);
await queryRunner.query(`ALTER TABLE "exif" ALTER COLUMN "exifTextSearchableColumn" DROP NOT NULL`);
}
}