add hashcode to initializationProvider

This commit is contained in:
Ken Stevens 2019-11-01 22:00:59 -04:00
parent 7855ce070c
commit 071b14cbcc
4 changed files with 86 additions and 3 deletions

View File

@ -22,6 +22,7 @@ package ca.uhn.fhir.jpa.migrate;
import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask; import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
import org.flywaydb.core.Flyway; import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfoService; import org.flywaydb.core.api.MigrationInfoService;
@ -161,4 +162,9 @@ public class FlywayMigrator {
return flyway.info(); return flyway.info();
} }
} }
@VisibleForTesting
public void removeAllTasksForUnitTest() {
myTasks.clear();
}
} }

View File

@ -2,6 +2,8 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider; import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -35,4 +37,24 @@ public class InitializeSchemaTask extends BaseTask<InitializeSchemaTask> {
executeSql(null, nextSql); executeSql(null, nextSql);
} }
} }
@Override
public boolean equals(Object theO) {
if (this == theO) return true;
if (theO == null || getClass() != theO.getClass()) return false;
InitializeSchemaTask that = (InitializeSchemaTask) theO;
return new EqualsBuilder()
.append(mySchemaInitializationProvider, that.mySchemaInitializationProvider)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(mySchemaInitializationProvider)
.toHashCode();
}
} }

View File

@ -5,6 +5,8 @@ import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider; import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -41,4 +43,26 @@ public class SchemaInitializationProvider implements ISchemaInitializationProvid
} }
return retval; return retval;
} }
@Override
public boolean equals(Object theO) {
if (this == theO) return true;
if (theO == null || getClass() != theO.getClass()) return false;
SchemaInitializationProvider that = (SchemaInitializationProvider) theO;
return size() == that.size();
}
private int size() {
return getSqlStatements(DriverTypeEnum.H2_EMBEDDED).size();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(size())
.toHashCode();
}
} }

View File

@ -2,6 +2,9 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.JdbcUtils; import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import ca.uhn.fhir.jpa.migrate.tasks.SchemaInitializationProvider;
import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.junit.Test; import org.junit.Test;
import java.sql.SQLException; import java.sql.SQLException;
@ -15,16 +18,44 @@ public class InitializeSchemaTaskTest extends BaseTest {
@Test @Test
public void testInitializeTwice() throws SQLException { public void testInitializeTwice() throws SQLException {
InitializeSchemaTask task = new InitializeSchemaTask("1", "1", t -> getSql()); InitializeSchemaTask task = new InitializeSchemaTask("1", "1", new TestProvider());
getMigrator().addTask(task); getMigrator().addTask(task);
getMigrator().migrate(); getMigrator().migrate();
assertThat(JdbcUtils.getTableNames(getConnectionProperties()), containsInAnyOrder("SOMETABLE")); assertThat(JdbcUtils.getTableNames(getConnectionProperties()), containsInAnyOrder("SOMETABLE"));
// Second migrate runs without issue // Second migrate runs without issue
getMigrator().removeAllTasksForUnitTest();
InitializeSchemaTask identicalTask = new InitializeSchemaTask("1", "1", new TestProvider());
getMigrator().addTask(identicalTask);
getMigrator().migrate(); getMigrator().migrate();
} }
private List<String> getSql() { private class TestProvider implements ISchemaInitializationProvider {
@Override
public List<String> getSqlStatements(DriverTypeEnum theDriverType) {
return Collections.singletonList("create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))"); return Collections.singletonList("create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))");
} }
@Override
public boolean equals(Object theO) {
if (this == theO) return true;
if (theO == null || getClass() != theO.getClass()) return false;
TestProvider that = (TestProvider) theO;
return size() == that.size();
}
private int size() {
return getSqlStatements(DriverTypeEnum.H2_EMBEDDED).size();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(size())
.toHashCode();
}
}
} }