SELECT CONCAT("ALTER TABLE UM_ROLE_PERMISSION DROP FOREIGN KEY ", constraint_name)
INTO @sqlst
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = @databasename
  AND TABLE_NAME = "UM_ROLE_PERMISSION"
  AND REFERENCED_TABLE_NAME = "UM_PERMISSION"
LIMIT 1;

PREPARE stmt FROM @sqlst;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sqlstr = NULL;

ALTER TABLE UM_ROLE_PERMISSION
    ADD FOREIGN KEY (UM_PERMISSION_ID, UM_TENANT_ID) REFERENCES UM_PERMISSION (UM_ID, UM_TENANT_ID) ON DELETE CASCADE;

SELECT CONCAT("ALTER TABLE UM_USER_PERMISSION DROP FOREIGN KEY ", constraint_name)
INTO @sqlst
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = @databasename
  AND TABLE_NAME = "UM_USER_PERMISSION"
  AND REFERENCED_TABLE_NAME = "UM_PERMISSION"
LIMIT 1;

PREPARE stmt FROM @sqlst;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sqlstr = NULL;

ALTER TABLE UM_USER_PERMISSION
    ADD FOREIGN KEY (UM_PERMISSION_ID, UM_TENANT_ID) REFERENCES UM_PERMISSION (UM_ID, UM_TENANT_ID) ON DELETE CASCADE;

SELECT CONCAT("ALTER TABLE UM_HYBRID_USER_ROLE DROP FOREIGN KEY ", constraint_name)
INTO @sqlst
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = @databasename
  AND TABLE_NAME = "UM_HYBRID_USER_ROLE"
  AND REFERENCED_TABLE_NAME = "UM_HYBRID_ROLE"
LIMIT 1;

PREPARE stmt FROM @sqlst;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sqlstr = NULL;

ALTER TABLE UM_HYBRID_USER_ROLE
    ADD FOREIGN KEY (UM_ROLE_ID, UM_TENANT_ID) REFERENCES UM_HYBRID_ROLE (UM_ID, UM_TENANT_ID) ON DELETE CASCADE;

update UM_PERMISSION
set UM_RESOURCE_ID = REPLACE(UM_RESOURCE_ID, '-at-', '-AT-')
where UM_TENANT_ID <> -1234;