aboutsummaryrefslogtreecommitdiff
path: root/migrations
diff options
context:
space:
mode:
authorNathan Perry <np@nathanperry.dev>2024-08-16 21:14:30 -0400
committerNathan Perry <np@nathanperry.dev>2024-08-16 21:15:07 -0400
commitef056edf92b678265a4666e1f9405e3b0ce66a42 (patch)
treeb3766d47ae2898d2a46f108de4e2be0ede6c400b /migrations
parentc5ce454319a7d54d3967c6ea7695543e943a37b2 (diff)
repo: overhaul for multitenancy
Diffstat (limited to 'migrations')
-rw-r--r--migrations/2024-08-16-111659_multitenant/down.sql12
-rw-r--r--migrations/2024-08-16-111659_multitenant/up.sql35
-rw-r--r--migrations/2024-08-16-230507_require_guild/down.sql6
-rw-r--r--migrations/2024-08-16-230507_require_guild/up.sql22
4 files changed, 75 insertions, 0 deletions
diff --git a/migrations/2024-08-16-111659_multitenant/down.sql b/migrations/2024-08-16-111659_multitenant/down.sql
new file mode 100644
index 0000000..f0cac09
--- /dev/null
+++ b/migrations/2024-08-16-111659_multitenant/down.sql
@@ -0,0 +1,12 @@
+ALTER TABLE memes
+ 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 memes_guild_title_key,
+ memes_guild_content_image_id_audio_id_key,
+ memes_guild_metadata_id_key;
+
+CREATE UNIQUE INDEX IF NOT EXISTS text_memes_title_key ON memes (title);
+CREATE UNIQUE INDEX IF NOT EXISTS text_memes_content_image_id_audio_id_key ON memes (content, image_id, audio_id);
+CREATE UNIQUE INDEX IF NOT EXISTS text_memes_metadata_id_key ON memes (metadata_id);
diff --git a/migrations/2024-08-16-111659_multitenant/up.sql b/migrations/2024-08-16-111659_multitenant/up.sql
new file mode 100644
index 0000000..8bda619
--- /dev/null
+++ b/migrations/2024-08-16-111659_multitenant/up.sql
@@ -0,0 +1,35 @@
+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;
diff --git a/migrations/2024-08-16-230507_require_guild/down.sql b/migrations/2024-08-16-230507_require_guild/down.sql
new file mode 100644
index 0000000..e7191c8
--- /dev/null
+++ b/migrations/2024-08-16-230507_require_guild/down.sql
@@ -0,0 +1,6 @@
+ALTER TABLE memes
+ ALTER COLUMN guild DROP NOT NULL;
+ALTER TABLE invocation_records
+ ALTER COLUMN guild DROP NOT NULL;
+ALTER TABLE tombstones
+ ALTER COLUMN guild DROP NOT NULL;
diff --git a/migrations/2024-08-16-230507_require_guild/up.sql b/migrations/2024-08-16-230507_require_guild/up.sql
new file mode 100644
index 0000000..06e5cd3
--- /dev/null
+++ b/migrations/2024-08-16-230507_require_guild/up.sql
@@ -0,0 +1,22 @@
+-- This migration will fail with any records in the db. If you are working with a normal server, you
+-- can run:
+--
+-- $ convert_null_guilds --guild $PREVIOUS_SINGLETENANT_GUILD
+--
+-- to set all existing db records to the specified guild. A mixed / fucked up db sceneario will
+-- require you to manually edit the db and ensure `guild` is set in `memes`, `invocation_records`,
+-- and `tombstones`.
+
+DO LANGUAGE plpgsql
+$$
+ BEGIN
+ RAISE NOTICE 'if this migration fails, consider running "convert_null_guilds"';
+ END
+$$;
+
+ALTER TABLE memes
+ ALTER COLUMN guild SET NOT NULL;
+ALTER TABLE invocation_records
+ ALTER COLUMN guild SET NOT NULL;
+ALTER TABLE tombstones
+ ALTER COLUMN guild SET NOT NULL;