@Override
protected void execute(Context context) throws SQLException {
+ populateUserUuid(context);
+ removeRowWithNonExistentUser(context);
+ }
+
+ private static void populateUserUuid(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate.select("select p.uuid, u.uuid " +
return true;
});
}
+
+ private static void removeRowWithNonExistentUser(Context context) throws SQLException {
+ context.prepareUpsert("delete from properties where user_uuid is null and user_id is not null")
+ .execute()
+ .commit();
+ }
}
assertThatPropertyUserUuidIsEqualTo(propertyUuid_5, null);
}
+ @Test
+ public void should_remove_property_for_non_existent_user() throws SQLException {
+ long userId_1 = 1L;
+ String userUuid_1 = "uuid-1";
+ insertUser(userId_1, userUuid_1);
+
+ long userId_2 = 2L;
+
+ long userId_3 = 3L;
+
+ String propertyUuid_1 = Uuids.createFast();
+ insertProperty(propertyUuid_1, userId_1);
+ String propertyUuid_2 = Uuids.createFast();
+ insertProperty(propertyUuid_2, userId_2);
+ String propertyUuid_3 = Uuids.createFast();
+ insertProperty(propertyUuid_3, userId_3);
+
+ underTest.execute();
+
+ assertThatPropertyUserUuidIsEqualTo(propertyUuid_1, userUuid_1);
+ assertPropertyIsRemoved(propertyUuid_2);
+ assertPropertyIsRemoved(propertyUuid_3);
+ }
+
@Test
public void migration_is_reentrant() throws SQLException {
long userId_1 = 1L;
}
+ private void assertPropertyIsRemoved(String propertyUuid){
+ assertThat(db.select(String.format("select 1 from properties where uuid = '%s'", propertyUuid))).isEmpty();
+ }
+
private void insertProperty(String uuid, Long userId) {
db.executeInsert("properties",
"uuid", uuid,