-- Import legacy card rows into catalog.card_definitions. -- -- Usage: -- 1. Load your old rows into temp_legacy_cards (COPY, INSERT, psql \copy, etc.). -- 2. Run the INSERT ... SELECT below. -- -- Notes: -- - This script targets the current Postgres schema used by SOR.Database. -- - It intentionally does NOT preserve the old CardId as the new primary key. -- - ArchSpirit tier chains are preserved by mapping legacy ParentCardId -> new UUID. -- - Repentance payloads cannot be losslessly converted yet because the old -- Repentances[] ids do not map directly to the new Action/Effect DSL in this repo. -- For those rows, a placeholder JSON payload is written to "effects" so the legacy -- ids are preserved for manual follow-up. -- - Card assets (CardArt / CardModelName) are not imported here; this only populates -- catalog.card_definitions. -- -- Legacy enum assumptions used here: -- Type: -- 1 = Spirit -- 3 = Repentance -- 5 = Terramorpha -- 6 = ArchSpirit -- Affinity: -- 1 = Void -- 2 = Lake -- 3 = Hill -- 4 = Field -- 5 = Forest -- 6 = Aether -- 7 = Ocean -- 8 = Island -- 9 = Marsh -- 10 = Swamp -- 11 = Mountain -- 12 = Steppe -- 13 = Fort -- 14 = Plains -- 15 = Grove -- 16 = Jungle -- -- If your old DB used a different numeric enum layout, update the two CASE maps below -- before running this script. CREATE EXTENSION IF NOT EXISTS pgcrypto; DROP TABLE IF EXISTS temp_legacy_cards; CREATE TEMP TABLE temp_legacy_cards ( legacy_card_id integer PRIMARY KEY, name text NOT NULL, description text NOT NULL, legacy_type integer NOT NULL, sp_cost integer NOT NULL, st_cost integer NOT NULL, legacy_affinity integer NOT NULL, card_data jsonb NOT NULL ); -- Paste the raw legacy SQL tuple blob below (the exact format you pasted in chat), -- replacing the placeholder line. Example row format: -- '146', 'Barrier of Unseen', 'A Barrier of nothing', '1', '4', '1', '1', '{\"CardGenus\": [\"Barrier\"]}' WITH raw_blob AS ( SELECT $blob$ PASTE_LEGACY_ROWS_HERE $blob$ AS blob ), parsed_rows AS ( SELECT (m)[1]::integer AS legacy_card_id, regexp_replace((m)[2], E'\\\\''', '''', 'g') AS name, regexp_replace((m)[3], E'\\\\''', '''', 'g') AS description, (m)[4]::integer AS legacy_type, (m)[5]::integer AS sp_cost, (m)[6]::integer AS st_cost, (m)[7]::integer AS legacy_affinity, replace( regexp_replace((m)[8], E'\\\\''', '''', 'g'), E'\\\"', '"' )::jsonb AS card_data FROM raw_blob rb CROSS JOIN LATERAL regexp_matches( rb.blob, '''((?:[^''\\]|\\.)*)''\s*,\s*''((?:[^''\\]|\\.)*)''\s*,\s*''((?:[^''\\]|\\.)*)''\s*,\s*''((?:[^''\\]|\\.)*)''\s*,\s*''((?:[^''\\]|\\.)*)''\s*,\s*''((?:[^''\\]|\\.)*)''\s*,\s*''((?:[^''\\]|\\.)*)''\s*,\s*''((?:[^''\\]|\\.)*)''', 'g' ) AS m ) INSERT INTO temp_legacy_cards (legacy_card_id, name, description, legacy_type, sp_cost, st_cost, legacy_affinity, card_data) SELECT legacy_card_id, name, description, legacy_type, sp_cost, st_cost, legacy_affinity, card_data FROM parsed_rows; WITH source_rows AS ( SELECT l.*, gen_random_uuid() AS new_id, CASE l.legacy_type WHEN 1 THEN 'Spirit' WHEN 3 THEN 'Repentance' WHEN 5 THEN 'Terramorpha' WHEN 6 THEN 'ArchSpirit' ELSE NULL END AS mapped_card_type, CASE l.legacy_affinity WHEN 1 THEN 'Void' WHEN 2 THEN 'Lake' WHEN 3 THEN 'Hill' WHEN 4 THEN 'Field' WHEN 5 THEN 'Forest' WHEN 6 THEN 'Aether' WHEN 7 THEN 'Ocean' WHEN 8 THEN 'Island' WHEN 9 THEN 'Marsh' WHEN 10 THEN 'Swamp' WHEN 11 THEN 'Mountain' WHEN 12 THEN 'Steppe' WHEN 13 THEN 'Fort' WHEN 14 THEN 'Plains' WHEN 15 THEN 'Grove' WHEN 16 THEN 'Jungle' ELSE NULL END AS mapped_affinity, CASE (l.card_data ->> 'AttackType') WHEN '0' THEN 'Melee' WHEN '1' THEN 'Ranged' ELSE NULL END AS mapped_attack_type, CASE (l.card_data ->> 'TerraType') WHEN '1' THEN 'Mono' WHEN '2' THEN 'Di' WHEN '3' THEN 'Tri' ELSE NULL END AS mapped_terramorpha_mode, NULLIF(l.card_data ->> 'ParentCardId', '')::integer AS legacy_parent_card_id FROM temp_legacy_cards l ), validated AS ( SELECT * FROM source_rows WHERE mapped_card_type IS NOT NULL AND mapped_affinity IS NOT NULL ), inserted AS ( INSERT INTO catalog.card_definitions ( "Id", "Name", "Description", "CardType", "Affinity", "SpCost", "StCost", "OtherCosts", "MinChannelRange", "MaxChannelRange", "AttackType", "RequiresLos", "MinAttackRange", "MaxAttackRange", "CanAttack", "Damage", "CanDefend", "MaxHealth", "MaxDefense", "CanBeAttacked", "CanBeBlocked", "CanMove", "MovementType", "MovementPoints", "CanBeObstructed", "CanUseAbility", "Genus", "CanBeSacrificed", "Tier", "PreviousTierId", "TerramorphaMode", "Abilities", "Effects", "Rarity", "SetId", "CollectorNumber", "IsPublished", "IsRetired", "RetiredAt", "CreatedAt", "UpdatedAt" ) SELECT s.new_id, s.name, COALESCE(s.description, ''), s.mapped_card_type, s.mapped_affinity, s.sp_cost, s.st_cost, NULL, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardChannelMinRange', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardChannelMaxRange', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') AND s.card_data ? 'AttackType' THEN s.mapped_attack_type ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') AND s.card_data ? 'AttackType' THEN false ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardAttackMinRange', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardAttackMaxRange', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN COALESCE((s.card_data ->> 'CanAttack')::boolean, false) ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardAttack', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') AND s.card_data ? 'CardDefense' THEN true ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardHealthPoints', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardDefense', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN true ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN true ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN COALESCE((s.card_data ->> 'CanMove')::boolean, false) ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') AND COALESCE((s.card_data ->> 'CanMove')::boolean, false) THEN 'Walk' ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN NULLIF(s.card_data ->> 'CardMovementPoints', '')::integer ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN true ELSE NULL END, CASE WHEN s.mapped_card_type IN ('Spirit', 'ArchSpirit') THEN COALESCE( NULLIF(s.card_data ->> 'CardCanUseAbilities', '')::boolean, jsonb_typeof(s.card_data -> 'Abilities') = 'array' AND jsonb_array_length(s.card_data -> 'Abilities') > 0, false) ELSE NULL END, CASE WHEN s.card_data ? 'CardGenus' THEN ARRAY(SELECT jsonb_array_elements_text(s.card_data -> 'CardGenus')) ELSE NULL END, CASE WHEN s.mapped_card_type = 'Spirit' THEN true ELSE NULL END, CASE WHEN s.mapped_card_type = 'ArchSpirit' THEN COALESCE(NULLIF(s.card_data ->> 'CardTier', '')::integer, 1) ELSE NULL END, parent.new_id, CASE WHEN s.mapped_card_type = 'Terramorpha' THEN s.mapped_terramorpha_mode ELSE NULL END, CASE WHEN s.card_data ? 'Abilities' AND jsonb_typeof(s.card_data -> 'Abilities') = 'array' THEN s.card_data -> 'Abilities' ELSE NULL END, CASE WHEN s.mapped_card_type = 'Repentance' AND s.card_data ? 'Repentances' THEN jsonb_build_array( jsonb_build_object( 'LegacyRepentanceIds', s.card_data -> 'Repentances', 'MigrationNote', 'Manual conversion to new Action/Effect DSL required')) ELSE NULL END, 'Common', NULL, NULL, true, false, NULL, now(), now() FROM validated s LEFT JOIN validated parent ON parent.legacy_card_id = s.legacy_parent_card_id RETURNING "Id", "Name" ) SELECT count(*) AS inserted_count FROM inserted;