Search in sources :

Example 1 with Migration

use of org.opennms.core.schema.Migration in project opennms by OpenNMS.

the class MigratorIT method testMultipleChangelogs.

@Test
@JUnitTemporaryDatabase(createSchema = false)
public void testMultipleChangelogs() throws Exception {
    assertFalse(changelogExists());
    final Migration migration = new Migration();
    migration.setAdminUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setAdminPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setDatabaseUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setDatabasePassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setChangeLog("changelog.xml");
    final Migrator m = new Migrator();
    m.setDataSource(m_dataSource);
    m.setAdminDataSource(m_dataSource);
    m.setValidateDatabaseVersion(false);
    m.setCreateUser(false);
    m.setCreateDatabase(false);
    // from the classpath
    for (final Resource resource : getTestResources()) {
        URI uri = resource.getURI();
        if (uri.getScheme().equals("jar") && !uri.toString().contains("test-api.schema"))
            continue;
        if (uri.getScheme().equals("file") && !uri.toString().contains("test-api/schema"))
            continue;
        LOG.info("=== found resource: {} ===", resource);
        migration.setAccessor(new ExistingResourceAccessor(resource));
        m.migrate(migration);
    }
    final List<ChangelogEntry> ids = getChangelogEntries();
    assertTrue(ids.size() > 0);
    assertEquals("test-api.schema.a", ids.get(0).getId());
    assertEquals("test-api.schema.b", ids.get(1).getId());
}
Also used : Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) Migrator(org.opennms.core.schema.Migrator) URI(java.net.URI) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 2 with Migration

use of org.opennms.core.schema.Migration in project opennms by OpenNMS.

the class MigratorIT method doMigration.

private void doMigration() throws MigrationException, IOException {
    final Migration migration = new Migration();
    migration.setAdminUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setAdminPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setDatabaseUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setDatabasePassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setChangeLog("changelog.xml");
    final Migrator m = new Migrator();
    m.setDataSource(m_dataSource);
    for (final Resource resource : getTestResources()) {
        migration.setAccessor(new ExistingResourceAccessor(resource));
        m.migrate(migration);
    }
}
Also used : Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) Migrator(org.opennms.core.schema.Migrator) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor)

Example 3 with Migration

use of org.opennms.core.schema.Migration in project opennms by OpenNMS.

the class MigratorIT method testRealChangelog.

@Test
@JUnitTemporaryDatabase(createSchema = false)
public void testRealChangelog() throws Exception {
    assertFalse(changelogExists());
    final Migration migration = new Migration();
    migration.setAdminUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setAdminPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setDatabaseUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setDatabasePassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setChangeLog("changelog.xml");
    final Migrator m = new Migrator();
    m.setDataSource(m_dataSource);
    m.setAdminDataSource(m_dataSource);
    m.setValidateDatabaseVersion(false);
    m.setCreateUser(false);
    m.setCreateDatabase(false);
    // from the classpath
    for (final Resource resource : getRealChangelog()) {
        LOG.info("=== found resource: {} ===", resource);
        migration.setAccessor(new ExistingResourceAccessor(resource));
        m.migrate(migration);
    }
    final List<ChangelogEntry> ids = getChangelogEntries();
    assertTrue(ids.size() > 0);
    // Check to make sure some of the changelogs ran
    assertTrue(ids.stream().anyMatch(id -> "17.0.0-remove-legacy-ipinterface-composite-key-fields".equals(id.getId())));
    assertTrue(ids.stream().anyMatch(id -> "17.0.0-remove-legacy-outages-composite-key-fields".equals(id.getId())));
}
Also used : Migrator(org.opennms.core.schema.Migrator) Connection(java.sql.Connection) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) MigrationException(org.opennms.core.schema.MigrationException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) URI(java.net.URI) Before(org.junit.Before) Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) Logger(org.slf4j.Logger) ResourceLoader(org.springframework.core.io.ResourceLoader) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) IOException(java.io.IOException) Test(org.junit.Test) PreparedStatement(java.sql.PreparedStatement) ApplicationContext(org.springframework.context.ApplicationContext) MockLogAppender(org.opennms.core.test.MockLogAppender) List(java.util.List) OpenNMSJUnit4ClassRunner(org.opennms.core.test.OpenNMSJUnit4ClassRunner) Assert.assertFalse(org.junit.Assert.assertFalse) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Assert.assertEquals(org.junit.Assert.assertEquals) Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) Migrator(org.opennms.core.schema.Migrator) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 4 with Migration

use of org.opennms.core.schema.Migration in project opennms by OpenNMS.

the class MigratorIT method testUpdate.

@Test
@JUnitTemporaryDatabase(createSchema = false)
public void testUpdate() throws Exception {
    // Make sure there is no databasechangelog table
    assertFalse(changelogExists());
    Resource aResource = null;
    for (final Resource resource : getTestResources()) {
        URI uri = resource.getURI();
        if (uri.getScheme().equals("file") && uri.toString().contains("test-api/schema/a")) {
            aResource = resource;
        }
        if (uri.getScheme().equals("jar") && uri.toString().contains("test-api.schema.a")) {
            aResource = resource;
        }
    }
    assertNotNull("aResource must not be null", aResource);
    Set<String> tables = getTables();
    assertFalse("must not contain table 'schematest'", tables.contains("schematest"));
    final Migration migration = new Migration();
    migration.setAdminUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setAdminPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setDatabaseUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setDatabasePassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setChangeLog("changelog.xml");
    migration.setAccessor(new ExistingResourceAccessor(aResource));
    LOG.info("Running migration on database: {}", migration);
    final Migrator m = new Migrator();
    m.setDataSource(m_dataSource);
    m.setAdminDataSource(m_dataSource);
    m.setValidateDatabaseVersion(false);
    m.setCreateUser(false);
    m.setCreateDatabase(false);
    m.prepareDatabase(migration);
    m.migrate(migration);
    LOG.info("Migration complete: {}", migration);
    tables = getTables();
    assertTrue("must contain table 'schematest'", tables.contains("schematest"));
}
Also used : Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) URI(java.net.URI) Migrator(org.opennms.core.schema.Migrator) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Aggregations

ExistingResourceAccessor (org.opennms.core.schema.ExistingResourceAccessor)4 Migration (org.opennms.core.schema.Migration)4 Migrator (org.opennms.core.schema.Migrator)4 Resource (org.springframework.core.io.Resource)4 URI (java.net.URI)3 Test (org.junit.Test)3 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)3 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 DataSource (javax.sql.DataSource)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1