diff --git a/src/crags/entities/crag.entity.ts b/src/crags/entities/crag.entity.ts index a7fd89f..ada4b81 100644 --- a/src/crags/entities/crag.entity.ts +++ b/src/crags/entities/crag.entity.ts @@ -268,4 +268,16 @@ export class Crag extends BaseEntity { @Column({ type: 'jsonb', nullable: true }) @Field((type) => GraphQLJSON, { nullable: true }) nrRoutesByGrade: JSON; + + @Column({ default: false }) + @Field() + hasSport: boolean; + + @Column({ default: false }) + @Field() + hasBoulder: boolean; + + @Column({ default: false }) + @Field() + hasMultipitch: boolean; } diff --git a/src/migration/1708077643082-addHasSportHasBoulderHasMultipitchToCrag.ts b/src/migration/1708077643082-addHasSportHasBoulderHasMultipitchToCrag.ts new file mode 100644 index 0000000..6ac5bd1 --- /dev/null +++ b/src/migration/1708077643082-addHasSportHasBoulderHasMultipitchToCrag.ts @@ -0,0 +1,40 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class addHasSportHasBoulderHasMultipitchToCrag1708077643082 + implements MigrationInterface +{ + name = 'addHasSportHasBoulderHasMultipitchToCrag1708077643082'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "crag" ADD "has_sport" boolean NOT NULL DEFAULT false`, + ); + + await queryRunner.query( + `ALTER TABLE "crag" ADD "has_boulder" boolean NOT NULL DEFAULT false`, + ); + await queryRunner.query( + `ALTER TABLE "crag" ADD "has_multipitch" boolean NOT NULL DEFAULT false`, + ); + + const crags = await queryRunner.query( + `SELECT c.id, r.route_type_id FROM crag c + INNER JOIN route r on r.crag_id = c.id + GROUP BY r.route_type_id, c.id`, + ); + + for (const crag of crags) { + if (['sport', 'boulder', 'multipitch'].includes(crag.route_type_id)) { + await queryRunner.query( + `UPDATE crag SET has_${crag.route_type_id} = true WHERE id = '${crag.id}'`, + ); + } + } + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "has_multipitch"`); + await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "has_boulder"`); + await queryRunner.query(`ALTER TABLE "crag" DROP COLUMN "has_sport"`); + } +}