@Test public void testCurrentVersionStructureAgainstGold() throws Throwable { int latestVersion = CatalogVersions.getCurrentVersion().getSchemaVersion(); String[] lastKnown = UpgradeTestUtils.getGoldVersion(latestVersion); String[] current = generateCatalog(); Pattern thanksHibernate = Pattern.compile("(alter table )(\\w+)( add index )(\\w+)(.*add constraint )(\\w+)(.*)"); assertEquals("number of stmts in schema ddl", lastKnown.length, current.length - 1); for (int i = 0; i < current.length - 1; i++) { if (!lastKnown[i].equals(current[i])) { // accept differences in index, constraint names if (lastKnown[i].startsWith("alter table") && current[i].startsWith("alter table")) { Matcher last = thanksHibernate.matcher(lastKnown[i]); Matcher now = thanksHibernate.matcher(current[i]); if (last.matches() && now.matches()) { String knownConstraint = lastKnown[i].substring(last.start(6), last.end(6)); String knownIndex = lastKnown[i].substring(last.start(4), last.end(4)); StringBuilder buf = new StringBuilder(); buf.append(current[i].substring(0, now.start(4))); buf.append(knownIndex); buf.append(current[i].substring(now.end(4), now.start(6))); buf.append(knownConstraint); buf.append(current[i].substring(now.end(6))); String munged = buf.toString(); if (lastKnown[i].equals(munged)) continue; } } assertEquals("schema ddl stmt " + i, lastKnown[i], current[i]); } } }
public void upgrade(InformationCallback stdout) throws PEException { DBHelper helper = new DBHelper(connectProperties); String driver = DBHelper.loadDriver(connectProperties.getProperty(DBHelper.CONN_DRIVER_CLASS)); DBType dbType = DBType.fromDriverClass(driver); DBNative dbn = DBNative.DBNativeFactory.newInstance(dbType); helper.connect(); try { CatalogSchemaVersion latest = CatalogVersions.getCurrentVersion(); CatalogSchemaVersion current = CatalogSchemaVersion.getPersistentVersion(helper); CatalogVersionNumber oldestSupported = CatalogVersions.getOldestUpgradeSupported(); if (current.getSchemaVersion() < oldestSupported.getCatalogVersion().getSchemaVersion()) throw new PEException( "No upgrade available for version " + current.getSchemaVersion() + ": please dump and load"); List<CatalogVersion> versionHistory = CatalogVersions.getVersionHistory(); int currentlyAt = -1; for (int i = 0; i < versionHistory.size(); i++) { if (versionHistory.get(i).getSchemaVersion() == current.getSchemaVersion()) { currentlyAt = i; break; } } if (currentlyAt == -1) throw new PEException( "No known upgrade for catalog schema version " + current.getSchemaVersion()); // keep track of whether an info schema upgrade is needed. if so we will only do it at the // end. boolean requiresInfoSchemaUpgrade = false; int firstUpgrade = currentlyAt + 1; for (int i = firstUpgrade; i < versionHistory.size(); i++) { CatalogVersion cv = versionHistory.get(i); if (cv.hasInfoSchemaUpgrade()) requiresInfoSchemaUpgrade = true; if (cv.getSchemaVersion() == latest.getSchemaVersion()) upgrade(helper, cv, latest, (requiresInfoSchemaUpgrade ? dbn : null), stdout); else upgrade( helper, cv, new CatalogSchemaVersion(cv.getSchemaVersion(), "via upgrade", "current"), null, stdout); } } finally { helper.disconnect(); } }
@Test public void testCurrentVersionContentsAgainstGold() throws Throwable { int latestVersion = CatalogVersions.getCurrentVersion().getSchemaVersion(); String[] lastKnown = UpgradeTestUtils.getInfoSchema(latestVersion); String[] current = CatalogSchemaGenerator.buildTestCurrentInfoSchema(); assertEquals("number of stmts in info schema dml", lastKnown.length, current.length); for (int i = 0; i < lastKnown.length; i++) { assertEquals("info schema dml " + i, lastKnown[i], current[i]); } }
@Test public void writeCurrentSchemaGold() throws Throwable { if (Boolean.getBoolean(propEnableName)) { String filename = System.getProperty(propDirName); if (filename == null) throw new PEException( "Must specify a directory via -D" + propDirName + " prior to running writeCurrentSchemaGold."); int latestVersion = CatalogVersions.getCurrentVersion().getSchemaVersion(); String[] current = generateCatalog(); writeFile(filename, "catalog_version" + latestVersion + ".sql", current, current.length - 1); current = CatalogSchemaGenerator.buildTestCurrentInfoSchema(); writeFile(filename, "infoschema_version" + latestVersion + ".sql", current, current.length); } }