Skip to content

Commit

Permalink
fix: mysql migration for multitenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed May 23, 2024
1 parent a3b9428 commit c3b9abe
Showing 1 changed file with 132 additions and 27 deletions.
159 changes: 132 additions & 27 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,33 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
SELECT concat('ALTER TABLE ', table_schema,'.',table_name,' DROP FOREIGN KEY ', constraint_name, ';')
FROM information_schema.table_constraints
WHERE constraint_type='FOREIGN KEY'
AND table_schema = DATABASE();
AND table_schema = DATABASE()
AND table_name in (
'all_auth_recipe_users',
'dashboard_user_sessions',
'dashboard_users',
'emailpassword_pswd_reset_tokens',
'emailpassword_users',
'emailverification_tokens',
'emailverification_verified_emails',
'jwt_signing_keys',
'key_value',
'passwordless_codes',
'passwordless_devices',
'passwordless_users',
'role_permissions',
'roles',
'session_access_token_signing_keys',
'session_info',
'thirdparty_users',
'totp_used_codes',
'totp_user_devices',
'totp_users',
'user_last_active',
'user_metadata',
'user_roles',
'userid_mapping'
);

DECLARE CONTINUE handler for NOT found SET done = true;
OPEN dropCur;
Expand All @@ -240,7 +266,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
CLOSE dropCur;
END

---
--

CREATE PROCEDURE st_drop_all_pkeys()
BEGIN
Expand All @@ -250,7 +276,33 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
SELECT concat('ALTER TABLE ', table_schema,'.',table_name,' DROP PRIMARY KEY ', ';')
FROM information_schema.table_constraints
WHERE constraint_type='PRIMARY KEY'
AND table_schema = DATABASE();
AND table_schema = DATABASE()
AND table_name in (
'all_auth_recipe_users',
'dashboard_user_sessions',
'dashboard_users',
'emailpassword_pswd_reset_tokens',
'emailpassword_users',
'emailverification_tokens',
'emailverification_verified_emails',
'jwt_signing_keys',
'key_value',
'passwordless_codes',
'passwordless_devices',
'passwordless_users',
'role_permissions',
'roles',
'session_access_token_signing_keys',
'session_info',
'thirdparty_users',
'totp_used_codes',
'totp_user_devices',
'totp_users',
'user_last_active',
'user_metadata',
'user_roles',
'userid_mapping'
);

DECLARE CONTINUE handler for NOT found SET done = true;
OPEN dropCur;
Expand All @@ -273,7 +325,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
CLOSE dropCur;
END

---
--

CREATE PROCEDURE st_drop_all_keys()
BEGIN
Expand All @@ -283,7 +335,33 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
SELECT concat('ALTER TABLE ', table_schema,'.',table_name,' DROP INDEX ', constraint_name, ';')
FROM information_schema.table_constraints
WHERE constraint_type='UNIQUE'
AND table_schema = DATABASE();
AND table_schema = DATABASE()
AND table_name in (
'all_auth_recipe_users',
'dashboard_user_sessions',
'dashboard_users',
'emailpassword_pswd_reset_tokens',
'emailpassword_users',
'emailverification_tokens',
'emailverification_verified_emails',
'jwt_signing_keys',
'key_value',
'passwordless_codes',
'passwordless_devices',
'passwordless_users',
'role_permissions',
'roles',
'session_access_token_signing_keys',
'session_info',
'thirdparty_users',
'totp_used_codes',
'totp_user_devices',
'totp_users',
'user_last_active',
'user_metadata',
'user_roles',
'userid_mapping'
);

DECLARE CONTINUE handler for NOT found SET done = true;
OPEN dropCur;
Expand All @@ -306,7 +384,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
CLOSE dropCur;
END

---
--

CREATE PROCEDURE st_drop_all_indexes()
BEGIN
Expand All @@ -315,7 +393,34 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
DECLARE dropCur CURSOR for
SELECT DISTINCT concat('ALTER TABLE ', table_schema, '.', table_name, ' DROP INDEX ', index_name, ';')
FROM information_schema.statistics
WHERE NON_UNIQUE = 1 AND table_schema = database();
WHERE NON_UNIQUE = 1
AND table_schema = database()
AND table_name in (
'all_auth_recipe_users',
'dashboard_user_sessions',
'dashboard_users',
'emailpassword_pswd_reset_tokens',
'emailpassword_users',
'emailverification_tokens',
'emailverification_verified_emails',
'jwt_signing_keys',
'key_value',
'passwordless_codes',
'passwordless_devices',
'passwordless_users',
'role_permissions',
'roles',
'session_access_token_signing_keys',
'session_info',
'thirdparty_users',
'totp_used_codes',
'totp_user_devices',
'totp_users',
'user_last_active',
'user_metadata',
'user_roles',
'userid_mapping'
);

DECLARE CONTINUE handler for NOT found SET done = true;
OPEN dropCur;
Expand All @@ -338,7 +443,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
CLOSE dropCur;
END

---
--

