
CREATE OR REPLACE FUNCTION tcg.public.FN_before_insert_TCG_MTG_Card()
RETURNS TRIGGER AS $$
DECLARE
    r_change_set RECORD;
BEGIN
    NEW.created_on = COALESCE(NEW.created_on, CURRENT_TIMESTAMP);
    NEW.updated_last_on = COALESCE(NEW.updated_last_on, CURRENT_TIMESTAMP);

    IF NEW.change_set_id IS NULL THEN
		RAISE EXCEPTION 'Change Set ID must be provided.';
    END IF;

    SELECT *
    INTO r_change_set
    FROM tcg.public.TCG_Change_Set CHANGE_SET
    WHERE NEW.change_set_id = CHANGE_SET.change_set_id
    ;

    IF FOUND THEN
        NEW.created_by_user_id := COALESCE(NEW.created_by_user_id, r_change_set.updated_last_by_user_id);
        NEW.updated_last_by_user_id := COALESCE(NEW.updated_last_by_user_id, r_change_set.updated_last_by_user_id);
        NEW.created_on := COALESCE(NEW.created_on, r_change_set.updated_last_on);
    ELSE
        RAISE EXCEPTION 'Change Set % not found.', NEW.change_set_id;
    END IF;
    
    RETURN NEW;
END;
$$ LANGUAGE plpgsql
;

