fix: add indexes and constraints for linked accounts

This commit is contained in:
Michael Thomas
2026-03-20 16:01:43 -04:00
parent c67f7dc9be
commit 7c8ec00cef
3 changed files with 31 additions and 1 deletions

View File

@@ -1,6 +1,15 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import {
Column,
Entity,
Index,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { User } from './User';
@Index('IDX_linked_accounts_provider_sub', ['provider', 'sub'], {
unique: true,
})
@Entity('linked_accounts')
export class LinkedAccount {
constructor(options: Omit<LinkedAccount, 'id'>) {
@@ -10,6 +19,7 @@ export class LinkedAccount {
@PrimaryGeneratedColumn()
id: number;
@Index()
@ManyToOne(() => User, (user) => user.linkedAccounts, { onDelete: 'CASCADE' })
user: User;

View File

@@ -10,9 +10,21 @@ export class AddLinkedAccount1742858617989 implements MigrationInterface {
await queryRunner.query(
`ALTER TABLE "linked_accounts" ADD CONSTRAINT "FK_2c77d2a0c06eeab6e62dc35af64" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`
);
await queryRunner.query(
`CREATE INDEX "IDX_2c77d2a0c06eeab6e62dc35af6" ON "linked_accounts" ("userId") `
);
await queryRunner.query(
`CREATE UNIQUE INDEX "IDX_linked_accounts_provider_sub" ON "linked_accounts" ("provider", "sub") `
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX "public"."IDX_linked_accounts_provider_sub"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_2c77d2a0c06eeab6e62dc35af6"`
);
await queryRunner.query(
`ALTER TABLE "linked_accounts" DROP CONSTRAINT "FK_2c77d2a0c06eeab6e62dc35af64"`
);

View File

@@ -7,9 +7,17 @@ export class AddLinkedAccounts1742858484395 implements MigrationInterface {
await queryRunner.query(
`CREATE TABLE "linked_accounts" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "provider" varchar(255) NOT NULL, "sub" varchar(255) NOT NULL, "username" varchar NOT NULL, "userId" integer, CONSTRAINT "FK_2c77d2a0c06eeab6e62dc35af64" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`
);
await queryRunner.query(
`CREATE INDEX "IDX_2c77d2a0c06eeab6e62dc35af6" ON "linked_accounts" ("userId") `
);
await queryRunner.query(
`CREATE UNIQUE INDEX "IDX_linked_accounts_provider_sub" ON "linked_accounts" ("provider", "sub") `
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_linked_accounts_provider_sub"`);
await queryRunner.query(`DROP INDEX "IDX_2c77d2a0c06eeab6e62dc35af6"`);
await queryRunner.query(`DROP TABLE "linked_accounts"`);
}
}