CREATE PROCEDURE st_add_column_if_not_exists(
IN p_table_name varchar(50),
Expand Down Expand Up @@ -390,7 +495,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
INSERT IGNORE INTO apps (app_id, created_at_time)
VALUES ('public', 0);

------------------------------------------------------------
--

CREATE TABLE IF NOT EXISTS tenants (
app_id VARCHAR(64) NOT NULL DEFAULT 'public',
Expand All @@ -408,7 +513,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
INSERT IGNORE INTO tenants (app_id, tenant_id, created_at_time)
VALUES ('public', 'public', 0);

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('key_value', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
CALL st_add_column_if_not_exists('key_value', 'tenant_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
Expand All @@ -420,7 +525,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id, tenant_id)
REFERENCES tenants (app_id, tenant_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CREATE TABLE IF NOT EXISTS app_id_to_user_id (
app_id VARCHAR(64) NOT NULL DEFAULT 'public',
Expand All @@ -439,7 +544,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
SELECT user_id, recipe_id
FROM all_auth_recipe_users;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('all_auth_recipe_users', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
CALL st_add_column_if_not_exists('all_auth_recipe_users', 'tenant_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
Expand Down Expand Up @@ -472,7 +577,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ALTER TABLE tenant_configs
ADD PRIMARY KEY (connection_uri_domain, app_id, tenant_id);

------------------------------------------------------------
--

CREATE TABLE IF NOT EXISTS tenant_thirdparty_providers (
connection_uri_domain VARCHAR(256) DEFAULT '',
Expand Down Expand Up @@ -505,7 +610,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (connection_uri_domain, app_id, tenant_id)
REFERENCES tenant_configs (connection_uri_domain, app_id, tenant_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CREATE TABLE IF NOT EXISTS tenant_thirdparty_provider_clients (
connection_uri_domain VARCHAR(256) DEFAULT '',
Expand Down Expand Up @@ -542,7 +647,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);

CREATE INDEX session_expiry_index ON session_info (expires_at);

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('session_access_token_signing_keys', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);

Expand Down Expand Up @@ -575,7 +680,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id)
REFERENCES apps (app_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('emailverification_tokens', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
CALL st_add_column_if_not_exists('emailverification_tokens', 'tenant_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
Expand Down Expand Up @@ -603,7 +708,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id, user_id)
REFERENCES app_id_to_user_id (app_id, user_id) ON DELETE CASCADE;

-- ------------------------------------------------------------
-- --

CREATE TABLE IF NOT EXISTS emailpassword_user_to_tenant (
app_id VARCHAR(64) DEFAULT 'public',
Expand All @@ -625,7 +730,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
INSERT IGNORE INTO emailpassword_user_to_tenant (user_id, email)
SELECT user_id, email FROM emailpassword_users;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('emailpassword_pswd_reset_tokens', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);

Expand All @@ -652,7 +757,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id, user_id)
REFERENCES app_id_to_user_id (app_id, user_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CREATE TABLE IF NOT EXISTS passwordless_user_to_tenant (
app_id VARCHAR(64) DEFAULT 'public',
Expand All @@ -678,7 +783,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
INSERT IGNORE INTO passwordless_user_to_tenant (user_id, email, phone_number)
SELECT user_id, email, phone_number FROM passwordless_users;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('passwordless_devices', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
CALL st_add_column_if_not_exists('passwordless_devices', 'tenant_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
Expand All @@ -694,7 +799,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);

CREATE INDEX passwordless_devices_phone_number_index ON passwordless_devices (app_id, tenant_id, phone_number);

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('passwordless_codes', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
CALL st_add_column_if_not_exists('passwordless_codes', 'tenant_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
Expand Down Expand Up @@ -727,7 +832,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);

CREATE INDEX thirdparty_users_email_index ON thirdparty_users (app_id, email);

------------------------------------------------------------
--

CREATE TABLE IF NOT EXISTS thirdparty_user_to_tenant (
app_id VARCHAR(64) DEFAULT 'public',
Expand Down Expand Up @@ -781,7 +886,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id)
REFERENCES apps (app_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('role_permissions', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);

Expand All @@ -794,7 +899,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);

CREATE INDEX role_permissions_permission_index ON role_permissions (app_id, permission);

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('user_roles', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
CALL st_add_column_if_not_exists('user_roles', 'tenant_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
Expand Down Expand Up @@ -838,7 +943,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id)
REFERENCES apps (app_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('dashboard_user_sessions', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);

Expand All @@ -862,7 +967,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id)
REFERENCES apps (app_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('totp_user_devices', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);

Expand All @@ -873,7 +978,7 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
ADD FOREIGN KEY (app_id, user_id)
REFERENCES totp_users (app_id, user_id) ON DELETE CASCADE;

------------------------------------------------------------
--

CALL st_add_column_if_not_exists('totp_used_codes', 'app_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
CALL st_add_column_if_not_exists('totp_used_codes', 'tenant_id', 'VARCHAR(64)', 'NOT NULL DEFAULT \'public\'', @status_message);
Expand Down

0 comments on commit c3b9abe

Please sign in to comment.