feat(renown): migrate Character/ItemDefinition schema and add Reputation/Renown/TurnIn tables

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UT2tLMQ2HfbWGkZyotocKm
This commit is contained in:
Bastian Wagner
2026-08-21 08:48:54 +02:00
parent 88b98aa605
commit 1a50e817f1
2 changed files with 319 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class CreateRenownAndReputation1791000000000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
// --- Character: level/experience -> renown (spec §13, design R1/R6) ---
await queryRunner.query(
'ALTER TABLE "characters" ADD COLUMN "renown" integer NOT NULL DEFAULT 1',
);
await queryRunner.query(
'UPDATE "characters" SET "renown" = LEAST(GREATEST("level", 1), 15)',
);
await queryRunner.query(
'ALTER TABLE "characters" ADD CONSTRAINT "CHK_characters_renown" CHECK ("renown" >= 1 AND "renown" <= 15)',
);
await queryRunner.query('ALTER TABLE "characters" DROP COLUMN "level"');
await queryRunner.query('ALTER TABLE "characters" DROP COLUMN "experience"');
// --- ItemDefinition: drop requiredLevel (spec §14, design R4) ---
await queryRunner.query(
'ALTER TABLE "item_definitions" DROP COLUMN "required_level"',
);
// --- ItemType enum rebuild (spec §16, design R3): WEAPON/ARMOR -> ---
// EQUIPMENT, MATERIAL -> TRADE_GOOD, add TROPHY/QUEST_ITEM. Postgres has
// no ALTER TYPE ... RENAME VALUE across all supported versions here, so
// this converts the column to text, migrates the data, rebuilds the
// type, and converts back -- the same technique already used by
// 1790000000000-ExtendCombatEventTypes.ts's down().
await queryRunner.query(
'ALTER TABLE "item_definitions" ALTER COLUMN "type" TYPE varchar USING "type"::text',
);
await queryRunner.query(
`UPDATE "item_definitions" SET "type" = 'EQUIPMENT' WHERE "type" IN ('WEAPON', 'ARMOR')`,
);
await queryRunner.query(
`UPDATE "item_definitions" SET "type" = 'TRADE_GOOD' WHERE "type" = 'MATERIAL'`,
);
await queryRunner.query('DROP TYPE "item_type_enum"');
await queryRunner.query(
`CREATE TYPE "item_type_enum" AS ENUM ('EQUIPMENT', 'TRADE_GOOD', 'TROPHY', 'QUEST_ITEM', 'CONSUMABLE')`,
);
await queryRunner.query(
'ALTER TABLE "item_definitions" ALTER COLUMN "type" TYPE "item_type_enum" USING "type"::"item_type_enum"',
);
// --- CombatReward: drop the XP audit column (spec §1, design R8) ---
await queryRunner.query(
'ALTER TABLE "combat_rewards" DROP COLUMN "experience_granted"',
);
// --- ReputationFaction (spec §9) ---
await queryRunner.query(`CREATE TABLE "reputation_factions" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"key" character varying(100) NOT NULL,
"name" character varying(150) NOT NULL,
"description" text NOT NULL,
"region_key" character varying(100) NOT NULL,
"enabled" boolean NOT NULL DEFAULT true,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
CONSTRAINT "PK_reputation_factions" PRIMARY KEY ("id")
)`);
await queryRunner.query(
'CREATE UNIQUE INDEX "IDX_reputation_factions_key" ON "reputation_factions" ("key")',
);
// --- CharacterReputation (spec §9) ---
await queryRunner.query(`CREATE TABLE "character_reputation" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"character_id" uuid NOT NULL,
"faction_id" uuid NOT NULL,
"reputation" integer NOT NULL DEFAULT 0,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
CONSTRAINT "PK_character_reputation" PRIMARY KEY ("id"),
CONSTRAINT "FK_character_reputation_character" FOREIGN KEY ("character_id") REFERENCES "characters"("id") ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT "FK_character_reputation_faction" FOREIGN KEY ("faction_id") REFERENCES "reputation_factions"("id") ON DELETE RESTRICT ON UPDATE NO ACTION
)`);
await queryRunner.query(
'CREATE UNIQUE INDEX "IDX_character_reputation_character_faction" ON "character_reputation" ("character_id", "faction_id")',
);
// --- RenownMilestoneDefinition (spec §5) ---
await queryRunner.query(`CREATE TABLE "renown_milestone_definitions" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"key" character varying(100) NOT NULL,
"name" character varying(150) NOT NULL,
"description" text NOT NULL,
"renown_reward" integer NOT NULL,
"repeatable" boolean NOT NULL DEFAULT false,
"enabled" boolean NOT NULL DEFAULT true,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
CONSTRAINT "PK_renown_milestone_definitions" PRIMARY KEY ("id")
)`);
await queryRunner.query(
'CREATE UNIQUE INDEX "IDX_renown_milestone_definitions_key" ON "renown_milestone_definitions" ("key")',
);
// --- CharacterRenownMilestone (spec §5) ---
await queryRunner.query(`CREATE TABLE "character_renown_milestones" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"character_id" uuid NOT NULL,
"milestone_id" uuid NOT NULL,
"completed_at" TIMESTAMP WITH TIME ZONE NOT NULL,
"times_completed" integer NOT NULL DEFAULT 1,
CONSTRAINT "PK_character_renown_milestones" PRIMARY KEY ("id"),
CONSTRAINT "FK_character_renown_milestones_character" FOREIGN KEY ("character_id") REFERENCES "characters"("id") ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT "FK_character_renown_milestones_milestone" FOREIGN KEY ("milestone_id") REFERENCES "renown_milestone_definitions"("id") ON DELETE RESTRICT ON UPDATE NO ACTION
)`);
await queryRunner.query(
'CREATE UNIQUE INDEX "IDX_character_renown_milestones_character_milestone" ON "character_renown_milestones" ("character_id", "milestone_id")',
);
// --- TurnInDefinition (spec §19) ---
await queryRunner.query(`CREATE TABLE "turn_in_definitions" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"key" character varying(100) NOT NULL,
"item_definition_id" uuid NOT NULL,
"faction_id" uuid NOT NULL,
"silver_reward_per_item" integer NOT NULL,
"reputation_reward_per_item" integer NOT NULL,
"repeatable" boolean NOT NULL DEFAULT true,
"enabled" boolean NOT NULL DEFAULT true,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
CONSTRAINT "PK_turn_in_definitions" PRIMARY KEY ("id"),
CONSTRAINT "FK_turn_in_definitions_item_definition" FOREIGN KEY ("item_definition_id") REFERENCES "item_definitions"("id") ON DELETE RESTRICT ON UPDATE NO ACTION,
CONSTRAINT "FK_turn_in_definitions_faction" FOREIGN KEY ("faction_id") REFERENCES "reputation_factions"("id") ON DELETE RESTRICT ON UPDATE NO ACTION
)`);
await queryRunner.query(
'CREATE UNIQUE INDEX "IDX_turn_in_definitions_key" ON "turn_in_definitions" ("key")',
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('DROP TABLE "turn_in_definitions"');
await queryRunner.query(
'DROP INDEX "IDX_character_renown_milestones_character_milestone"',
);
await queryRunner.query('DROP TABLE "character_renown_milestones"');
await queryRunner.query('DROP INDEX "IDX_renown_milestone_definitions_key"');
await queryRunner.query('DROP TABLE "renown_milestone_definitions"');
await queryRunner.query('DROP INDEX "IDX_character_reputation_character_faction"');
await queryRunner.query('DROP TABLE "character_reputation"');
await queryRunner.query('DROP INDEX "IDX_reputation_factions_key"');
await queryRunner.query('DROP TABLE "reputation_factions"');
await queryRunner.query(
'ALTER TABLE "combat_rewards" ADD COLUMN "experience_granted" integer NOT NULL DEFAULT 0',
);
await queryRunner.query(
'ALTER TABLE "item_definitions" ALTER COLUMN "type" TYPE varchar USING "type"::text',
);
await queryRunner.query('DROP TYPE "item_type_enum"');
await queryRunner.query(
"CREATE TYPE \"item_type_enum\" AS ENUM ('WEAPON', 'ARMOR', 'MATERIAL', 'CONSUMABLE')",
);
await queryRunner.query(
`UPDATE "item_definitions" SET "type" = 'MATERIAL' WHERE "type" = 'TRADE_GOOD'`,
);
// TROPHY/QUEST_ITEM/EQUIPMENT have no clean pre-image; best-effort revert
// for genuinely disposable dev data (design R5).
await queryRunner.query(
`UPDATE "item_definitions" SET "type" = 'WEAPON' WHERE "type" IN ('EQUIPMENT', 'TROPHY', 'QUEST_ITEM')`,
);
await queryRunner.query(
'ALTER TABLE "item_definitions" ALTER COLUMN "type" TYPE "item_type_enum" USING "type"::"item_type_enum"',
);
await queryRunner.query(
'ALTER TABLE "item_definitions" ADD COLUMN "required_level" integer NOT NULL DEFAULT 1',
);
await queryRunner.query('ALTER TABLE "characters" ADD COLUMN "level" integer NOT NULL DEFAULT 1');
await queryRunner.query(
'ALTER TABLE "characters" ADD COLUMN "experience" integer NOT NULL DEFAULT 0',
);
await queryRunner.query('UPDATE "characters" SET "level" = "renown"');
await queryRunner.query('ALTER TABLE "characters" DROP CONSTRAINT "CHK_characters_renown"');
await queryRunner.query('ALTER TABLE "characters" DROP COLUMN "renown"');
}
}