Skip to content

Commit

Permalink
Spsh 1390 (#789)
Browse files Browse the repository at this point in the history
* add indexes+extension

* add extension to test-container

* add extension to db-init
  • Loading branch information
clauyan authored Nov 29, 2024
1 parent d79b1d7 commit b1a5074
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 4 deletions.
84 changes: 84 additions & 0 deletions migrations/.snapshot-dbildungs-iam-server.json
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,50 @@
"unique": false,
"expression": "create unique index \"person_keycloak_user_id_unique\" on \"person\" (\"keycloak_user_id\") nulls not distinct;"
},
{
"keyName": "person_referrer_trgm_index",
"columnNames": [
"referrer"
],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "create index \"person_referrer_trgm_index\" on \"person\" using gin (\"referrer\" gin_trgm_ops);"
},
{
"keyName": "person_familienname_trgm_index",
"columnNames": [
"familienname"
],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "create index \"person_familienname_trgm_index\" on \"person\" using gin (\"familienname\" gin_trgm_ops);"
},
{
"keyName": "person_vorname_trgm_index",
"columnNames": [
"vorname"
],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "create index \"person_vorname_trgm_index\" on \"person\" using gin (\"vorname\" gin_trgm_ops);"
},
{
"keyName": "person_personalnummer_trgm_index",
"columnNames": [
"personalnummer"
],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "create index \"person_personalnummer_trgm_index\" on \"person\" using gin (\"personalnummer\" gin_trgm_ops);"
},
{
"keyName": "person_personalnummer_unique",
"columnNames": [
Expand Down Expand Up @@ -2345,6 +2389,16 @@
"primary": false,
"unique": true
},
{
"columnNames": [
"person_id"
],
"composite": false,
"keyName": "email_address_person_id_index",
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "email_address_pkey",
"columnNames": [
Expand Down Expand Up @@ -2976,6 +3030,16 @@
"primary": false,
"unique": false
},
{
"columnNames": [
"organisation_id"
],
"composite": false,
"keyName": "personenkontext_organisation_id_index",
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "personenkontext_person_id_organisation_id_rolle_id_unique",
"columnNames": [
Expand Down Expand Up @@ -3222,6 +3286,16 @@
"name": "rolle_merkmal",
"schema": "public",
"indexes": [
{
"columnNames": [
"rolle_id"
],
"composite": false,
"keyName": "rolle_merkmal_rolle_id_index",
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "rolle_merkmal_pkey",
"columnNames": [
Expand Down Expand Up @@ -3454,6 +3528,16 @@
"name": "rolle_systemrecht",
"schema": "public",
"indexes": [
{
"columnNames": [
"rolle_id"
],
"composite": false,
"keyName": "rolle_systemrecht_rolle_id_index",
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "rolle_systemrecht_pkey",
"columnNames": [
Expand Down
41 changes: 41 additions & 0 deletions migrations/Migration20241127092339-S.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20241127092339 extends Migration {
public async up(): Promise<void> {
this.addSql('create extension if not exists pg_trgm;'); // manually added

this.addSql('create index "person_referrer_trgm_index" on "person" using gin ("referrer" gin_trgm_ops);;');
this.addSql(
'create index "person_familienname_trgm_index" on "person" using gin ("familienname" gin_trgm_ops);;',
);
this.addSql('create index "person_vorname_trgm_index" on "person" using gin ("vorname" gin_trgm_ops);;');
this.addSql(
'create index "person_personalnummer_trgm_index" on "person" using gin ("personalnummer" gin_trgm_ops);;',
);

this.addSql('create index "email_address_person_id_index" on "email_address" ("person_id");');

this.addSql('create index "personenkontext_organisation_id_index" on "personenkontext" ("organisation_id");');

this.addSql('create index "rolle_merkmal_rolle_id_index" on "rolle_merkmal" ("rolle_id");');

this.addSql('create index "rolle_systemrecht_rolle_id_index" on "rolle_systemrecht" ("rolle_id");');
}

public override async down(): Promise<void> {
this.addSql('drop extension if exists pg_trgm;'); // manually added

this.addSql('drop index "person_referrer_trgm_index";');
this.addSql('drop index "person_familienname_trgm_index";');
this.addSql('drop index "person_vorname_trgm_index";');
this.addSql('drop index "person_personalnummer_trgm_index";');

this.addSql('drop index "email_address_person_id_index";');

this.addSql('drop index "personenkontext_organisation_id_index";');

this.addSql('drop index "rolle_merkmal_rolle_id_index";');

this.addSql('drop index "rolle_systemrecht_rolle_id_index";');
}
}
3 changes: 3 additions & 0 deletions src/console/db-init.console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class DbInitConsole extends CommandRunner {
this.logger.info('Dropping Schema');
await this.orm.getSchemaGenerator().dropSchema({ wrap: false });

this.logger.info('Creating pg_trgm Extension');
await this.orm.em.getConnection().execute('CREATE EXTENSION IF NOT EXISTS pg_trgm');

this.logger.info('Creating Schema');
await this.orm.getSchemaGenerator().createSchema({ wrap: false });

Expand Down
5 changes: 4 additions & 1 deletion src/modules/email/persistence/email-address.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Enum, ManyToOne, Property, Ref } from '@mikro-orm/core';
import { Entity, Enum, Index, ManyToOne, Property, Ref } from '@mikro-orm/core';
import { PersonEntity } from '../../person/persistence/person.entity.js';
import { TimestampedEntity } from '../../../persistence/timestamped.entity.js';
import { EmailAddressStatus } from '../domain/email-address.js';
Expand All @@ -13,6 +13,9 @@ export class EmailAddressEntity extends TimestampedEntity {
deleteRule: 'set null',
entity: () => PersonEntity,
})
@Index({
name: 'email_address_person_id_index',
})
public personId!: Ref<PersonEntity>;

@Property({ primary: true, nullable: false, unique: true })
Expand Down
18 changes: 18 additions & 0 deletions src/modules/person/persistence/person.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class PersonEntity extends TimestampedEntity {
public keycloakUserId!: string;

@AutoMap()
@Index({
name: 'person_referrer_trgm_index',
expression: 'create index "person_referrer_trgm_index" on "person" using gin ("referrer" gin_trgm_ops);',
})
@Property({ nullable: true })
public referrer?: string;

Expand All @@ -50,10 +54,19 @@ export class PersonEntity extends TimestampedEntity {
public readonly stammorganisation?: string;

@AutoMap()
@Index({
name: 'person_familienname_trgm_index',
expression:
'create index "person_familienname_trgm_index" on "person" using gin ("familienname" gin_trgm_ops);',
})
@Property()
public familienname!: string;

@AutoMap()
@Index({
name: 'person_vorname_trgm_index',
expression: 'create index "person_vorname_trgm_index" on "person" using gin ("vorname" gin_trgm_ops);',
})
@Property()
public vorname!: string;

Expand Down Expand Up @@ -125,6 +138,11 @@ export class PersonEntity extends TimestampedEntity {
name: 'person_personalnummer_unique',
expression: 'create unique index "person_personalnummer_unique" on "person" ("personalnummer") nulls distinct;',
})
@Index({
name: 'person_personalnummer_trgm_index',
expression:
'create index "person_personalnummer_trgm_index" on "person" using gin ("personalnummer" gin_trgm_ops);',
})
@Property({ nullable: true })
public personalnummer?: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class PersonenkontextEntity extends TimestampedEntity {
// TODO EW-636: get from access_token, see SchulConneX (Version 1.003.003.000) page 91
@AutoMap()
@Property({ columnType: 'uuid', nullable: true })
@Index({ name: 'personenkontext_organisation_id_index' })
public organisationId!: string;

@ManyToOne({
Expand Down
5 changes: 4 additions & 1 deletion src/modules/rolle/entity/rolle-merkmal.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BaseEntity, Entity, Enum, ManyToOne, PrimaryKeyProp, Rel } from '@mikro-orm/core';
import { BaseEntity, Entity, Enum, Index, ManyToOne, PrimaryKeyProp, Rel } from '@mikro-orm/core';
import { RollenMerkmal } from '../domain/rolle.enums.js';
import { RolleEntity } from './rolle.entity.js';

@Entity({ tableName: 'rolle_merkmal' })
export class RolleMerkmalEntity extends BaseEntity {
@ManyToOne({ primary: true, entity: () => RolleEntity })
@Index({
name: 'rolle_merkmal_rolle_id_index',
})
public rolle!: Rel<RolleEntity>;

@Enum({ primary: true, items: () => RollenMerkmal, nativeEnumName: 'rollen_merkmal_enum' })
Expand Down
5 changes: 4 additions & 1 deletion src/modules/rolle/entity/rolle-systemrecht.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BaseEntity, Entity, Enum, ManyToOne, PrimaryKeyProp, Rel } from '@mikro-orm/core';
import { BaseEntity, Entity, Enum, Index, ManyToOne, PrimaryKeyProp, Rel } from '@mikro-orm/core';
import { RolleEntity } from './rolle.entity.js';
import { RollenSystemRecht } from '../domain/rolle.enums.js';

@Entity({ tableName: 'rolle_systemrecht' })
export class RolleSystemrechtEntity extends BaseEntity {
@ManyToOne({ primary: true, entity: () => RolleEntity })
@Index({
name: 'rolle_systemrecht_rolle_id_index',
})
public rolle!: Rel<RolleEntity>;

@Enum({ primary: true, items: () => RollenSystemRecht, nativeEnumName: 'rollen_system_recht_enum' })
Expand Down
3 changes: 2 additions & 1 deletion test/utils/database-test.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MikroORM } from '@mikro-orm/core';
import { Migrator, TSMigrationGenerator } from '@mikro-orm/migrations';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { defineConfig } from '@mikro-orm/postgresql';
import { DynamicModule, OnModuleDestroy } from '@nestjs/common';
Expand All @@ -7,7 +8,6 @@ import { PostgreSqlContainer, StartedPostgreSqlContainer } from '@testcontainers
import { randomUUID } from 'crypto';
import { PullPolicy } from 'testcontainers';
import { DbConfig, ServerConfig } from '../../src/shared/config/index.js';
import { Migrator, TSMigrationGenerator } from '@mikro-orm/migrations';

type DatabaseTestModuleOptions = { isDatabaseRequired: boolean; databaseName?: string };

Expand Down Expand Up @@ -65,6 +65,7 @@ export class DatabaseTestModule implements OnModuleDestroy {
}

public static async setupDatabase(orm: MikroORM): Promise<void> {
await orm.em.getConnection().execute('CREATE EXTENSION IF NOT EXISTS pg_trgm');
await orm.getSchemaGenerator().createSchema();
}

Expand Down

0 comments on commit b1a5074

Please sign in to comment.