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('backfills renown from level BEFORE dropping the level column', async () => { const up = await runUp(); const backfillIndex = up.findIndex((sql) => sql.includes('LEAST(GREATEST("level", 1), 15)'), ); const dropLevelIndex = up.findIndex((sql) => sql.includes('DROP COLUMN "level"'), ); expect(backfillIndex).toBeGreaterThanOrEqual(0); expect(dropLevelIndex).toBeGreaterThanOrEqual(0); // Reversing these two would silently discard every character's progression. expect(backfillIndex).toBeLessThan(dropLevelIndex); }); it('adds the renown range constraint only AFTER the backfill has populated valid values', async () => { const up = await runUp(); const backfillIndex = up.findIndex((sql) => sql.includes('LEAST(GREATEST("level", 1), 15)'), ); const constraintIndex = up.findIndex((sql) => sql.includes('CHK_characters_renown'), ); expect(backfillIndex).toBeGreaterThanOrEqual(0); expect(constraintIndex).toBeGreaterThanOrEqual(0); // ADD CONSTRAINT validates the whole table; running it before the backfill // would only pass by coincidence of the column DEFAULT being in range. expect(backfillIndex).toBeLessThan(constraintIndex); }); 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"', ), ]), ); }); /** * XP is abolished outright (design R7), so the column goes with the concept. * It is NOT NULL with no default, so leaving it behind while the seed stops * supplying a value would fail every monster insert against a real database * -- a break no suite here can catch, since none of them connect to Postgres. */ it('drops experience_reward from monster_definitions', async () => { const up = await runUp(); expect(up).toEqual( expect.arrayContaining([ expect.stringContaining( 'ALTER TABLE "monster_definitions" DROP COLUMN "experience_reward"', ), ]), ); }); it('keeps silver_min and silver_max on monster_definitions', async () => { const up = await runUp(); expect(up.join(' ')).not.toContain('"silver_min"'); expect(up.join(' ')).not.toContain('"silver_max"'); }); 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 "monster_definitions" ADD COLUMN "experience_reward"', ), 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"', ), ]), ); const turnInDropIndex = down.findIndex((sql) => sql.includes('DROP TABLE "turn_in_definitions"'), ); const reputationFactionsDropIndex = down.findIndex((sql) => sql.includes('DROP TABLE "reputation_factions"'), ); expect(turnInDropIndex).toBeGreaterThanOrEqual(0); expect(reputationFactionsDropIndex).toBeGreaterThanOrEqual(0); // turn_in_definitions has FK dependencies on reputation_factions and // item_definitions that must be gone before those tables are touched. expect(turnInDropIndex).toBeLessThan(reputationFactionsDropIndex); }); });