Merge pull request #1504 from mposolda/master

Migration
This commit is contained in:
Marek Posolda 2015-07-25 11:36:04 +02:00
commit 0d7f26bd14
4 changed files with 95 additions and 8 deletions

View file

@ -27,7 +27,8 @@ public class DefaultMongoUpdaterProvider implements MongoUpdaterProvider {
Update1_1_0_Beta1.class,
Update1_2_0_Beta1.class,
Update1_2_0_CR1.class,
Update1_3_0.class
Update1_3_0.class,
Update1_4_0.class
};
@Override

View file

@ -0,0 +1,63 @@
package org.keycloak.connections.mongo.updater.impl.updates;
import java.util.HashSet;
import java.util.Map;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import org.keycloak.models.KeycloakSession;
import org.keycloak.representations.idm.IdentityProviderRepresentation;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class Update1_4_0 extends Update {
@Override
public String getId() {
return "1.4.0";
}
@Override
public void update(KeycloakSession session) throws ClassNotFoundException {
deleteEntries("clientSessions");
deleteEntries("sessions");
// Remove warning
removeField("realms", "authenticators");
updateUserAttributes();
}
private void updateUserAttributes() {
DBCollection users = db.getCollection("users");
DBCursor usersCursor = users.find();
try {
while (usersCursor.hasNext()) {
BasicDBObject user = (BasicDBObject) usersCursor.next();
BasicDBObject attributes = (BasicDBObject) user.get("attributes");
if (attributes != null) {
for (Map.Entry<String, Object> attr : new HashSet<>(attributes.entrySet())) {
String attrName = attr.getKey();
Object attrValue = attr.getValue();
if (attrValue != null && attrValue instanceof String) {
BasicDBList asList = new BasicDBList();
asList.add(attrValue);
attributes.put(attrName, asList);
}
}
user.put("attributes", attributes);
users.save(user);
}
}
} finally {
usersCursor.close();
}
}
}

View file

@ -56,9 +56,24 @@ public class ModelVersion {
}
public boolean lessThan(ModelVersion version) {
if (major < version.major) return true;
if (minor < version.minor) return true;
if (micro < version.micro) return true;
if (major < version.major) {
return true;
} else if (major > version.major) {
return false;
}
if (minor < version.minor) {
return true;
} else if (minor > version.minor) {
return false;
}
if (micro < version.micro) {
return true;
} else if (minor > version.minor) {
return false;
}
if (qualifier != null && qualifier.equals(version.qualifier)) return false;
if (qualifier == null) return false;
if (version.qualifier == null) return true;

View file

@ -21,10 +21,12 @@ public class MigrationVersionTest {
ModelVersion version_110Beta1 = new ModelVersion("1.1.0.Beta1");
ModelVersion version_110CR1 = new ModelVersion("1.1.0.CR1");
ModelVersion version_110 = new ModelVersion("1.1.0");
ModelVersion version_111Beta1 = new ModelVersion("1.1.1.Beta1");
ModelVersion version_111CR1 = new ModelVersion("1.1.1.CR1");
ModelVersion version_111 = new ModelVersion("1.1.1");
ModelVersion version_211Beta1 = new ModelVersion("2.1.1.Beta1");
ModelVersion version_120CR1 = new ModelVersion("1.2.0.CR1");
ModelVersion version_130Beta1 = new ModelVersion("1.3.0.Beta1");
ModelVersion version_130 = new ModelVersion("1.3.0");
ModelVersion version_140 = new ModelVersion("1.4.0");
ModelVersion version_211CR1 = new ModelVersion("2.1.1.CR1");
Assert.assertEquals(version_211CR1.getMajor(), 2);
Assert.assertEquals(version_211CR1.getMinor(), 1);
@ -38,6 +40,12 @@ public class MigrationVersionTest {
Assert.assertTrue(version_100Beta1.lessThan(version_110Beta1));
Assert.assertTrue(version_100Beta1.lessThan(version_110CR1));
Assert.assertTrue(version_100Beta1.lessThan(version_110));
Assert.assertFalse(version_110Beta1.lessThan(version_100CR1));
Assert.assertFalse(version_130Beta1.lessThan(version_120CR1));
Assert.assertTrue(version_130Beta1.lessThan(version_130));
Assert.assertTrue(version_130Beta1.lessThan(version_140));
Assert.assertFalse(version_211CR1.lessThan(version_140));
Assert.assertTrue(version_140.lessThan(version_211CR1));
Assert.assertFalse(version_211.lessThan(version_110CR1));