-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
postgres migrations, docker-compose.local-postgres.yml
- Loading branch information
mrkeksz
committed
Jan 7, 2024
1 parent
ac7bf30
commit eb9abeb
Showing
14 changed files
with
291 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
/node_modules | ||
error-graph.json | ||
schema.gql | ||
/temp-postgres-data | ||
|
||
# Logs | ||
logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: "3.1" | ||
services: | ||
waisy_postgres_db_dev: | ||
image: postgres:16.1 | ||
ports: | ||
- '5444:5432' | ||
container_name: 'waisy_postgres_db_dev' | ||
volumes: | ||
- ./temp-postgres-data/dev-data:/var/lib/postgresql/data | ||
environment: | ||
POSTGRES_DB: "waisy" | ||
POSTGRES_USER: "waisy" | ||
POSTGRES_PASSWORD: "1234" | ||
POSTGRES_HOST_AUTH_METHOD: "trust" | ||
LANG: 'C' | ||
LANGUAGE: 'C' | ||
LC_ALL: 'C' | ||
waisy_postgres_db_test: | ||
image: postgres:16.1 | ||
ports: | ||
- '5443:5432' | ||
container_name: 'waisy_postgres_db_test' | ||
volumes: | ||
- ./temp-postgres-data/test-data:/var/lib/postgresql/data | ||
environment: | ||
POSTGRES_DB: "waisy" | ||
POSTGRES_USER: "waisy" | ||
POSTGRES_PASSWORD: "1234" | ||
POSTGRES_HOST_AUTH_METHOD: "trust" | ||
LANG: 'C' | ||
LANGUAGE: 'C' | ||
LC_ALL: 'C' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as dotenv from 'dotenv' | ||
import {DataSource} from 'typeorm' | ||
import * as fs from 'fs' | ||
import * as Joi from 'joi' | ||
import {buildLoggerInstance} from './src/logger' | ||
import {buildDataSourceOptions} from './src/type-orm/type-orm.module.options' | ||
import {postgresConfigEnvValidationSchema} from './src/config/postgres/postgres.config.env-validation-schema' | ||
|
||
const hasLocalEnvFile = fs.existsSync('.env.local') | ||
dotenv.config({ | ||
override: false, | ||
path: hasLocalEnvFile ? '.env.local' : '.env', | ||
}) | ||
|
||
function buildOrmConfig(): DataSource { | ||
const logger = buildLoggerInstance('warn') | ||
|
||
const {error, value, warning} = Joi.object(postgresConfigEnvValidationSchema).validate( | ||
process.env, | ||
{stripUnknown: true}, | ||
) | ||
|
||
if (error) { | ||
logger.error(error) | ||
throw new Error(`Config validation error: ${error}`) | ||
} | ||
if (warning) logger.warn(warning) | ||
|
||
return new DataSource( | ||
buildDataSourceOptions({ | ||
host: value.POSTGRES_HOST, | ||
port: Number(value.POSTGRES_PORT), | ||
username: value.POSTGRES_USERNAME, | ||
password: value.POSTGRES_PASSWORD, | ||
database: value.POSTGRES_DATABASE, | ||
}), | ||
) | ||
} | ||
|
||
export default buildOrmConfig() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import {MigrationInterface, QueryRunner} from 'typeorm' | ||
|
||
export class Migration1704656438060 implements MigrationInterface { | ||
name = 'Migration1704656438060' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE TYPE "public"."email_verification_code_input_attempt_status_enum" AS ENUM('success', 'failure') | ||
`) | ||
await queryRunner.query(` | ||
CREATE TABLE "email_verification_code_input_attempt" ( | ||
"id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
"email" character varying NOT NULL, | ||
"senderIp" inet NOT NULL, | ||
"status" "public"."email_verification_code_input_attempt_status_enum" NOT NULL, | ||
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
CONSTRAINT "PK_8d50b1c50ee28684f3207e9f560" PRIMARY KEY ("id") | ||
); | ||
COMMENT ON COLUMN "email_verification_code_input_attempt"."email" IS 'Email address that the code was sent to'; | ||
COMMENT ON COLUMN "email_verification_code_input_attempt"."senderIp" IS 'The IP address of the client that entered the code'; | ||
COMMENT ON COLUMN "email_verification_code_input_attempt"."status" IS 'The status of the verification attempt' | ||
`) | ||
await queryRunner.query(` | ||
CREATE TABLE "email_verification_code_sending_attempt" ( | ||
"id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
"email" character varying NOT NULL, | ||
"senderIp" inet NOT NULL, | ||
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
CONSTRAINT "PK_a5a612a41a1b640702da6638861" PRIMARY KEY ("id") | ||
); | ||
COMMENT ON COLUMN "email_verification_code_sending_attempt"."email" IS 'Email address to which the code was sent'; | ||
COMMENT ON COLUMN "email_verification_code_sending_attempt"."senderIp" IS 'The IP address of the client that requested the code' | ||
`) | ||
await queryRunner.query(` | ||
CREATE TABLE "user" ( | ||
"id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
"email" character varying NOT NULL, | ||
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
CONSTRAINT "UQ_e12875dfb3b1d92d7d7c5377e22" UNIQUE ("email"), | ||
CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id") | ||
) | ||
`) | ||
await queryRunner.query(` | ||
CREATE TYPE "public"."refresh_token_status_enum" AS ENUM('active', 'inactive') | ||
`) | ||
await queryRunner.query(` | ||
CREATE TABLE "refresh_token" ( | ||
"id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
"status" "public"."refresh_token_status_enum" NOT NULL DEFAULT 'active', | ||
"refreshToken" character varying NOT NULL, | ||
"deviceInfo" character varying NOT NULL, | ||
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
"userId" uuid NOT NULL, | ||
CONSTRAINT "UQ_428e14ded7299edfcf58918beaf" UNIQUE ("refreshToken"), | ||
CONSTRAINT "PK_b575dd3c21fb0831013c909e7fe" PRIMARY KEY ("id") | ||
); | ||
COMMENT ON COLUMN "refresh_token"."status" IS 'Status of token'; | ||
COMMENT ON COLUMN "refresh_token"."refreshToken" IS 'Hashed refresh token'; | ||
COMMENT ON COLUMN "refresh_token"."deviceInfo" IS 'Device information' | ||
`) | ||
await queryRunner.query(` | ||
CREATE UNIQUE INDEX "IDX_c98a57bf5874dc146aa6360fd0" ON "refresh_token" ("userId", "deviceInfo") | ||
WHERE "status" = 'active' | ||
`) | ||
await queryRunner.query(` | ||
CREATE TYPE "public"."email_verification_code_status_enum" AS ENUM('active', 'expired', 'used') | ||
`) | ||
await queryRunner.query(` | ||
CREATE TABLE "email_verification_code" ( | ||
"id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
"code" integer NOT NULL, | ||
"status" "public"."email_verification_code_status_enum" NOT NULL DEFAULT 'active', | ||
"expirationDate" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), | ||
"userId" uuid NOT NULL, | ||
CONSTRAINT "PK_7fc72ac16aeeab466c48748221c" PRIMARY KEY ("id") | ||
); | ||
COMMENT ON COLUMN "email_verification_code"."code" IS 'The verification code sent via email'; | ||
COMMENT ON COLUMN "email_verification_code"."status" IS 'Status of the verification code' | ||
`) | ||
await queryRunner.query(` | ||
ALTER TABLE "refresh_token" | ||
ADD CONSTRAINT "FK_8e913e288156c133999341156ad" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION | ||
`) | ||
await queryRunner.query(` | ||
ALTER TABLE "email_verification_code" | ||
ADD CONSTRAINT "FK_cace043f9e8bee80c2dd5c66ccc" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION | ||
`) | ||
await queryRunner.query(` | ||
CREATE TABLE "query-result-cache" ( | ||
"id" SERIAL NOT NULL, | ||
"identifier" character varying, | ||
"time" bigint NOT NULL, | ||
"duration" integer NOT NULL, | ||
"query" text NOT NULL, | ||
"result" text NOT NULL, | ||
CONSTRAINT "PK_6a98f758d8bfd010e7e10ffd3d3" PRIMARY KEY ("id") | ||
) | ||
`) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
DROP TABLE "query-result-cache" | ||
`) | ||
await queryRunner.query(` | ||
ALTER TABLE "email_verification_code" DROP CONSTRAINT "FK_cace043f9e8bee80c2dd5c66ccc" | ||
`) | ||
await queryRunner.query(` | ||
ALTER TABLE "refresh_token" DROP CONSTRAINT "FK_8e913e288156c133999341156ad" | ||
`) | ||
await queryRunner.query(` | ||
DROP TABLE "email_verification_code" | ||
`) | ||
await queryRunner.query(` | ||
DROP TYPE "public"."email_verification_code_status_enum" | ||
`) | ||
await queryRunner.query(` | ||
DROP INDEX "public"."IDX_c98a57bf5874dc146aa6360fd0" | ||
`) | ||
await queryRunner.query(` | ||
DROP TABLE "refresh_token" | ||
`) | ||
await queryRunner.query(` | ||
DROP TYPE "public"."refresh_token_status_enum" | ||
`) | ||
await queryRunner.query(` | ||
DROP TABLE "user" | ||
`) | ||
await queryRunner.query(` | ||
DROP TABLE "email_verification_code_sending_attempt" | ||
`) | ||
await queryRunner.query(` | ||
DROP TABLE "email_verification_code_input_attempt" | ||
`) | ||
await queryRunner.query(` | ||
DROP TYPE "public"."email_verification_code_input_attempt_status_enum" | ||
`) | ||
} | ||
} |
Oops, something went wrong.