use of com.helger.phoss.smp.backend.sql.EDatabaseType in project phoss-smp by phax.
the class SMPUserManagerJDBC method updateOwnershipsAndKillUsers.
public void updateOwnershipsAndKillUsers(@Nonnull final ICommonsMap<String, String> aOldToNewUserNameMap) {
ValueEnforcer.notNull(aOldToNewUserNameMap, "OldToNewUserNameMap");
final DBExecutor aExecutor = newExecutor();
aExecutor.performInTransaction(() -> {
// Drop the Foreign Key Constraint - do this all the time
try {
final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
switch(eDBType) {
case MYSQL:
aExecutor.executeStatement("ALTER TABLE smp_ownership DROP FOREIGN KEY FK_smp_ownership_username;");
break;
case POSTGRESQL:
aExecutor.executeStatement("ALTER TABLE smp_ownership DROP CONSTRAINT FK_smp_ownership_username;");
break;
case ORACLE:
case DB2:
// No such constraint
break;
default:
throw new IllegalStateException("The migration code for DB type " + eDBType + " is missing");
}
} catch (final RuntimeException ex) {
LOGGER.warn("Error in droping constraints", ex);
}
// Update user names
for (final Map.Entry<String, String> aEntry : aOldToNewUserNameMap.entrySet()) {
final String sOld = aEntry.getKey();
final String sNew = aEntry.getValue();
aExecutor.insertOrUpdateOrDelete("UPDATE smp_ownership SET username=? WHERE username=?", new ConstantPreparedStatementDataProvider(sNew, sOld));
}
try {
aExecutor.executeStatement("DROP TABLE smp_user;");
} catch (final RuntimeException ex) {
LOGGER.warn("Error in droping table smp_user", ex);
}
});
}
use of com.helger.phoss.smp.backend.sql.EDatabaseType in project phoss-smp by phax.
the class V15__MigrateDBUsersToPhotonUsers method migrate.
public void migrate(@Nonnull final Context context) throws Exception {
try (final WebScoped aWS = new WebScoped()) {
LOGGER.info("Migrating all old DB users to ph-oton users");
final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
// Old JDBC user manager
final SMPUserManagerJDBC aSQLUserMgr = new SMPUserManagerJDBC(SMPDBExecutor::new);
final ICommonsList<DBUser> aSQLUsers = aSQLUserMgr.getAllUsers();
LOGGER.info("Found " + aSQLUsers.size() + " DB user to migrate");
final ICommonsOrderedMap<String, String> aCreatedMappings = new CommonsLinkedHashMap<>();
// New JDBC user manager
final IUserManager aPhotonUserMgr = PhotonSecurityManager.getUserMgr();
for (final DBUser aSQLUser : aSQLUsers) {
final DBUser aDBUser = aSQLUser;
IUser aPhotonUser = null;
int nIndex = 0;
while (true) {
final String sUserName = aDBUser.getUserName() + (nIndex > 0 ? Integer.toString(nIndex) : "");
// The suffix "@example.org" is added to make it an email-address
final String sEmailAddress = sUserName + "@example.org";
aPhotonUser = aPhotonUserMgr.createNewUser(sEmailAddress, sEmailAddress, aDBUser.getPassword(), null, sUserName, null, CSMPServer.DEFAULT_LOCALE, null, false);
if (aPhotonUser != null) {
// New user was successfully created
break;
}
// User name already taken
++nIndex;
if (nIndex > 1000) {
// Avoid endless loop
throw new IllegalStateException("Too many iterations mapping the DB user '" + aDBUser.getUserName() + "' to a ph-oton user");
}
}
aCreatedMappings.put(aDBUser.getUserName(), aPhotonUser.getID());
LOGGER.info("Mapped DB user '" + aDBUser.getUserName() + "' to ph-oton user " + aPhotonUser.getID());
}
// Update the ownership in "smp_ownership"
// Remove the table "smp_user"
aSQLUserMgr.updateOwnershipsAndKillUsers(aCreatedMappings);
if (XMLMapHandler.writeMap(aCreatedMappings, new FileSystemResource(WebFileIO.getDataIO().getFile("migrations/db-photon-user-mapping-" + eDBType.getID() + ".xml"))).isFailure())
LOGGER.error("Failed to store mapping of DB users to ph-oton users as XML");
LOGGER.info("Finished migrating all DB users to ph-oton users");
}
}
Aggregations