Skip to content

Commit

Permalink
Add new Filler List pivot table + DB migration
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa committed Jan 26, 2024
1 parent 23aeaa3 commit e1c6ac6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
25 changes: 25 additions & 0 deletions server/dao/entities/FillerListContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Entity,
ManyToOne,
PrimaryKeyProp,
Property,
Rel,
Unique,
} from '@mikro-orm/core';
import { FillerShow } from './FillerShow.js';
import { Program } from './Program.js';

@Entity({ tableName: 'filler_show_content' }) // Original entity was called FillerShow - we want to replace the pivot table
@Unique({ properties: ['fillerList', 'content', 'index'] })
export class FillerListContent {
@ManyToOne({ primary: true, name: 'filler_show_uuid' })
fillerList!: Rel<FillerShow>;

@ManyToOne({ primary: true, name: 'program_uuid' })
content!: Rel<Program>;

@Property()
index!: number;

[PrimaryKeyProp]?: ['fillerList', 'content'];
}
3 changes: 2 additions & 1 deletion server/dao/entities/FillerShow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Collection, Entity, ManyToMany, Property } from '@mikro-orm/core';
import { BaseEntity } from './BaseEntity.js';
import { Channel } from './Channel.js';
import { Program } from './Program.js';
import { FillerListContent } from './FillerListContent.js';

@Entity()
export class FillerShow extends BaseEntity {
@Property()
name!: string;

@ManyToMany(() => Program, 'fillerShows', { owner: true })
@ManyToMany({ entity: () => Program, pivotEntity: () => FillerListContent })
content = new Collection<Program>(this);

@ManyToMany({ entity: () => Channel, mappedBy: (channel) => channel.fillers })
Expand Down
4 changes: 3 additions & 1 deletion server/dao/entities/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ export class Program extends BaseEntity {
})
customShows = new Collection<CustomShow>(this);

@ManyToMany(() => FillerShow, (fillerShow) => fillerShow.content, {
@ManyToMany({
entity: () => FillerShow,
eager: false,
mappedBy: (e) => e.content,
})
fillerShows = new Collection<FillerShow>(this);

Expand Down
23 changes: 21 additions & 2 deletions server/migrations/.snapshot-db.db.json
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,15 @@
"primary": false,
"nullable": false,
"mappedType": "text"
},
"index": {
"name": "index",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
}
},
"name": "filler_show_content",
Expand All @@ -961,6 +970,18 @@
"primary": false,
"unique": false
},
{
"keyName": "filler_show_content_filler_show_uuid_program_uuid_index_unique",
"columnNames": [
"filler_show_uuid",
"program_uuid",
"index"
],
"composite": true,
"constraint": true,
"primary": false,
"unique": true
},
{
"keyName": "primary",
"columnNames": [
Expand All @@ -985,7 +1006,6 @@
"uuid"
],
"referencedTableName": "filler_show",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"filler_show_content_program_uuid_foreign": {
Expand All @@ -998,7 +1018,6 @@
"uuid"
],
"referencedTableName": "program",
"deleteRule": "cascade",
"updateRule": "cascade"
}
},
Expand Down
14 changes: 14 additions & 0 deletions server/migrations/Migration20240126165808.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240126165808 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table `filler_show_content` add column `index` integer not null constraint `filler_show_content_filler_show_uuid_foreign` references `filler_show` (`uuid`) on update cascade constraint `filler_show_content_program_uuid_foreign` references `program` (`uuid`) on update cascade;',
);
this.addSql(
'create unique index `filler_show_content_filler_show_uuid_program_uuid_index_unique` on `filler_show_content` (`filler_show_uuid`, `program_uuid`, `index`);',
);

// We're not going to add index values since these tables should be empty at the moment anyway
}
}

0 comments on commit e1c6ac6

Please sign in to comment.