Files
dh-inspeccion-v2/api-v3/src/database/migrations/1786548001000-phase-a-auth-sessions.ts
T

49 lines
1.7 KiB
TypeScript

import { MigrationInterface, QueryRunner } from 'typeorm';
export class PhaseAAuthSessions1786548001000 implements MigrationInterface {
name = 'PhaseAAuthSessions1786548001000';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE auth_sessions (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
refresh_token_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at timestamptz NOT NULL,
last_used_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
revoked_at timestamptz,
replaced_by_session_id uuid,
ip inet,
user_agent text,
device_label text,
CONSTRAINT fk_auth_sessions_user
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE RESTRICT,
CONSTRAINT fk_auth_sessions_replaced_by
FOREIGN KEY (replaced_by_session_id)
REFERENCES auth_sessions(id) ON DELETE SET NULL,
CONSTRAINT chk_auth_sessions_expiration
CHECK (expires_at > created_at)
)
`);
await queryRunner.query(`
CREATE UNIQUE INDEX uq_auth_sessions_refresh_token_hash
ON auth_sessions (refresh_token_hash)
`);
await queryRunner.query(`
CREATE INDEX idx_auth_sessions_user_id ON auth_sessions (user_id)
`);
await queryRunner.query(`
CREATE INDEX idx_auth_sessions_expires_at ON auth_sessions (expires_at)
`);
await queryRunner.query(`
CREATE INDEX idx_auth_sessions_revoked_at ON auth_sessions (revoked_at)
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('DROP TABLE auth_sessions');
}
}