Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.persistence.jpa.upgrade;

public abstract class AbstractUpgradeStatements implements UpgradeStatements {

protected String commonStatements() {
return """
INSERT INTO GroupTypeExtension SELECT * FROM TypeExtension;
INSERT INTO GroupTypeExtension_Class SELECT * FROM TypeExtension_AnyTypeClass;

DROP TABLE TypeExtension_AnyTypeClass;
DROP TABLE TypeExtension;
DROP TABLE SyncopeRole_DynRealm;
DROP TABLE DynRealmMembership;
DROP TABLE DynRealm;
DROP TABLE UDynGroupMembership;
DROP TABLE ADynGroupMembership;
DROP TABLE UDynGroupMembers;
DROP TABLE ADynGroupMembers;
DROP TABLE DynRoleMembers;
DROP TABLE DynRealmMembers;
""";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public class GenerateUpgradeSQL {
UPDATE SyncopeGroup SET gManager_id=groupOwner_id;
ALTER TABLE SyncopeGroup DROP COLUMN groupOwner_id;

INSERT INTO OIDCOpEntity SELECT id,json AS jwks,'{}' AS customScopes FROM OIDCJWKS;
DROP TABLE OIDCJWKS;

DROP TABLE SyncopeRole_DynRealm;
DROP TABLE DynRealmMembership;
DROP TABLE DynRealm;
Expand All @@ -67,7 +64,11 @@ public void run(final Writer out) throws IOException, SQLException {
// run OpenJPA's SchemaTool to get the update statements
schemaTool.run();

out.append('\n').append(INIT_SQL_STATEMENTS).append('\n');
UpgradeStatements statements = UpgradeStatementsFactory.forDatabase(jdbcConf.getDBDictionaryInstance());

out.append('\n');
out.append(statements.getStatements());
out.append('\n');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.persistence.jpa.upgrade;

public class MariaDBUpgradeStatements extends AbstractUpgradeStatements {

@Override
public String getStatements() {

String mariaDBUpgradeStatements = """
UPDATE SyncopeGroup SET uManager_id=userOwner_id;

SET @fk_name = (
SELECT CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'SyncopeGroup'
AND COLUMN_NAME = 'userOwner_id'
AND REFERENCED_TABLE_NAME IS NOT NULL
LIMIT 1
);

SET @sql = CONCAT('ALTER TABLE SyncopeGroup DROP FOREIGN KEY `', @fk_name, '`');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
ALTER TABLE SyncopeGroup DROP COLUMN userOwner_id;

UPDATE SyncopeGroup SET gManager_id=groupOwner_id;
SET @fk_name = (
SELECT CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'SyncopeGroup'
AND COLUMN_NAME = 'groupOwner_id'
AND REFERENCED_TABLE_NAME IS NOT NULL
LIMIT 1
);

SET @sql = CONCAT('ALTER TABLE SyncopeGroup DROP FOREIGN KEY `', @fk_name, '`');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
ALTER TABLE SyncopeGroup DROP COLUMN groupOwner_id;

DELETE FROM AccessToken;
ALTER TABLE AccessToken MODIFY COLUMN authorities TEXT;

INSERT INTO OIDCOpEntity (id, jwks, customScopes)
SELECT id, json ,'{}'
FROM OIDCJWKS;

DROP TABLE OIDCJWKS;
""";

return commonStatements() + mariaDBUpgradeStatements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.persistence.jpa.upgrade;

public class MySQLUpgradeStatements extends AbstractUpgradeStatements {

@Override
public String getStatements() {

String mysqlUpgradeStatements = """
UPDATE SyncopeGroup SET uManager_id=userOwner_id;

SET @fk_name = (
SELECT CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'SyncopeGroup'
AND COLUMN_NAME = 'userOwner_id'
AND REFERENCED_TABLE_NAME IS NOT NULL
LIMIT 1
);

SET @sql = CONCAT(
'ALTER TABLE SyncopeGroup DROP FOREIGN KEY ',
@fk_name
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
ALTER TABLE SyncopeGroup DROP COLUMN userOwner_id;

UPDATE SyncopeGroup SET gManager_id=groupOwner_id;
SET @fk_name = (
SELECT CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'SyncopeGroup'
AND COLUMN_NAME = 'groupOwner_id'
AND REFERENCED_TABLE_NAME IS NOT NULL
LIMIT 1
);

SET @sql = CONCAT(
'ALTER TABLE SyncopeGroup DROP FOREIGN KEY ',
@fk_name
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
ALTER TABLE SyncopeGroup DROP COLUMN groupOwner_id;

DELETE FROM AccessToken;
ALTER TABLE AccessToken MODIFY COLUMN authorities TEXT;

INSERT INTO OIDCOpEntity (id, jwks, customScopes)
SELECT id, CAST(json AS BINARY) ,'{}'
FROM OIDCJWKS;

DROP TABLE OIDCJWKS;
""";

return commonStatements() + mysqlUpgradeStatements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.persistence.jpa.upgrade;

public class OracleUpgradeStatements extends AbstractUpgradeStatements {

@Override
public String getStatements() {

String postgreSQLUpgradeStatements = """
UPDATE SyncopeGroup SET uManager_id=userOwner_id;
ALTER TABLE SyncopeGroup DROP COLUMN userOwner_id;

UPDATE SyncopeGroup SET gManager_id=groupOwner_id;
ALTER TABLE SyncopeGroup DROP COLUMN groupOwner_id;

DELETE FROM ACCESSTOKEN;
ALTER TABLE ACCESSTOKEN MODIFY AUTHORITIES CLOB;
INSERT INTO OIDCOpEntity (id, jwks, customScopes)
SELECT id, clob_to_blob(json),'{}' FROM OIDCJWKS;

DROP TABLE OIDCJWKS;
""";

return convertCLOBToBLOBFunction() + commonStatements() + postgreSQLUpgradeStatements;
}

private String convertCLOBToBLOBFunction() {
return """
CREATE OR REPLACE FUNCTION clob_to_blob(
p_clob IN CLOB
) RETURN BLOB IS
l_blob BLOB;
l_dest_offset INTEGER := 1;
l_src_offset INTEGER := 1;
l_lang_ctx INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
l_warning INTEGER;
BEGIN
IF p_clob IS NULL THEN
RETURN NULL;
END IF;

DBMS_LOB.CREATETEMPORARY(l_blob, TRUE);

DBMS_LOB.CONVERTTOBLOB(
dest_lob => l_blob,
src_clob => p_clob,
amount => DBMS_LOB.LOBMAXSIZE,
dest_offset => l_dest_offset,
src_offset => l_src_offset,
blob_csid => NLS_CHARSET_ID('AL32UTF8'),
lang_context => l_lang_ctx,
warning => l_warning
);

RETURN l_blob;
END;

""";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.persistence.jpa.upgrade;

public class PostgreSQLUpgradeStatements extends AbstractUpgradeStatements {

@Override
public String getStatements() {

String postgreSQLUpgradeStatements = """
UPDATE SyncopeGroup SET uManager_id=userOwner_id;
ALTER TABLE SyncopeGroup DROP COLUMN userOwner_id;

UPDATE SyncopeGroup SET gManager_id=groupOwner_id;
ALTER TABLE SyncopeGroup DROP COLUMN groupOwner_id;

DELETE FROM AccessToken;
ALTER TABLE AccessToken ALTER COLUMN authorities TYPE TEXT;
INSERT INTO OIDCOpEntity (id, jwks, customScopes)
SELECT id,convert_to(json, 'UTF-8') AS jwks,'{}' AS customScopes FROM OIDCJWKS;

DROP TABLE OIDCJWKS;
""";

return commonStatements() + postgreSQLUpgradeStatements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.persistence.jpa.upgrade;

public interface UpgradeStatements {

String getStatements();
}
Loading
Loading