import { QueryRunner } from 'typeorm'; import { CreateRenownAndReputation1791000000000 } from './1791000000000-CreateRenownAndReputation'; describe('CreateRenownAndReputation1791000000000', () => { async function runUp() { const query = jest.fn().mockResolvedValue(undefined); const queryRunner = { query } as unknown as QueryRunner; const migration = new CreateRenownAndReputation1791000000000(); await migration.up(queryRunner); return query.mock.calls.map(([sql]) => sql as string); } async function runUpThenDown() { const query = jest.fn().mockResolvedValue(undefined); const queryRunner = { query } as unknown as QueryRunner; const migration = new CreateRenownAndReputation1791000000000(); await migration.up(queryRunner); const upCount = query.mock.calls.length; await migration.down(queryRunner); return query.mock.calls.slice(upCount).map(([sql]) => sql as string); } it('replaces level/experience with a renown column on characters', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([ expect.stringContaining('ALTER TABLE "characters" ADD COLUMN "renown"'), expect.stringContaining('DROP COLUMN "level"'), expect.stringContaining('DROP COLUMN "experience"'), ]), ); }); it('clamps existing level into the 1-15 Renown range before dropping it', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([expect.stringContaining('LEAST(GREATEST("level", 1), 15)')]), ); }); it('enforces the renown range at the database level', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([ expect.stringContaining('CHK_characters_renown'), ]), ); }); it('drops required_level from item_definitions', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([ expect.stringContaining('ALTER TABLE "item_definitions" DROP COLUMN "required_level"'), ]), ); }); it('rebuilds item_type_enum with exactly the five Slice 0.6.5 values, migrating existing rows', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([ expect.stringContaining(`SET "type" = 'EQUIPMENT' WHERE "type" IN ('WEAPON', 'ARMOR')`), expect.stringContaining(`SET "type" = 'TRADE_GOOD' WHERE "type" = 'MATERIAL'`), expect.stringContaining('DROP TYPE "item_type_enum"'), expect.stringContaining( `CREATE TYPE "item_type_enum" AS ENUM ('EQUIPMENT', 'TRADE_GOOD', 'TROPHY', 'QUEST_ITEM', 'CONSUMABLE')`, ), ]), ); }); it('drops experience_granted from combat_rewards', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([ expect.stringContaining('ALTER TABLE "combat_rewards" DROP COLUMN "experience_granted"'), ]), ); }); it('creates all five new tables with their unique constraints', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([ expect.stringContaining('CREATE TABLE "reputation_factions"'), expect.stringContaining('CREATE UNIQUE INDEX "IDX_reputation_factions_key"'), expect.stringContaining('CREATE TABLE "character_reputation"'), expect.stringContaining( 'CREATE UNIQUE INDEX "IDX_character_reputation_character_faction" ON "character_reputation" ("character_id", "faction_id")', ), expect.stringContaining('CREATE TABLE "renown_milestone_definitions"'), expect.stringContaining('CREATE UNIQUE INDEX "IDX_renown_milestone_definitions_key"'), expect.stringContaining('CREATE TABLE "character_renown_milestones"'), expect.stringContaining( 'CREATE UNIQUE INDEX "IDX_character_renown_milestones_character_milestone" ON "character_renown_milestones" ("character_id", "milestone_id")', ), expect.stringContaining('CREATE TABLE "turn_in_definitions"'), expect.stringContaining('CREATE UNIQUE INDEX "IDX_turn_in_definitions_key"'), ]), ); }); it('down() reverses every up() step in exact opposite order, dropping the five new tables first', async () => { const down = await runUpThenDown(); expect(down).toEqual( expect.arrayContaining([ expect.stringContaining('DROP TABLE "turn_in_definitions"'), expect.stringContaining('DROP TABLE "character_renown_milestones"'), expect.stringContaining('DROP TABLE "renown_milestone_definitions"'), expect.stringContaining('DROP TABLE "character_reputation"'), expect.stringContaining('DROP TABLE "reputation_factions"'), expect.stringContaining('ALTER TABLE "combat_rewards" ADD COLUMN "experience_granted"'), expect.stringContaining('ALTER TABLE "item_definitions" ADD COLUMN "required_level"'), expect.stringContaining('ALTER TABLE "characters" ADD COLUMN "level"'), expect.stringContaining('ALTER TABLE "characters" ADD COLUMN "experience"'), expect.stringContaining('ALTER TABLE "characters" DROP COLUMN "renown"'), ]), ); // The dropped turn_in_definitions table must be the very first statement // in down() -- it's the last thing up() created, and it has FK // dependencies on reputation_factions/item_definitions that must be // gone before those tables can be touched. expect(down[0]).toContain('DROP TABLE "turn_in_definitions"'); }); });