ALTER TABLE memes
    DROP CONSTRAINT IF EXISTS text_memes_title_key,
    DROP CONSTRAINT IF EXISTS text_memes_content_image_id_audio_id_key,
    DROP CONSTRAINT IF EXISTS text_memes_metadata_id_key,
    DROP CONSTRAINT IF EXISTS memes_guild_title_key,
    DROP CONSTRAINT IF EXISTS memes_guild_content_image_id_audio_id_key,
    DROP CONSTRAINT IF EXISTS memes_guild_metadata_id_key
;

DROP INDEX IF EXISTS text_memes_title_key, text_memes_content_image_id_audio_id_key, text_memes_metadata_id_key;

ALTER TABLE memes
    ADD COLUMN IF NOT EXISTS guild BIGINT;

ALTER TABLE MEMES
    ALTER COLUMN guild TYPE BIGINT,
    ADD CONSTRAINT memes_guild_title_key UNIQUE (guild, title),
    ADD CONSTRAINT memes_guild_content_image_id_audio_id_key UNIQUE (guild, content, image_id, audio_id),
    ADD CONSTRAINT memes_guild_metadata_id_key UNIQUE (guild, metadata_id);

CREATE UNIQUE INDEX IF NOT EXISTS memes_guild_title_key ON memes (guild, title);
CREATE UNIQUE INDEX IF NOT EXISTS memes_guild_content_image_id_audio_id_key ON memes (guild, content, image_id, audio_id);
CREATE UNIQUE INDEX IF NOT EXISTS memes_guild_metadata_id_key ON memes (guild, metadata_id);

ALTER TABLE tombstones
    ADD COLUMN IF NOT EXISTS guild BIGINT;

ALTER TABLE tombstones
    ALTER COLUMN guild TYPE BIGINT;

ALTER TABLE invocation_records
    ADD COLUMN IF NOT EXISTS guild BIGINT;

ALTER TABLE invocation_records
    ALTER COLUMN guild TYPE BIGINT;
