From 7edd42251f1adfd0ebefef123a8607fc32a0489a Mon Sep 17 00:00:00 2001 From: shamilkhan Date: Fri, 21 Apr 2023 01:01:38 +0400 Subject: [PATCH 1/3] Add CreateElectionSchema migration. --- .../1624373989787-CreateElectionSchema.ts | 205 ++++++++++++++++++ src/migrations/1681498835690-initialSetup.ts | 56 ----- 2 files changed, 205 insertions(+), 56 deletions(-) create mode 100644 src/migrations/1624373989787-CreateElectionSchema.ts delete mode 100644 src/migrations/1681498835690-initialSetup.ts diff --git a/src/migrations/1624373989787-CreateElectionSchema.ts b/src/migrations/1624373989787-CreateElectionSchema.ts new file mode 100644 index 0000000..b8785e4 --- /dev/null +++ b/src/migrations/1624373989787-CreateElectionSchema.ts @@ -0,0 +1,205 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class CreateElectionSchema1624373989787 implements MigrationInterface { + name = 'CreateElectionSchema1624373989787'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query('CREATE EXTENSION IF NOT EXISTS postgis;'); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS states ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + boundary geometry(MULTIPOLYGON, 4326) + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS counties ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + state_id INTEGER REFERENCES states(id) NOT NULL, + boundary geometry(MULTIPOLYGON, 4326) + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS cities ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + state_id INTEGER REFERENCES states(id) NOT NULL, + county_id INTEGER REFERENCES counties(id) NOT NULL, + boundary geometry(POINT, 4326) + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS parties ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS candidates ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + party_id INTEGER REFERENCES parties(id) NOT NULL + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS elections ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + year INTEGER NOT NULL, + type VARCHAR(255) NOT NULL, + start_date DATE NOT NULL, + end_date DATE NOT NULL + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS state_election_results ( + id SERIAL PRIMARY KEY, + election_id INTEGER REFERENCES elections(id) NOT NULL, + state_id INTEGER REFERENCES states(id) NOT NULL, + winner_id INTEGER REFERENCES candidates(id) NOT NULL, + total_votes INTEGER NOT NULL, + candidate_votes JSONB NOT NULL + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS county_election_results ( + id SERIAL PRIMARY KEY, + election_id INTEGER REFERENCES elections(id) NOT NULL, + county_id INTEGER REFERENCES counties(id) NOT NULL, + winner_id INTEGER REFERENCES candidates(id) NOT NULL, + total_votes INTEGER NOT NULL, + candidate_votes JSONB NOT NULL + );`, + ); + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS city_election_results ( + id SERIAL PRIMARY KEY, + election_id INTEGER REFERENCES elections(id) NOT NULL, + city_id INTEGER REFERENCES cities(id) NOT NULL, + winner_id INTEGER REFERENCES candidates(id) NOT NULL, + total_votes INTEGER NOT NULL, + candidate_votes JSONB NOT NULL + );`, + ); + + // Add indexes and constraints + await queryRunner.query( + 'CREATE INDEX IF NOT EXISTS idx_states_boundary ON states USING GIST(boundary);', + ); + await queryRunner.query( + 'CREATE INDEX IF NOT EXISTS idx_counties_boundary ON counties USING GIST(boundary);', + ); + await queryRunner.query( + 'CREATE INDEX IF NOT EXISTS idx_cities_boundary ON cities USING GIST(boundary);', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT IF NOT EXISTS fk_state_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT IF NOT EXISTS fk_state_election_results_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT IF NOT EXISTS fk_state_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT IF NOT EXISTS fk_county_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT IF NOT EXISTS fk_county_election_results_counties FOREIGN KEY (county_id) REFERENCES counties(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT IF NOT EXISTS fk_county_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT IF NOT EXISTS fk_city_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT IF NOT EXISTS fk_city_election_results_cities FOREIGN KEY (city_id) REFERENCES cities(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT IF NOT EXISTS fk_city_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS counties ADD CONSTRAINT IF NOT EXISTS fk_counties_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS cities ADD CONSTRAINT IF NOT EXISTS fk_cities_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS cities ADD CONSTRAINT IF NOT EXISTS fk_cities_counties FOREIGN KEY (county_id) REFERENCES counties(id) ON DELETE CASCADE;', + ); + } + + public async down(queryRunner: QueryRunner): Promise { + // Remove indexes and constraints + await queryRunner.query('DROP INDEX IF EXISTS idx_states_boundary;'); + await queryRunner.query('DROP INDEX IF EXISTS idx_counties_boundary;'); + await queryRunner.query('DROP INDEX IF EXISTS idx_cities_boundary;'); + await queryRunner.query( + 'ALTER TABLE IF EXISTS state_election_results DROP CONSTRAINT IF EXISTS fk_state_election_results_elections;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS county_election_results DROP CONSTRAINT IF EXISTS fk_county_election_results_elections;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS city_election_results DROP CONSTRAINT IF EXISTS fk_city_election_results_elections;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS state_election_results DROP CONSTRAINT IF EXISTS fk_state_election_results_states;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS county_election_results DROP CONSTRAINT IF EXISTS fk_county_election_results_counties;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS city_election_results DROP CONSTRAINT IF EXISTS fk_city_election_results_cities;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS states DROP CONSTRAINT IF EXISTS pk_states;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS counties DROP CONSTRAINT IF EXISTS pk_counties;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS cities DROP CONSTRAINT IF EXISTS pk_cities;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS parties DROP CONSTRAINT IF EXISTS pk_parties;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS candidates DROP CONSTRAINT IF EXISTS pk_candidates;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS elections DROP CONSTRAINT IF EXISTS pk_elections;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS state_election_results DROP CONSTRAINT IF EXISTS pk_state_election_results;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS county_election_results DROP CONSTRAINT IF EXISTS pk_county_election_results;', + ); + await queryRunner.query( + 'ALTER TABLE IF EXISTS city_election_results DROP CONSTRAINT IF EXISTS pk_city_election_results;', + ); + + // Drop tables + await queryRunner.query( + 'DROP TABLE IF EXISTS state_election_results CASCADE;', + ); + await queryRunner.query( + 'DROP TABLE IF EXISTS county_election_results CASCADE;', + ); + await queryRunner.query( + 'DROP TABLE IF EXISTS city_election_results CASCADE;', + ); + await queryRunner.query('DROP TABLE IF EXISTS elections CASCADE;'); + await queryRunner.query('DROP TABLE IF EXISTS candidates CASCADE;'); + await queryRunner.query('DROP TABLE IF EXISTS parties CASCADE;'); + await queryRunner.query('DROP TABLE IF EXISTS cities CASCADE;'); + await queryRunner.query('DROP TABLE IF EXISTS counties CASCADE;'); + await queryRunner.query('DROP TABLE IF EXISTS states CASCADE;'); + + // Remove PostGIS extension + await queryRunner.query('DROP EXTENSION IF EXISTS postgis;'); + } +} diff --git a/src/migrations/1681498835690-initialSetup.ts b/src/migrations/1681498835690-initialSetup.ts deleted file mode 100644 index 91a8930..0000000 --- a/src/migrations/1681498835690-initialSetup.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { MigrationInterface, QueryRunner } from "typeorm"; - -export class InitialSetup1681498835690 implements MigrationInterface { - name = 'InitialSetup1681498835690' - - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TABLE "party" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, CONSTRAINT "PK_e6189b3d533e140bb33a6d2cec1" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "candidate" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "partyId" integer, CONSTRAINT "PK_b0ddec158a9a60fbc785281581b" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "election" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "year" integer NOT NULL, "type" character varying NOT NULL, "startDate" TIMESTAMP NOT NULL, "endDate" TIMESTAMP NOT NULL, CONSTRAINT "PK_17467b9ade12257d01912737bdb" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "state" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "electoralVotes" integer NOT NULL, "boundary" geometry(MultiPolygon,4326) NOT NULL, CONSTRAINT "PK_549ffd046ebab1336c3a8030a12" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "county" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "boundary" geometry(MultiPolygon,4326) NOT NULL, "stateId" integer, CONSTRAINT "PK_e64ba58a034afb0e3d15b329351" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "city" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "boundary" geometry(Point,4326) NOT NULL, "stateId" integer, "countyId" integer, CONSTRAINT "PK_b222f51ce26f7e5ca86944a6739" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "city_election_result" ("id" SERIAL NOT NULL, "totalVotes" integer NOT NULL, "candidateVotes" jsonb NOT NULL, "electionId" integer, "cityId" integer, "winnerId" integer, CONSTRAINT "PK_d77a3431b6323f8034c9f58367c" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "county_election_result" ("id" SERIAL NOT NULL, "totalVotes" integer NOT NULL, "candidateVotes" jsonb NOT NULL, "electionId" integer, "countyId" integer, "winnerId" integer, CONSTRAINT "PK_60dd53fdc76892293d4311bc90d" PRIMARY KEY ("id"))`); - await queryRunner.query(`CREATE TABLE "state_election_result" ("id" SERIAL NOT NULL, "totalVotes" integer NOT NULL, "candidateVotes" jsonb NOT NULL, "electionId" integer, "stateId" integer, "winnerId" integer, CONSTRAINT "PK_56a1be9f8dfc306a08e8d1a5543" PRIMARY KEY ("id"))`); - await queryRunner.query(`ALTER TABLE "candidate" ADD CONSTRAINT "FK_ee54545f559cc92ca30424fa82f" FOREIGN KEY ("partyId") REFERENCES "party"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "county" ADD CONSTRAINT "FK_4c13926e067e17762475723eabf" FOREIGN KEY ("stateId") REFERENCES "state"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "city" ADD CONSTRAINT "FK_e99de556ee56afe72154f3ed04a" FOREIGN KEY ("stateId") REFERENCES "state"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "city" ADD CONSTRAINT "FK_473f9ba4b3863bf5356a05f0930" FOREIGN KEY ("countyId") REFERENCES "county"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "city_election_result" ADD CONSTRAINT "FK_cf31816a583a42ad3129aa733ed" FOREIGN KEY ("electionId") REFERENCES "election"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "city_election_result" ADD CONSTRAINT "FK_c8fb7ea06758cd18f97fb38c47c" FOREIGN KEY ("cityId") REFERENCES "city"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "city_election_result" ADD CONSTRAINT "FK_d272df6582822ffb340f38a3d31" FOREIGN KEY ("winnerId") REFERENCES "candidate"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "county_election_result" ADD CONSTRAINT "FK_06b7d33f26dffbacc3cf85be967" FOREIGN KEY ("electionId") REFERENCES "election"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "county_election_result" ADD CONSTRAINT "FK_1cc33f36fd79bc46bdb929851a6" FOREIGN KEY ("countyId") REFERENCES "county"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "county_election_result" ADD CONSTRAINT "FK_f0a552136102783c4270f628b80" FOREIGN KEY ("winnerId") REFERENCES "candidate"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "state_election_result" ADD CONSTRAINT "FK_fd20dc1974300858fda5ba21b78" FOREIGN KEY ("electionId") REFERENCES "election"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "state_election_result" ADD CONSTRAINT "FK_a19e55a82f88cc0830cec4aecdc" FOREIGN KEY ("stateId") REFERENCES "state"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE "state_election_result" ADD CONSTRAINT "FK_e12bccbbefe4feb191e200b30d3" FOREIGN KEY ("winnerId") REFERENCES "candidate"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE "state_election_result" DROP CONSTRAINT "FK_e12bccbbefe4feb191e200b30d3"`); - await queryRunner.query(`ALTER TABLE "state_election_result" DROP CONSTRAINT "FK_a19e55a82f88cc0830cec4aecdc"`); - await queryRunner.query(`ALTER TABLE "state_election_result" DROP CONSTRAINT "FK_fd20dc1974300858fda5ba21b78"`); - await queryRunner.query(`ALTER TABLE "county_election_result" DROP CONSTRAINT "FK_f0a552136102783c4270f628b80"`); - await queryRunner.query(`ALTER TABLE "county_election_result" DROP CONSTRAINT "FK_1cc33f36fd79bc46bdb929851a6"`); - await queryRunner.query(`ALTER TABLE "county_election_result" DROP CONSTRAINT "FK_06b7d33f26dffbacc3cf85be967"`); - await queryRunner.query(`ALTER TABLE "city_election_result" DROP CONSTRAINT "FK_d272df6582822ffb340f38a3d31"`); - await queryRunner.query(`ALTER TABLE "city_election_result" DROP CONSTRAINT "FK_c8fb7ea06758cd18f97fb38c47c"`); - await queryRunner.query(`ALTER TABLE "city_election_result" DROP CONSTRAINT "FK_cf31816a583a42ad3129aa733ed"`); - await queryRunner.query(`ALTER TABLE "city" DROP CONSTRAINT "FK_473f9ba4b3863bf5356a05f0930"`); - await queryRunner.query(`ALTER TABLE "city" DROP CONSTRAINT "FK_e99de556ee56afe72154f3ed04a"`); - await queryRunner.query(`ALTER TABLE "county" DROP CONSTRAINT "FK_4c13926e067e17762475723eabf"`); - await queryRunner.query(`ALTER TABLE "candidate" DROP CONSTRAINT "FK_ee54545f559cc92ca30424fa82f"`); - await queryRunner.query(`DROP TABLE "state_election_result"`); - await queryRunner.query(`DROP TABLE "county_election_result"`); - await queryRunner.query(`DROP TABLE "city_election_result"`); - await queryRunner.query(`DROP TABLE "city"`); - await queryRunner.query(`DROP TABLE "county"`); - await queryRunner.query(`DROP TABLE "state"`); - await queryRunner.query(`DROP TABLE "election"`); - await queryRunner.query(`DROP TABLE "candidate"`); - await queryRunner.query(`DROP TABLE "party"`); - } - -} From 564c0f17bc950798cf10330c9f5ba6610e5890ac Mon Sep 17 00:00:00 2001 From: shamilkhan Date: Fri, 21 Apr 2023 18:06:22 +0400 Subject: [PATCH 2/3] Remove IF NOT EXISTS --- .../1624373989787-CreateElectionSchema.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/migrations/1624373989787-CreateElectionSchema.ts b/src/migrations/1624373989787-CreateElectionSchema.ts index b8785e4..04d9ade 100644 --- a/src/migrations/1624373989787-CreateElectionSchema.ts +++ b/src/migrations/1624373989787-CreateElectionSchema.ts @@ -94,40 +94,40 @@ export class CreateElectionSchema1624373989787 implements MigrationInterface { 'CREATE INDEX IF NOT EXISTS idx_cities_boundary ON cities USING GIST(boundary);', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT IF NOT EXISTS fk_state_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT fk_state_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT IF NOT EXISTS fk_state_election_results_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT fk_state_election_results_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT IF NOT EXISTS fk_state_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS state_election_results ADD CONSTRAINT fk_state_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT IF NOT EXISTS fk_county_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT fk_county_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT IF NOT EXISTS fk_county_election_results_counties FOREIGN KEY (county_id) REFERENCES counties(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT fk_county_election_results_counties FOREIGN KEY (county_id) REFERENCES counties(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT IF NOT EXISTS fk_county_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS county_election_results ADD CONSTRAINT fk_county_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT IF NOT EXISTS fk_city_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT fk_city_election_results_elections FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT IF NOT EXISTS fk_city_election_results_cities FOREIGN KEY (city_id) REFERENCES cities(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT fk_city_election_results_cities FOREIGN KEY (city_id) REFERENCES cities(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT IF NOT EXISTS fk_city_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS city_election_results ADD CONSTRAINT fk_city_election_results_candidates FOREIGN KEY (winner_id) REFERENCES candidates(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS counties ADD CONSTRAINT IF NOT EXISTS fk_counties_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS counties ADD CONSTRAINT fk_counties_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS cities ADD CONSTRAINT IF NOT EXISTS fk_cities_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS cities ADD CONSTRAINT fk_cities_states FOREIGN KEY (state_id) REFERENCES states(id) ON DELETE CASCADE;', ); await queryRunner.query( - 'ALTER TABLE IF EXISTS cities ADD CONSTRAINT IF NOT EXISTS fk_cities_counties FOREIGN KEY (county_id) REFERENCES counties(id) ON DELETE CASCADE;', + 'ALTER TABLE IF EXISTS cities ADD CONSTRAINT fk_cities_counties FOREIGN KEY (county_id) REFERENCES counties(id) ON DELETE CASCADE;', ); } From b9bb85eaa08012b5b2f66993288596573b8d64b8 Mon Sep 17 00:00:00 2001 From: shamilkhan Date: Fri, 21 Apr 2023 18:17:49 +0400 Subject: [PATCH 3/3] Add migrationsRun: true --- src/config/typeorm.config.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config/typeorm.config.ts b/src/config/typeorm.config.ts index 6733684..52e60bd 100644 --- a/src/config/typeorm.config.ts +++ b/src/config/typeorm.config.ts @@ -9,6 +9,7 @@ export const dataSourceOptions: DataSourceOptions = { database: process.env.DB_NAME || 'elections_us', entities: [__dirname + '/../**/*.entity{.ts,.js}'], migrations: [__dirname + '/../migrations/*{.ts,.js}'], + migrationsRun: true, }; const dataSource = new DataSource(dataSourceOptions);