@Test(expected = SchemaMigrationException.class) public void testWithNoMigrationTable() { getSession().execute(SchemaBuilder.dropTable(SchemaMigrationMetadataRepositoryImpl.METADATA_TABLE)); new SchemaMigrator().execute(new SchemaMigrationMetadataRepositoryImpl(getSession())); }
public void execute(SchemaMigrationMetadataRepository repository) { for (SchemaMigrationDescriptor descriptor : descriptors) { final SchemaMigrationMetadata expected = new SchemaMigrationMetadata(descriptor); repository.create(expected); final SchemaMigrationMetadata actual = repository.retrieve(descriptor.getId()); if (ObjectUtils.notEqual(expected.getIndex(), actual.getIndex())) { throw new SchemaMigrationException("Schema migrations out of sync. Schema migration \"%s\" previously executed at position %d, but is currently at position %d.", descriptor.getId(), actual.getIndex(), expected.getIndex()); } if (StringUtils.equals(expected.getToken(), actual.getToken())) { LOGGER.info("Executing schema migration \"{}\"...", actual.getId()); descriptor.getSchemaMigration().execute(repository.getSession()); actual.setStatus(SchemaMigrationStatus.Complete); repository.update(actual); } else { if (actual.isRunning()) { LOGGER.error("Another process is currently executing schema migration \"{}\", aborting.", actual.getId()); return; } else if (!StringUtils.equals(expected.getFingerprint(), actual.getFingerprint())) { LOGGER.error("Schema migration \"{}\" has changed since it was executed (fingerprint mismatch), aborting.", actual.getId()); return; } LOGGER.info("Schema migration \"{}\" already complete, continuing...", actual.getId()); } } }
SchemaMigrator { public void execute(SchemaMigrationMetadataRepository repository) { for (SchemaMigrationDescriptor descriptor : descriptors) { final SchemaMigrationMetadata expected = new SchemaMigrationMetadata(descriptor); repository.create(expected); final SchemaMigrationMetadata actual = repository.retrieve(descriptor.getId()); if (ObjectUtils.notEqual(expected.getIndex(), actual.getIndex())) { throw new SchemaMigrationException("Schema migrations out of sync. Schema migration \"%s\" previously executed at position %d, but is currently at position %d.", descriptor.getId(), actual.getIndex(), expected.getIndex()); } if (StringUtils.equals(expected.getToken(), actual.getToken())) { LOGGER.info("Executing schema migration \"{}\"...", actual.getId()); descriptor.getSchemaMigration().execute(repository.getSession()); actual.setStatus(SchemaMigrationStatus.Complete); repository.update(actual); } else { if (actual.isRunning()) { LOGGER.error("Another process is currently executing schema migration \"{}\", aborting.", actual.getId()); return; } else if (!StringUtils.equals(expected.getFingerprint(), actual.getFingerprint())) { LOGGER.error("Schema migration \"{}\" has changed since it was executed (fingerprint mismatch), aborting.", actual.getId()); return; } LOGGER.info("Schema migration \"{}\" already complete, continuing...", actual.getId()); } } } }
SchemaMigrator { public void execute(SchemaMigrationMetadataRepository repository) { for (SchemaMigrationDescriptor descriptor : descriptors) { final SchemaMigrationMetadata expected = new SchemaMigrationMetadata(descriptor); repository.create(expected); final SchemaMigrationMetadata actual = repository.retrieve(descriptor.getId()); if (ObjectUtils.notEqual(expected.getIndex(), actual.getIndex())) { throw new SchemaMigrationException("Schema migrations out of sync. Schema migration \"%s\" previously executed at position %d, but is currently at position %d.", descriptor.getId(), actual.getIndex(), expected.getIndex()); } if (StringUtils.equals(expected.getToken(), actual.getToken())) { LOGGER.info("Executing schema migration \"{}\"...", actual.getId()); descriptor.getSchemaMigration().execute(repository.getSession()); actual.setStatus(SchemaMigrationStatus.Complete); repository.update(actual); } else { if (actual.isRunning()) { LOGGER.error("Another process is currently executing schema migration \"{}\", aborting.", actual.getId()); return; } else if (!StringUtils.equals(expected.getFingerprint(), actual.getFingerprint())) { LOGGER.error("Schema migration \"{}\" has changed since it was executed (fingerprint mismatch), aborting.", actual.getId()); return; } LOGGER.info("Schema migration \"{}\" already complete, continuing...", actual.getId()); } } }  }
SchemaMigrator { public void execute(SchemaMigrationMetadataRepository repository) { for (SchemaMigrationDescriptor descriptor : descriptors) { final SchemaMigrationMetadata expected = new SchemaMigrationMetadata(descriptor); repository.create(expected); final SchemaMigrationMetadata actual = repository.retrieve(descriptor.getId()); if (ObjectUtils.notEqual(expected.getIndex(), actual.getIndex())) { throw new SchemaMigrationException("Schema migrations out of sync. Schema migration \"%s\" previously executed at position %d, but is currently at position %d.", descriptor.getId(), actual.getIndex(), expected.getIndex()); } if (StringUtils.equals(expected.getToken(), actual.getToken())) { LOGGER.info("Executing schema migration \"{}\"...", actual.getId()); descriptor.getSchemaMigration().execute(repository.getSession()); actual.setStatus(SchemaMigrationStatus.Complete); repository.update(actual); } else { if (actual.isRunning()) { LOGGER.error("Another process is currently executing schema migration \"{}\", aborting.", actual.getId()); return; } else if (!StringUtils.equals(expected.getFingerprint(), actual.getFingerprint())) { LOGGER.error("Schema migration \"{}\" has changed since it was executed (fingerprint mismatch), aborting.", actual.getId()); return; } LOGGER.info("Schema migration \"{}\" already complete, continuing...", actual.getId()); } } }  SchemaMigrator addMigration(String id, SchemaMigration migration); void execute(SchemaMigrationMetadataRepository repository); }
SchemaMigrator { public void execute(SchemaMigrationMetadataRepository repository) { for (SchemaMigrationDescriptor descriptor : descriptors) { final SchemaMigrationMetadata expected = new SchemaMigrationMetadata(descriptor); repository.create(expected); final SchemaMigrationMetadata actual = repository.retrieve(descriptor.getId()); if (ObjectUtils.notEqual(expected.getIndex(), actual.getIndex())) { throw new SchemaMigrationException("Schema migrations out of sync. Schema migration \"%s\" previously executed at position %d, but is currently at position %d.", descriptor.getId(), actual.getIndex(), expected.getIndex()); } if (StringUtils.equals(expected.getToken(), actual.getToken())) { LOGGER.info("Executing schema migration \"{}\"...", actual.getId()); descriptor.getSchemaMigration().execute(repository.getSession()); actual.setStatus(SchemaMigrationStatus.Complete); repository.update(actual); } else { if (actual.isRunning()) { LOGGER.error("Another process is currently executing schema migration \"{}\", aborting.", actual.getId()); return; } else if (!StringUtils.equals(expected.getFingerprint(), actual.getFingerprint())) { LOGGER.error("Schema migration \"{}\" has changed since it was executed (fingerprint mismatch), aborting.", actual.getId()); return; } LOGGER.info("Schema migration \"{}\" already complete, continuing...", actual.getId()); } } }  SchemaMigrator addMigration(String id, SchemaMigration migration); void execute(SchemaMigrationMetadataRepository repository);  }