Skip to content

Commit

Permalink
fix/add-indexes-to-searchable-columns: added indexes to searcable col…
Browse files Browse the repository at this point in the history
…umns in the database (#394)
  • Loading branch information
BEdev24 authored Sep 30, 2024
1 parent d9b6cb2 commit ed8f73d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddIndexesToSearchableColumns1727279527027
implements MigrationInterface
{
name = 'AddIndexesToSearchableColumns1727279527027';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE INDEX "users_status_idx" ON "users" ("status") `,
);
await queryRunner.query(
`CREATE INDEX "users_role_id_idx" ON "users" ("role_id") `,
);
await queryRunner.query(
`CREATE INDEX "roles_code_idx" ON "roles" ("code") `,
);
await queryRunner.query(
`CREATE INDEX "gov_action_status_idx" ON "gov_action_proposals" ("status") `,
);
await queryRunner.query(
`CREATE INDEX "gov_action_type_idx" ON "gov_action_proposals" ("gov_action_type") `,
);
await queryRunner.query(
`CREATE INDEX "votes_user_id_idx" ON "votes" ("user_id") `,
);
await queryRunner.query(
`CREATE INDEX "votes_gov_action_proposal_id_idx" ON "votes" ("gov_action_proposal_id") `,
);
await queryRunner.query(
`CREATE INDEX "votes_vote_idx" ON "votes" ("vote") `,
);
await queryRunner.query(
`ALTER TABLE "rationales" ADD CONSTRAINT "UQ_d2e92e8f049eaa02a6de27bcb48" UNIQUE ("user_id", "gov_action_proposal_id")`,
);
await queryRunner.query(
`ALTER TABLE "rationales" ADD CONSTRAINT "FK_a5fa1ad294195c3957205a2937a" FOREIGN KEY ("gov_action_proposal_id") REFERENCES "gov_action_proposals"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "rationales" DROP CONSTRAINT "FK_a5fa1ad294195c3957205a2937a"`,
);
await queryRunner.query(
`ALTER TABLE "rationales" DROP CONSTRAINT "UQ_d2e92e8f049eaa02a6de27bcb48"`,
);
await queryRunner.query(`DROP INDEX "public"."votes_vote_idx"`);
await queryRunner.query(
`DROP INDEX "public"."votes_gov_action_proposal_id_idx"`,
);
await queryRunner.query(`DROP INDEX "public"."votes_user_id_idx"`);
await queryRunner.query(`DROP INDEX "public"."gov_action_type_idx"`);
await queryRunner.query(`DROP INDEX "public"."gov_action_status_idx"`);
await queryRunner.query(`DROP INDEX "public"."roles_code_idx"`);
await queryRunner.query(`DROP INDEX "public"."users_role_id_idx"`);
await queryRunner.query(`DROP INDEX "public"."users_status_idx"`);
}
}
6 changes: 3 additions & 3 deletions backend/src/governance/entities/gov-action-proposal.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonEntity } from '../../common/entities/common.entity';
import { Column, Entity, OneToMany, PrimaryColumn } from 'typeorm';
import { Column, Entity, Index, OneToMany, PrimaryColumn } from 'typeorm';
import { Vote } from './vote.entity';
import { Rationale } from './rationale.entity';

Expand All @@ -23,15 +23,13 @@ export class GovActionProposal extends CommonEntity {
@Column({
name: 'title',
type: 'varchar',
length: 80,
nullable: true,
})
title: string;

@Column({
name: 'abstract',
type: 'varchar',
length: 2500,
nullable: true,
})
abstract: string;
Expand All @@ -42,12 +40,14 @@ export class GovActionProposal extends CommonEntity {
})
govMetadataUrl: string;

@Index('gov_action_status_idx')
@Column({
name: 'status',
type: 'varchar',
})
status: string;

@Index('gov_action_type_idx')
@Column({
name: 'gov_action_type',
type: 'varchar',
Expand Down
12 changes: 11 additions & 1 deletion backend/src/governance/entities/vote.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { CommonEntity } from '../../common/entities/common.entity';
import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn } from 'typeorm';
import {
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
PrimaryColumn,
} from 'typeorm';
import { GovActionProposal } from './gov-action-proposal.entity';

@Entity('votes')
Expand All @@ -10,6 +17,7 @@ export class Vote extends CommonEntity {
})
id: string;

@Index('votes_user_id_idx')
@Column({
name: 'user_id',
type: 'uuid',
Expand All @@ -22,6 +30,7 @@ export class Vote extends CommonEntity {
})
hotAddress: string;

@Index('votes_gov_action_proposal_id_idx')
@ManyToOne(
() => GovActionProposal,
(govActionProposal) => govActionProposal.id,
Expand All @@ -32,6 +41,7 @@ export class Vote extends CommonEntity {
@JoinColumn({ name: 'gov_action_proposal_id' })
govActionProposal: GovActionProposal;

@Index('votes_vote_idx')
@Column({
name: 'vote',
type: 'varchar',
Expand Down
2 changes: 2 additions & 0 deletions backend/src/users/entities/role.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Column,
Entity,
Index,
JoinTable,
ManyToMany,
OneToMany,
Expand All @@ -16,6 +17,7 @@ export class Role extends CommonEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Index('roles_code_idx')
@Column({
name: 'code',
type: 'enum',
Expand Down
3 changes: 3 additions & 0 deletions backend/src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Column,
Entity,
Index,
JoinColumn,
JoinTable,
ManyToMany,
Expand Down Expand Up @@ -50,6 +51,7 @@ export class User extends CommonEntity {
})
profilePhotoUrl: string;

@Index('users_status_idx')
@Column({
name: 'status',
type: 'enum',
Expand All @@ -63,6 +65,7 @@ export class User extends CommonEntity {
})
hotAddresses: HotAddress[];

@Index('users_role_id_idx')
@ManyToOne(() => Role, (role) => role.users, {
eager: true,
})
Expand Down

0 comments on commit ed8f73d

Please sign in to comment.