From f0f76b2520d4221d6c8c29425cb65603df19187c Mon Sep 17 00:00:00 2001 From: Neo Ryo Date: Wed, 10 Apr 2024 11:31:23 +0200 Subject: [PATCH] add new table country + migration and seed all country --- server/entities/country.ts | 2 +- .../1712732757287-New-Country-Table.ts | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 server/migrations/1712732757287-New-Country-Table.ts diff --git a/server/entities/country.ts b/server/entities/country.ts index 1853b8e72..57b12e498 100644 --- a/server/entities/country.ts +++ b/server/entities/country.ts @@ -6,7 +6,7 @@ export class Country { public id: number; @Column({ type: 'text', nullable: false }) - public code: string; + public isoCode: string; @Column({ type: 'text', nullable: false }) public name: string; diff --git a/server/migrations/1712732757287-New-Country-Table.ts b/server/migrations/1712732757287-New-Country-Table.ts new file mode 100644 index 000000000..9acb64849 --- /dev/null +++ b/server/migrations/1712732757287-New-Country-Table.ts @@ -0,0 +1,27 @@ +import type { MigrationInterface, QueryRunner } from 'typeorm'; + +import { countries } from '../utils/iso-3166-countries-french'; + +export class NewCountryTable1712732757287 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE IF NOT EXISTS country ( + id int PRIMARY KEY AUTO_INCREMENT, + isoCode varchar(3) UNIQUE NOT NULL, + name varchar(255) UNIQUE NOT NULL + );`); + // seed country in db + for (const country of countries) { + // try catch here to prevent if country is already set in db for some reason + try { + console.warn(country); + await queryRunner.query(`INSERT INTO country (isoCode, name) VALUES ("${country.isoCode}", "${country.name}");`); + } catch (error) { + console.error(error); + } + } + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE IF EXISTS country;`); + } +}