CREATE OR REPLACE FUNCTION tcg.public.FN_before_update_TCG_MTG_Card()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_last_on = CURRENT_TIMESTAMP;

    IF OLD.change_set_id IS NOT DISTINCT FROM NEW.change_set_id THEN
		RAISE EXCEPTION 'New Change Set ID must be provided.';
    END IF;

    INSERT INTO tcg.public.TCG_MTG_Card_Audit (
		  card_id
        , name_field
        , value_prev
        , value_new
        , change_set_id
    )
	-- Changed all_parts
	SELECT NEW.card_id, 'all_parts', OLD.all_parts, NEW.all_parts, NEW.change_set_id
		WHERE OLD.all_parts IS NOT DISTINCT FROM NEW.all_parts
	UNION
	-- Changed arena_id
	SELECT NEW.card_id, 'arena_id', OLD.arena_id::TEXT, NEW.arena_id::TEXT, NEW.change_set_id
		WHERE OLD.arena_id IS NOT DISTINCT FROM NEW.arena_id
	UNION
	-- Changed artist
	SELECT NEW.card_id, 'artist', OLD.artist, NEW.artist, NEW.change_set_id
		WHERE OLD.artist IS NOT DISTINCT FROM NEW.artist
	UNION
	-- Changed artist_ids
	SELECT NEW.card_id, 'artist_ids', OLD.artist_ids, NEW.artist_ids, NEW.change_set_id
		WHERE OLD.artist_ids IS NOT DISTINCT FROM NEW.artist_ids
	UNION
	-- Changed booster
	SELECT NEW.card_id, 'booster', OLD.booster::TEXT, NEW.booster::TEXT, NEW.change_set_id
		WHERE OLD.booster IS NOT DISTINCT FROM NEW.booster
	UNION
	-- Changed border_color
	SELECT NEW.card_id, 'border_color', OLD.border_color, NEW.border_color, NEW.change_set_id
		WHERE OLD.border_color IS NOT DISTINCT FROM NEW.border_color
	UNION
	-- Changed card_back_id
	SELECT NEW.card_id, 'card_back_id', OLD.card_back_id, NEW.card_back_id, NEW.change_set_id
		WHERE OLD.card_back_id IS NOT DISTINCT FROM NEW.card_back_id
	UNION
	-- Changed cmc
	SELECT NEW.card_id, 'cmc', OLD.cmc::TEXT, NEW.cmc::TEXT, NEW.change_set_id
		WHERE OLD.cmc IS NOT DISTINCT FROM NEW.cmc
	UNION
	-- Changed collector_number
	SELECT NEW.card_id, 'collector_number', OLD.collector_number, NEW.collector_number, NEW.change_set_id
		WHERE OLD.collector_number IS NOT DISTINCT FROM NEW.collector_number
	UNION
	-- Changed color_identity
	SELECT NEW.card_id, 'color_identity', OLD.color_identity, NEW.color_identity, NEW.change_set_id
		WHERE OLD.color_identity IS NOT DISTINCT FROM NEW.color_identity
	UNION
	-- Changed colors
	SELECT NEW.card_id, 'colors', OLD.colors, NEW.colors, NEW.change_set_id
		WHERE OLD.colors IS NOT DISTINCT FROM NEW.colors
	UNION
	-- Changed content_warning
	SELECT NEW.card_id, 'content_warning', OLD.content_warning::TEXT, NEW.content_warning::TEXT, NEW.change_set_id
		WHERE OLD.content_warning IS NOT DISTINCT FROM NEW.content_warning
	UNION
	-- Changed digital
	SELECT NEW.card_id, 'digital', OLD.digital::TEXT, NEW.digital::TEXT, NEW.change_set_id
		WHERE OLD.digital IS NOT DISTINCT FROM NEW.digital
	UNION
	-- Changed edhrec_rank
	SELECT NEW.card_id, 'edhrec_rank', OLD.edhrec_rank::TEXT, NEW.edhrec_rank::TEXT, NEW.change_set_id
		WHERE OLD.edhrec_rank IS NOT DISTINCT FROM NEW.edhrec_rank
	UNION
	-- Changed finishes
	SELECT NEW.card_id, 'finishes', OLD.finishes, NEW.finishes, NEW.change_set_id
		WHERE OLD.finishes IS NOT DISTINCT FROM NEW.finishes
	UNION
	-- Changed flavor_name
	SELECT NEW.card_id, 'flavor_name', OLD.flavor_name, NEW.flavor_name, NEW.change_set_id
		WHERE OLD.flavor_name IS NOT DISTINCT FROM NEW.flavor_name
	UNION
	-- Changed flavor_text
	SELECT NEW.card_id, 'flavor_text', OLD.flavor_text, NEW.flavor_text, NEW.change_set_id
		WHERE OLD.flavor_text IS NOT DISTINCT FROM NEW.flavor_text
	UNION
	-- Changed foil
	SELECT NEW.card_id, 'foil', OLD.foil::TEXT, NEW.foil::TEXT, NEW.change_set_id
		WHERE OLD.foil IS NOT DISTINCT FROM NEW.foil
	UNION
	-- Changed frame
	SELECT NEW.card_id, 'frame', OLD.frame, NEW.frame, NEW.change_set_id
		WHERE OLD.frame IS NOT DISTINCT FROM NEW.frame
	UNION
	-- Changed full_art
	SELECT NEW.card_id, 'full_art', OLD.full_art::TEXT, NEW.full_art::TEXT, NEW.change_set_id
		WHERE OLD.full_art IS NOT DISTINCT FROM NEW.full_art
	UNION
	-- Changed game_changer
	SELECT NEW.card_id, 'game_changer', OLD.game_changer::TEXT, NEW.game_changer::TEXT, NEW.change_set_id
		WHERE OLD.game_changer IS NOT DISTINCT FROM NEW.game_changer
	UNION
	-- Changed games
	SELECT NEW.card_id, 'games', OLD.games, NEW.games, NEW.change_set_id
		WHERE OLD.games IS NOT DISTINCT FROM NEW.games
	UNION
	-- Changed hand_modifier
	SELECT NEW.card_id, 'hand_modifier', OLD.hand_modifier, NEW.hand_modifier, NEW.change_set_id
		WHERE OLD.hand_modifier IS NOT DISTINCT FROM NEW.hand_modifier
	UNION
	-- Changed highres_image
	SELECT NEW.card_id, 'highres_image', OLD.highres_image::TEXT, NEW.highres_image::TEXT, NEW.change_set_id
		WHERE OLD.highres_image IS NOT DISTINCT FROM NEW.highres_image
	UNION
	-- Changed id
	SELECT NEW.card_id, 'id', OLD.id::TEXT, NEW.id::TEXT, NEW.change_set_id
		WHERE OLD.id IS NOT DISTINCT FROM NEW.id
	UNION
	-- Changed image_status
	SELECT NEW.card_id, 'image_status', OLD.image_status, NEW.image_status, NEW.change_set_id
		WHERE OLD.image_status IS NOT DISTINCT FROM NEW.image_status
	UNION
	-- Changed image_uri_art_crop
	SELECT NEW.card_id, 'image_uri_art_crop', OLD.image_uri_art_crop, NEW.image_uri_art_crop, NEW.change_set_id
		WHERE OLD.image_uri_art_crop IS NOT DISTINCT FROM NEW.image_uri_art_crop
	UNION
	-- Changed image_uri_border_crop
	SELECT NEW.card_id, 'image_uri_border_crop', OLD.image_uri_border_crop, NEW.image_uri_border_crop, NEW.change_set_id
		WHERE OLD.image_uri_border_crop IS NOT DISTINCT FROM NEW.image_uri_border_crop
	UNION
	-- Changed image_uri_large
	SELECT NEW.card_id, 'image_uri_large', OLD.image_uri_large, NEW.image_uri_large, NEW.change_set_id
		WHERE OLD.image_uri_large IS NOT DISTINCT FROM NEW.image_uri_large
	UNION
	-- Changed image_uri_normal
	SELECT NEW.card_id, 'image_uri_normal', OLD.image_uri_normal, NEW.image_uri_normal, NEW.change_set_id
		WHERE OLD.image_uri_normal IS NOT DISTINCT FROM NEW.image_uri_normal
	UNION
	-- Changed image_uri_png
	SELECT NEW.card_id, 'image_uri_png', OLD.image_uri_png, NEW.image_uri_png, NEW.change_set_id
		WHERE OLD.image_uri_png IS NOT DISTINCT FROM NEW.image_uri_png
	UNION
	-- Changed image_uri_small
	SELECT NEW.card_id, 'image_uri_small', OLD.image_uri_small, NEW.image_uri_small, NEW.change_set_id
		WHERE OLD.image_uri_small IS NOT DISTINCT FROM NEW.image_uri_small
	UNION
	-- Changed keywords
	SELECT NEW.card_id, 'keywords', OLD.keywords, NEW.keywords, NEW.change_set_id
		WHERE OLD.keywords IS NOT DISTINCT FROM NEW.keywords
	UNION
	-- Changed lang
	SELECT NEW.card_id, 'lang', OLD.lang, NEW.lang, NEW.change_set_id
		WHERE OLD.lang IS NOT DISTINCT FROM NEW.lang
	UNION
	-- Changed layout
	SELECT NEW.card_id, 'layout', OLD.layout, NEW.layout, NEW.change_set_id
		WHERE OLD.layout IS NOT DISTINCT FROM NEW.layout
	UNION
	-- Changed legal_alchemy
	SELECT NEW.card_id, 'legal_alchemy', OLD.legal_alchemy::TEXT, NEW.legal_alchemy::TEXT, NEW.change_set_id
		WHERE OLD.legal_alchemy IS NOT DISTINCT FROM NEW.legal_alchemy
	UNION
	-- Changed legal_brawl
	SELECT NEW.card_id, 'legal_brawl', OLD.legal_brawl::TEXT, NEW.legal_brawl::TEXT, NEW.change_set_id
		WHERE OLD.legal_brawl IS NOT DISTINCT FROM NEW.legal_brawl
	UNION
	-- Changed legal_commander
	SELECT NEW.card_id, 'legal_commander', OLD.legal_commander::TEXT, NEW.legal_commander::TEXT, NEW.change_set_id
		WHERE OLD.legal_commander IS NOT DISTINCT FROM NEW.legal_commander
	UNION
	-- Changed legal_duel
	SELECT NEW.card_id, 'legal_duel', OLD.legal_duel::TEXT, NEW.legal_duel::TEXT, NEW.change_set_id
		WHERE OLD.legal_duel IS NOT DISTINCT FROM NEW.legal_duel
	UNION
	-- Changed legal_future
	SELECT NEW.card_id, 'legal_future', OLD.legal_future::TEXT, NEW.legal_future::TEXT, NEW.change_set_id
		WHERE OLD.legal_future IS NOT DISTINCT FROM NEW.legal_future
	UNION
	-- Changed legal_gladiator
	SELECT NEW.card_id, 'legal_gladiator', OLD.legal_gladiator::TEXT, NEW.legal_gladiator::TEXT, NEW.change_set_id
		WHERE OLD.legal_gladiator IS NOT DISTINCT FROM NEW.legal_gladiator
	UNION
	-- Changed legal_historic
	SELECT NEW.card_id, 'legal_historic', OLD.legal_historic::TEXT, NEW.legal_historic::TEXT, NEW.change_set_id
		WHERE OLD.legal_historic IS NOT DISTINCT FROM NEW.legal_historic
	UNION
	-- Changed legal_legacy
	SELECT NEW.card_id, 'legal_legacy', OLD.legal_legacy::TEXT, NEW.legal_legacy::TEXT, NEW.change_set_id
		WHERE OLD.legal_legacy IS NOT DISTINCT FROM NEW.legal_legacy
	UNION
	-- Changed legal_modern
	SELECT NEW.card_id, 'legal_modern', OLD.legal_modern::TEXT, NEW.legal_modern::TEXT, NEW.change_set_id
		WHERE OLD.legal_modern IS NOT DISTINCT FROM NEW.legal_modern
	UNION
	-- Changed legal_oathbreaker
	SELECT NEW.card_id, 'legal_oathbreaker', OLD.legal_oathbreaker::TEXT, NEW.legal_oathbreaker::TEXT, NEW.change_set_id
		WHERE OLD.legal_oathbreaker IS NOT DISTINCT FROM NEW.legal_oathbreaker
	UNION
	-- Changed legal_oldschool
	SELECT NEW.card_id, 'legal_oldschool', OLD.legal_oldschool::TEXT, NEW.legal_oldschool::TEXT, NEW.change_set_id
		WHERE OLD.legal_oldschool IS NOT DISTINCT FROM NEW.legal_oldschool
	UNION
	-- Changed legal_pauper
	SELECT NEW.card_id, 'legal_pauper', OLD.legal_pauper::TEXT, NEW.legal_pauper::TEXT, NEW.change_set_id
		WHERE OLD.legal_pauper IS NOT DISTINCT FROM NEW.legal_pauper
	UNION
	-- Changed legal_paupercommander
	SELECT NEW.card_id, 'legal_paupercommander', OLD.legal_paupercommander::TEXT, NEW.legal_paupercommander::TEXT, NEW.change_set_id
		WHERE OLD.legal_paupercommander IS NOT DISTINCT FROM NEW.legal_paupercommander
	UNION
	-- Changed legal_penny
	SELECT NEW.card_id, 'legal_penny', OLD.legal_penny::TEXT, NEW.legal_penny::TEXT, NEW.change_set_id
		WHERE OLD.legal_penny IS NOT DISTINCT FROM NEW.legal_penny
	UNION
	-- Changed legal_pioneer
	SELECT NEW.card_id, 'legal_pioneer', OLD.legal_pioneer::TEXT, NEW.legal_pioneer::TEXT, NEW.change_set_id
		WHERE OLD.legal_pioneer IS NOT DISTINCT FROM NEW.legal_pioneer
	UNION
	-- Changed legal_predh
	SELECT NEW.card_id, 'legal_predh', OLD.legal_predh::TEXT, NEW.legal_predh::TEXT, NEW.change_set_id
		WHERE OLD.legal_predh IS NOT DISTINCT FROM NEW.legal_predh
	UNION
	-- Changed legal_premodern
	SELECT NEW.card_id, 'legal_premodern', OLD.legal_premodern::TEXT, NEW.legal_premodern::TEXT, NEW.change_set_id
		WHERE OLD.legal_premodern IS NOT DISTINCT FROM NEW.legal_premodern
	UNION
	-- Changed legal_standard
	SELECT NEW.card_id, 'legal_standard', OLD.legal_standard::TEXT, NEW.legal_standard::TEXT, NEW.change_set_id
		WHERE OLD.legal_standard IS NOT DISTINCT FROM NEW.legal_standard
	UNION
	-- Changed legal_standardbrawl
	SELECT NEW.card_id, 'legal_standardbrawl', OLD.legal_standardbrawl::TEXT, NEW.legal_standardbrawl::TEXT, NEW.change_set_id
		WHERE OLD.legal_standardbrawl IS NOT DISTINCT FROM NEW.legal_standardbrawl
	UNION
	-- Changed legal_timeless
	SELECT NEW.card_id, 'legal_timeless', OLD.legal_timeless::TEXT, NEW.legal_timeless::TEXT, NEW.change_set_id
		WHERE OLD.legal_timeless IS NOT DISTINCT FROM NEW.legal_timeless
	UNION
	-- Changed legal_vintage
	SELECT NEW.card_id, 'legal_vintage', OLD.legal_vintage::TEXT, NEW.legal_vintage::TEXT, NEW.change_set_id
		WHERE OLD.legal_vintage IS NOT DISTINCT FROM NEW.legal_vintage
    UNION
	-- Changed life_modifier
	SELECT NEW.card_id, 'life_modifier', OLD.life_modifier, NEW.life_modifier, NEW.change_set_id
		WHERE OLD.life_modifier IS NOT DISTINCT FROM NEW.life_modifier
    UNION
	-- Changed loyalty
	SELECT NEW.card_id, 'loyalty', OLD.loyalty, NEW.loyalty, NEW.change_set_id
		WHERE OLD.loyalty IS NOT DISTINCT FROM NEW.loyalty
    UNION
	-- Changed mana_cost
	SELECT NEW.card_id, 'mana_cost', OLD.mana_cost, NEW.mana_cost, NEW.change_set_id
		WHERE OLD.mana_cost IS NOT DISTINCT FROM NEW.mana_cost
	UNION
	-- Changed mtgo_id
	SELECT NEW.card_id, 'mtgo_id', OLD.mtgo_id::TEXT, NEW.mtgo_id::TEXT, NEW.change_set_id
		WHERE OLD.mtgo_id IS NOT DISTINCT FROM NEW.mtgo_id
    UNION
	-- Changed multiverse_ids
	SELECT NEW.card_id, 'multiverse_ids', OLD.multiverse_ids, NEW.multiverse_ids, NEW.change_set_id
		WHERE OLD.multiverse_ids IS NOT DISTINCT FROM NEW.multiverse_ids
    UNION
	-- Changed name
	SELECT NEW.card_id, 'name', OLD.name, NEW.name, NEW.change_set_id
		WHERE OLD.name IS NOT DISTINCT FROM NEW.name
	UNION
	-- Changed nonfoil
	SELECT NEW.card_id, 'nonfoil', OLD.nonfoil::TEXT, NEW.nonfoil::TEXT, NEW.change_set_id
		WHERE OLD.nonfoil IS NOT DISTINCT FROM NEW.nonfoil
	UNION
	-- Changed oracle_id
	SELECT NEW.card_id, 'oracle_id', OLD.oracle_id::TEXT, NEW.oracle_id::TEXT, NEW.change_set_id
		WHERE OLD.oracle_id IS NOT DISTINCT FROM NEW.oracle_id
    UNION
	-- Changed oracle_text
	SELECT NEW.card_id, 'oracle_text', OLD.oracle_text, NEW.oracle_text, NEW.change_set_id
		WHERE OLD.oracle_text IS NOT DISTINCT FROM NEW.oracle_text
	UNION
	-- Changed oversized
	SELECT NEW.card_id, 'oversized', OLD.oversized::TEXT, NEW.oversized::TEXT, NEW.change_set_id
		WHERE OLD.oversized IS NOT DISTINCT FROM NEW.oversized
	UNION
	-- Changed penny_rank
	SELECT NEW.card_id, 'penny_rank', OLD.penny_rank::TEXT, NEW.penny_rank::TEXT, NEW.change_set_id
		WHERE OLD.penny_rank IS NOT DISTINCT FROM NEW.penny_rank
    UNION
	-- Changed power
	SELECT NEW.card_id, 'power', OLD.power, NEW.power, NEW.change_set_id
		WHERE OLD.power IS NOT DISTINCT FROM NEW.power
	UNION
	-- Changed preview_date
	SELECT NEW.card_id, 'preview_date', TO_CHAR(OLD.preview_date, 'YYYY-MM-DD'), TO_CHAR(NEW.preview_date, 'YYYY-MM-DD'), NEW.change_set_id
		WHERE OLD.preview_date IS NOT DISTINCT FROM NEW.preview_date
    UNION
	-- Changed preview_source
	SELECT NEW.card_id, 'preview_source', OLD.preview_source, NEW.preview_source, NEW.change_set_id
		WHERE OLD.preview_source IS NOT DISTINCT FROM NEW.preview_source
    UNION
	-- Changed preview_source_uri
	SELECT NEW.card_id, 'preview_source_uri', OLD.preview_source_uri, NEW.preview_source_uri, NEW.change_set_id
		WHERE OLD.preview_source_uri IS NOT DISTINCT FROM NEW.preview_source_uri
	UNION
	-- Changed price_eur
	SELECT NEW.card_id, 'price_eur', OLD.price_eur::TEXT, NEW.price_eur::TEXT, NEW.change_set_id
		WHERE OLD.price_eur IS NOT DISTINCT FROM NEW.price_eur
	UNION
	-- Changed price_eur_foil
	SELECT NEW.card_id, 'price_eur_foil', OLD.price_eur_foil::TEXT, NEW.price_eur_foil::TEXT, NEW.change_set_id
		WHERE OLD.price_eur_foil IS NOT DISTINCT FROM NEW.price_eur_foil
	UNION
	-- Changed price_tix
	SELECT NEW.card_id, 'price_tix', OLD.price_tix::TEXT, NEW.price_tix::TEXT, NEW.change_set_id
		WHERE OLD.price_tix IS NOT DISTINCT FROM NEW.price_tix
	UNION
	-- Changed price_usd
	SELECT NEW.card_id, 'price_usd', OLD.price_usd::TEXT, NEW.price_usd::TEXT, NEW.change_set_id
		WHERE OLD.price_usd IS NOT DISTINCT FROM NEW.price_usd
	UNION
	-- Changed price_usd_etched
	SELECT NEW.card_id, 'price_usd_etched', OLD.price_usd_etched::TEXT, NEW.price_usd_etched::TEXT, NEW.change_set_id
		WHERE OLD.price_usd_etched IS NOT DISTINCT FROM NEW.price_usd_etched
	UNION
	-- Changed price_usd_foil
	SELECT NEW.card_id, 'price_usd_foil', OLD.price_usd_foil::TEXT, NEW.price_usd_foil::TEXT, NEW.change_set_id
		WHERE OLD.price_usd_foil IS NOT DISTINCT FROM NEW.price_usd_foil
    UNION
	-- Changed printed_name
	SELECT NEW.card_id, 'printed_name', OLD.printed_name, NEW.printed_name, NEW.change_set_id
		WHERE OLD.printed_name IS NOT DISTINCT FROM NEW.printed_name
    UNION
	-- Changed printed_text
	SELECT NEW.card_id, 'printed_text', OLD.printed_text, NEW.printed_text, NEW.change_set_id
		WHERE OLD.printed_text IS NOT DISTINCT FROM NEW.printed_text
    UNION
	-- Changed printed_type_line
	SELECT NEW.card_id, 'printed_type_line', OLD.printed_type_line, NEW.printed_type_line, NEW.change_set_id
		WHERE OLD.printed_type_line IS NOT DISTINCT FROM NEW.printed_type_line
    UNION
	-- Changed prints_search_uri
	SELECT NEW.card_id, 'printed_search_uri', OLD.printed_search_uri, NEW.printed_search_uri, NEW.change_set_id
		WHERE OLD.printed_search_uri IS NOT DISTINCT FROM NEW.printed_search_uri
    UNION
	-- Changed produced_mana
	SELECT NEW.card_id, 'produced_mana', OLD.produced_mana, NEW.produced_mana, NEW.change_set_id
		WHERE OLD.produced_mana IS NOT DISTINCT FROM NEW.produced_mana
	UNION
	-- Changed promo
	SELECT NEW.card_id, 'promo', OLD.promo::TEXT, NEW.promo::TEXT, NEW.change_set_id
		WHERE OLD.promo IS NOT DISTINCT FROM NEW.promo
    UNION
	-- Changed purchase_cardhoarder
	SELECT NEW.card_id, 'purchase_cardhoarder', OLD.purchase_cardhoarder, NEW.purchase_cardhoarder, NEW.change_set_id
		WHERE OLD.purchase_cardhoarder IS NOT DISTINCT FROM NEW.purchase_cardhoarder
    UNION
	-- Changed purchase_cardmarket
	SELECT NEW.card_id, 'purchase_cardmarket', OLD.purchase_cardmarket, NEW.purchase_cardmarket, NEW.change_set_id
		WHERE OLD.purchase_cardmarket IS NOT DISTINCT FROM NEW.purchase_cardmarket
    UNION
	-- Changed purchase_tcgplayer
	SELECT NEW.card_id, 'purchase_tcgplayer', OLD.purchase_tcgplayer, NEW.purchase_tcgplayer, NEW.change_set_id
		WHERE OLD.purchase_tcgplayer IS NOT DISTINCT FROM NEW.purchase_tcgplayer
    UNION
	-- Changed rarity
	SELECT NEW.card_id, 'rarity', OLD.rarity, NEW.rarity, NEW.change_set_id
		WHERE OLD.rarity IS NOT DISTINCT FROM NEW.rarity
	UNION
	-- Changed released_at
	SELECT NEW.card_id, 'released_at', TO_CHAR(OLD.released_at, 'YYYY-MM-DD'), TO_CHAR(NEW.released_at, 'YYYY-MM-DD'), NEW.change_set_id
		WHERE OLD.released_at IS NOT DISTINCT FROM NEW.released_at
	UNION
	-- Changed reprint
	SELECT NEW.card_id, 'reprint', OLD.reprint::TEXT, NEW.reprint::TEXT, NEW.change_set_id
		WHERE OLD.reprint IS NOT DISTINCT FROM NEW.reprint
	UNION
	-- Changed reserved
	SELECT NEW.card_id, 'reserved', OLD.reserved::TEXT, NEW.reserved::TEXT, NEW.change_set_id
		WHERE OLD.reserved IS NOT DISTINCT FROM NEW.reserved
    UNION
	-- Changed rulings_uri
	SELECT NEW.card_id, 'rulings_uri', OLD.rulings_uri, NEW.rulings_uri, NEW.change_set_id
		WHERE OLD.rulings_uri IS NOT DISTINCT FROM NEW.rulings_uri
    UNION
	-- Changed scryfall_set_uri
	SELECT NEW.card_id, 'scryfall_set_uri', OLD.scryfall_set_uri, NEW.scryfall_set_uri, NEW.change_set_id
		WHERE OLD.scryfall_set_uri IS NOT DISTINCT FROM NEW.scryfall_set_uri
    UNION
	-- Changed scryfall_uri
	SELECT NEW.card_id, 'scryfall_uri', OLD.scryfall_uri, NEW.scryfall_uri, NEW.change_set_id
		WHERE OLD.scryfall_uri IS NOT DISTINCT FROM NEW.scryfall_uri
    UNION
	-- Changed security_stamp
	SELECT NEW.card_id, 'security_stamp', OLD.security_stamp, NEW.security_stamp, NEW.change_set_id
		WHERE OLD.security_stamp IS NOT DISTINCT FROM NEW.security_stamp
    UNION
	-- Changed set
	SELECT NEW.card_id, 'set', OLD.set, NEW.set, NEW.change_set_id
		WHERE OLD.set IS NOT DISTINCT FROM NEW.set
	UNION
	-- Changed set_id
	SELECT NEW.card_id, 'set_id', OLD.set_id::TEXT, NEW.set_id::TEXT, NEW.change_set_id
		WHERE OLD.set_id IS NOT DISTINCT FROM NEW.set_id
    UNION
	-- Changed set_name
	SELECT NEW.card_id, 'set_name', OLD.set_name, NEW.set_name, NEW.change_set_id
		WHERE OLD.set_name IS NOT DISTINCT FROM NEW.set_name
    UNION
	-- Changed set_search_uri
	SELECT NEW.card_id, 'set_search_uri', OLD.set_search_uri, NEW.set_search_uri, NEW.change_set_id
		WHERE OLD.set_search_uri IS NOT DISTINCT FROM NEW.set_search_uri
    UNION
	-- Changed set_type
	SELECT NEW.card_id, 'set_type', OLD.set_type, NEW.set_type, NEW.change_set_id
		WHERE OLD.set_type IS NOT DISTINCT FROM NEW.set_type
    UNION
	-- Changed set_uri
	SELECT NEW.card_id, 'set_uri', OLD.set_uri, NEW.set_uri, NEW.change_set_id
		WHERE OLD.set_uri IS NOT DISTINCT FROM NEW.set_uri
	UNION
	-- Changed story_spotlight
	SELECT NEW.card_id, 'story_spotlight', OLD.story_spotlight::TEXT, NEW.story_spotlight::TEXT, NEW.change_set_id
		WHERE OLD.story_spotlight IS NOT DISTINCT FROM NEW.story_spotlight
	UNION
	-- Changed tcgplayer_id
	SELECT NEW.card_id, 'tcgplayer_id', OLD.tcgplayer_id::TEXT, NEW.tcgplayer_id::TEXT, NEW.change_set_id
		WHERE OLD.tcgplayer_id IS NOT DISTINCT FROM NEW.tcgplayer_id
	UNION
	-- Changed textless
	SELECT NEW.card_id, 'textless', OLD.textless::TEXT, NEW.textless::TEXT, NEW.change_set_id
		WHERE OLD.textless IS NOT DISTINCT FROM NEW.textless
    UNION
	-- Changed toughness
	SELECT NEW.card_id, 'toughness', OLD.toughness, NEW.toughness, NEW.change_set_id
		WHERE OLD.toughness IS NOT DISTINCT FROM NEW.toughness
    UNION
	-- Changed type_line
	SELECT NEW.card_id, 'type_line', OLD.type_line, NEW.type_line, NEW.change_set_id
		WHERE OLD.type_line IS NOT DISTINCT FROM NEW.type_line
    UNION
	-- Changed uri
	SELECT NEW.card_id, 'uri', OLD.uri, NEW.uri, NEW.change_set_id
		WHERE OLD.uri IS NOT DISTINCT FROM NEW.uri
    UNION
	-- Changed uri_edhrec
	SELECT NEW.card_id, 'uri_edhrec', OLD.uri_edhrec, NEW.uri_edhrec, NEW.change_set_id
		WHERE OLD.uri_edhrec IS NOT DISTINCT FROM NEW.uri_edhrec
    UNION
	-- Changed uri_gatherer
	SELECT NEW.card_id, 'uri_gatherer', OLD.uri_gatherer, NEW.uri_gatherer, NEW.change_set_id
		WHERE OLD.uri_gatherer IS NOT DISTINCT FROM NEW.uri_gatherer
    UNION
	-- Changed uri_tcgplayer_infinite_articles
	SELECT NEW.card_id, 'uri_tcgplayer_infinite_articles', OLD.uri_tcgplayer_infinite_articles, NEW.uri_tcgplayer_infinite_articles, NEW.change_set_id
		WHERE OLD.uri_tcgplayer_infinite_articles IS NOT DISTINCT FROM NEW.uri_tcgplayer_infinite_articles
    UNION
	-- Changed uri_tcgplayer_infinite_decks
	SELECT NEW.card_id, 'uri_tcgplayer_infinite_decks', OLD.uri_tcgplayer_infinite_decks, NEW.uri_tcgplayer_infinite_decks, NEW.change_set_id
		WHERE OLD.uri_tcgplayer_infinite_decks IS NOT DISTINCT FROM NEW.uri_tcgplayer_infinite_decks
	UNION
	-- Changed variation
	SELECT NEW.card_id, 'variation', OLD.variation::TEXT, NEW.variation::TEXT, NEW.change_set_id
		WHERE OLD.variation IS NOT DISTINCT FROM NEW.variation
    UNION
	-- Changed active
	SELECT NEW.card_id, 'active', OLD.active::TEXT, NEW.active::TEXT, NEW.change_set_id
		WHERE OLD.active IS NOT DISTINCT FROM NEW.active
    ;
    
    RETURN NEW;
END;
$$ LANGUAGE plpgsql
;

CREATE TRIGGER TRI_before_insert_TCG_MTG_Card
    BEFORE INSERT ON tcg.public.TCG_MTG_Card
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_insert_TCG_MTG_Card()
;
CREATE TRIGGER TRI_before_update_TCG_MTG_Card
    BEFORE UPDATE ON tcg.public.TCG_MTG_Card
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_update_TCG_MTG_Card()
;