Skip to content

Commit de70b57

Browse files
authored
fix: mysql migration for multitenancy (#120)
1 parent a3b9428 commit de70b57

File tree

1 file changed

+132
-27
lines changed

1 file changed

+132
-27
lines changed

CHANGELOG.md

+132-27
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,33 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
217217
SELECT concat('ALTER TABLE ', table_schema,'.',table_name,' DROP FOREIGN KEY ', constraint_name, ';')
218218
FROM information_schema.table_constraints
219219
WHERE constraint_type='FOREIGN KEY'
220-
AND table_schema = DATABASE();
220+
AND table_schema = DATABASE()
221+
AND table_name in (
222+
'all_auth_recipe_users',
223+
'dashboard_user_sessions',
224+
'dashboard_users',
225+
'emailpassword_pswd_reset_tokens',
226+
'emailpassword_users',
227+
'emailverification_tokens',
228+
'emailverification_verified_emails',
229+
'jwt_signing_keys',
230+
'key_value',
231+
'passwordless_codes',
232+
'passwordless_devices',
233+
'passwordless_users',
234+
'role_permissions',
235+
'roles',
236+
'session_access_token_signing_keys',
237+
'session_info',
238+
'thirdparty_users',
239+
'totp_used_codes',
240+
'totp_user_devices',
241+
'totp_users',
242+
'user_last_active',
243+
'user_metadata',
244+
'user_roles',
245+
'userid_mapping'
246+
);
221247

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

243-
---
269+
--
244270

245271
CREATE PROCEDURE st_drop_all_pkeys()
246272
BEGIN
@@ -250,7 +276,33 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
250276
SELECT concat('ALTER TABLE ', table_schema,'.',table_name,' DROP PRIMARY KEY ', ';')
251277
FROM information_schema.table_constraints
252278
WHERE constraint_type='PRIMARY KEY'
253-
AND table_schema = DATABASE();
279+
AND table_schema = DATABASE()
280+
AND table_name in (
281+
'all_auth_recipe_users',
282+
'dashboard_user_sessions',
283+
'dashboard_users',
284+
'emailpassword_pswd_reset_tokens',
285+
'emailpassword_users',
286+
'emailverification_tokens',
287+
'emailverification_verified_emails',
288+
'jwt_signing_keys',
289+
'key_value',
290+
'passwordless_codes',
291+
'passwordless_devices',
292+
'passwordless_users',
293+
'role_permissions',
294+
'roles',
295+
'session_access_token_signing_keys',
296+
'session_info',
297+
'thirdparty_users',
298+
'totp_used_codes',
299+
'totp_user_devices',
300+
'totp_users',
301+
'user_last_active',
302+
'user_metadata',
303+
'user_roles',
304+
'userid_mapping'
305+
);
254306

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

276-
---
328+
--
277329

278330
CREATE PROCEDURE st_drop_all_keys()
279331
BEGIN
@@ -283,7 +335,33 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
283335
SELECT concat('ALTER TABLE ', table_schema,'.',table_name,' DROP INDEX ', constraint_name, ';')
284336
FROM information_schema.table_constraints
285337
WHERE constraint_type='UNIQUE'
286-
AND table_schema = DATABASE();
338+
AND table_schema = DATABASE()
339+
AND table_name in (
340+
'all_auth_recipe_users',
341+
'dashboard_user_sessions',
342+
'dashboard_users',
343+
'emailpassword_pswd_reset_tokens',
344+
'emailpassword_users',
345+
'emailverification_tokens',
346+
'emailverification_verified_emails',
347+
'jwt_signing_keys',
348+
'key_value',
349+
'passwordless_codes',
350+
'passwordless_devices',
351+
'passwordless_users',
352+
'role_permissions',
353+
'roles',
354+
'session_access_token_signing_keys',
355+
'session_info',
356+
'thirdparty_users',
357+
'totp_used_codes',
358+
'totp_user_devices',
359+
'totp_users',
360+
'user_last_active',
361+
'user_metadata',
362+
'user_roles',
363+
'userid_mapping'
364+
);
287365

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

309-
---
387+
--
310388

311389
CREATE PROCEDURE st_drop_all_indexes()
312390
BEGIN
@@ -315,7 +393,34 @@ CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
315393
DECLARE dropCur CURSOR for
316394
SELECT DISTINCT concat('ALTER TABLE ', table_schema, '.', table_name, ' DROP INDEX ', index_name, ';')
317395
FROM information_schema.statistics
318-
WHERE NON_UNIQUE = 1 AND table_schema = database();
396+
WHERE NON_UNIQUE = 1
397+
AND table_schema = database()
398+
AND table_name in (
399+
'all_auth_recipe_users',
400+
'dashboard_user_sessions',
401+
'dashboard_users',
402+
'emailpassword_pswd_reset_tokens',
403+
'emailpassword_users',
404+
'emailverification_tokens',
405+
'emailverification_verified_emails',
406+
'jwt_signing_keys',
407+
'key_value',
408+
'passwordless_codes',
409+
'passwordless_devices',
410+
'passwordless_users',
411+
'role_permissions',
412+
'roles',
413+
'session_access_token_signing_keys',
414+
'session_info',
415+
'thirdparty_users',
416+
'totp_used_codes',
417+
'totp_user_devices',
418+
'totp_users',
419+
'user_last_active',
420+
'user_metadata',
421+
'user_roles',
422+
'userid_mapping'
423+
);
319424

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

341-
---
446+
--
342447

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

393-
------------------------------------------------------------
498+
--
394499

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

411-
------------------------------------------------------------
516+
--
412517

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

423-
------------------------------------------------------------
528+
--
424529

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

442-
------------------------------------------------------------
547+
--
443548

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

475-
------------------------------------------------------------
580+
--
476581

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

508-
------------------------------------------------------------
613+
--
509614

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

543648
CREATE INDEX session_expiry_index ON session_info (expires_at);
544649

545-
------------------------------------------------------------
650+
--
546651

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

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

578-
------------------------------------------------------------
683+
--
579684

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

606-
-- ------------------------------------------------------------
711+
-- --
607712

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

628-
------------------------------------------------------------
733+
--
629734

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

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

655-
------------------------------------------------------------
760+
--
656761

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

681-
------------------------------------------------------------
786+
--
682787

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

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

697-
------------------------------------------------------------
802+
--
698803

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

728833
CREATE INDEX thirdparty_users_email_index ON thirdparty_users (app_id, email);
729834

730-
------------------------------------------------------------
835+
--
731836

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

784-
------------------------------------------------------------
889+
--
785890

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

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

795900
CREATE INDEX role_permissions_permission_index ON role_permissions (app_id, permission);
796901

797-
------------------------------------------------------------
902+
--
798903

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

841-
------------------------------------------------------------
946+
--
842947

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

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

865-
------------------------------------------------------------
970+
--
866971

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

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

876-
------------------------------------------------------------
981+
--
877982

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

0 commit comments

Comments
 (0)