UpgradeCatalog250 extends AbstractUpgradeCatalog { @Override protected void executeDDLUpdates() throws AmbariException, SQLException { updateHostVersionTable(); createComponentVersionTable(); updateGroupsTable(); dbAccessor.addColumn("stage", new DBAccessor.DBColumnInfo("command_execution_type", String.class, 32, CommandExecutionType.STAGE.toString(), false)); updateServiceDesiredStateTable(); updateHostComponentDesiredStateTable(); } @Inject  UpgradeCatalog250(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion(); static final String COMPONENT_TABLE; static final String COMPONENT_VERSION_TABLE; static final String COMPONENT_VERSION_PK; static final String COMPONENT_VERSION_FK_COMPONENT; static final String COMPONENT_VERSION_FK_REPO_VERSION; }
@Test public void testExecuteDDLUpdates() throws Exception { dbAccessor.addUniqueConstraint("host_version", "UQ_host_repo", "repo_version_id", "host_id"); Capture<DBAccessor.DBColumnInfo> groupGroupType = newCapture(); dbAccessor.addColumn(eq(UpgradeCatalog250.GROUPS_TABLE), capture(groupGroupType)); dbAccessor.addUniqueConstraint("groups", "UNQ_groups_0", "group_name", "group_type"); expectLastCall().once(); Capture<List<DBAccessor.DBColumnInfo>> capturedComponentVersionColumns = newCapture(); dbAccessor.createTable(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE), capture(capturedComponentVersionColumns), eq((String[]) null)); dbAccessor.addPKConstraint(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE), eq(UpgradeCatalog250.COMPONENT_VERSION_PK), eq("id")); dbAccessor.addFKConstraint(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE), eq(UpgradeCatalog250.COMPONENT_VERSION_FK_COMPONENT), eq("component_id"), eq(UpgradeCatalog250.COMPONENT_TABLE), eq("id"), eq(false)); dbAccessor.addFKConstraint(eq(UpgradeCatalog250.COMPONENT_VERSION_TABLE), eq(UpgradeCatalog250.COMPONENT_VERSION_FK_REPO_VERSION), eq("repo_version_id"), eq("repo_version"), eq("repo_version_id"), eq(false)); Capture<DBAccessor.DBColumnInfo> capturedCredentialStoreEnabledCol = newCapture(); dbAccessor.addColumn(eq(UpgradeCatalog250.SERVICE_DESIRED_STATE_TABLE), capture(capturedCredentialStoreEnabledCol)); expect(dbAccessor.getConnection()).andReturn(connection).anyTimes(); expect(connection.createStatement()).andReturn(statement).anyTimes(); expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet).anyTimes(); expect(configuration.getDatabaseType()).andReturn(Configuration.DatabaseType.POSTGRES).anyTimes(); replay(dbAccessor, configuration, connection, statement, resultSet); Module module = new Module() { @Override public void configure(Binder binder) { binder.bind(DBAccessor.class).toInstance(dbAccessor); binder.bind(OsFamily.class).toInstance(osFamily); binder.bind(EntityManager.class).toInstance(entityManager); binder.bind(Configuration.class).toInstance(configuration); } }; Injector injector = Guice.createInjector(module); UpgradeCatalog250 upgradeCatalog250 = injector.getInstance(UpgradeCatalog250.class); upgradeCatalog250.executeDDLUpdates(); DBAccessor.DBColumnInfo capturedGroupTypeColumn = groupGroupType.getValue(); Assert.assertNotNull(capturedGroupTypeColumn); Assert.assertEquals(UpgradeCatalog250.GROUP_TYPE_COL, capturedGroupTypeColumn.getName()); Assert.assertEquals(String.class, capturedGroupTypeColumn.getType()); Assert.assertEquals(null, capturedGroupTypeColumn.getLength()); Assert.assertEquals("LOCAL", capturedGroupTypeColumn.getDefaultValue()); Assert.assertEquals(false, capturedGroupTypeColumn.isNullable()); verify(dbAccessor); Map<String, DBAccessor.DBColumnInfo> expected = new HashMap<>(); expected.put("id", new DBAccessor.DBColumnInfo("id", Long.class, null, null, false)); expected.put("component_id", new DBAccessor.DBColumnInfo("component_id", Long.class, null, null, false)); expected.put("repo_version_id", new DBAccessor.DBColumnInfo("repo_version_id", Long.class, null, null, false)); expected.put("state", new DBAccessor.DBColumnInfo("state", String.class, 32, null, false)); expected.put("user_name", new DBAccessor.DBColumnInfo("user_name", String.class, 255, null, false)); List<DBAccessor.DBColumnInfo> captured = capturedComponentVersionColumns.getValue(); Assert.assertEquals(5, captured.size()); for (DBAccessor.DBColumnInfo column : captured) { DBAccessor.DBColumnInfo expectedColumn = expected.remove(column.getName()); Assert.assertNotNull(expectedColumn); Assert.assertEquals(expectedColumn.getDefaultValue(), column.getDefaultValue()); Assert.assertEquals(expectedColumn.getName(), column.getName()); Assert.assertEquals(expectedColumn.getLength(), column.getLength()); Assert.assertEquals(expectedColumn.getType(), column.getType()); Assert.assertEquals(expectedColumn.getClass(), column.getClass()); } Assert.assertEquals(0, expected.size()); DBAccessor.DBColumnInfo capturedCredentialStoreEnabledColValues = capturedCredentialStoreEnabledCol.getValue(); Assert.assertNotNull(capturedCredentialStoreEnabledColValues); Assert.assertEquals(UpgradeCatalog250.CREDENTIAL_STORE_ENABLED_COL, capturedCredentialStoreEnabledColValues.getName()); Assert.assertEquals(null, capturedCredentialStoreEnabledColValues.getLength()); Assert.assertEquals(Short.class, capturedCredentialStoreEnabledColValues.getType()); Assert.assertEquals(0, capturedCredentialStoreEnabledColValues.getDefaultValue()); Assert.assertEquals(false, capturedCredentialStoreEnabledColValues.isNullable()); }