Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-103998 Improve UpgradeCTModel #8495

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,7 +15,6 @@
package com.liferay.document.library.content.internal.upgrade;

import com.liferay.document.library.content.internal.upgrade.v1_0_0.UpgradeClassNames;
import com.liferay.document.library.content.internal.upgrade.v1_2_0.util.DLContentTable;
import com.liferay.portal.kernel.upgrade.UpgradeCTModel;
import com.liferay.portal.kernel.upgrade.UpgradeMVCCVersion;
import com.liferay.portal.upgrade.registry.UpgradeStepRegistrator;
Expand All @@ -42,8 +41,7 @@ protected String[] getModuleTableNames() {
}

});
registry.register(
"1.1.0", "1.2.0", new UpgradeCTModel(DLContentTable.class));
registry.register("1.1.0", "1.2.0", new UpgradeCTModel("DLContent"));
}

}
Expand Up @@ -277,12 +277,10 @@ protected String[] getModuleTableNames() {
"3.2.0", "3.2.1", new UpgradeDDMDataProviderInstance(_jsonFactory));

registry.register(
"3.2.1", "3.3.0", new UpgradeCTModel(DDMStructureTable.class),
new UpgradeCTModel(
com.liferay.dynamic.data.mapping.internal.upgrade.v3_3_0.util.
DDMStructureVersionTable.class),
new UpgradeCTModel(DDMTemplateTable.class),
new UpgradeCTModel(DDMTemplateVersionTable.class));
"3.2.1", "3.3.0", new UpgradeCTModel("DDMStructure"),
new UpgradeCTModel("DDMStructureVersion"),
new UpgradeCTModel("DDMTemplate"),
new UpgradeCTModel("DDMTemplateVersion"));
}

@Activate
Expand Down
Expand Up @@ -54,8 +54,6 @@
import com.liferay.journal.internal.upgrade.v2_0_0.util.JournalArticleTable;
import com.liferay.journal.internal.upgrade.v2_0_0.util.JournalFeedTable;
import com.liferay.journal.internal.upgrade.v2_0_0.util.JournalFolderTable;
import com.liferay.journal.internal.upgrade.v3_2_0.util.JournalArticleLocalizationTable;
import com.liferay.journal.internal.upgrade.v3_2_0.util.JournalArticleResourceTable;
import com.liferay.journal.model.JournalArticle;
import com.liferay.portal.configuration.upgrade.PrefsPropsToConfigurationUpgradeHelper;
import com.liferay.portal.kernel.dao.db.DB;
Expand Down Expand Up @@ -239,15 +237,10 @@ protected String[] getModuleTableNames() {
});

registry.register(
"3.1.0", "3.2.0",
new UpgradeCTModel(JournalArticleLocalizationTable.class),
new UpgradeCTModel(JournalArticleResourceTable.class),
new UpgradeCTModel(
com.liferay.journal.internal.upgrade.v3_2_0.util.
JournalArticleTable.class),
new UpgradeCTModel(
com.liferay.journal.internal.upgrade.v3_2_0.util.
JournalFolderTable.class));
"3.1.0", "3.2.0", new UpgradeCTModel("JournalArticleLocalization"),
new UpgradeCTModel("JournalArticleResource"),
new UpgradeCTModel("JournalArticle"),
new UpgradeCTModel("JournalFolder"));
}

protected void deleteTempImages() throws Exception {
Expand Down
Expand Up @@ -29,7 +29,6 @@
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -71,13 +70,13 @@ public void setUp() throws Exception {

@After
public void tearDown() throws Exception {
_db.runSQL(TestTableClass.TABLE_SQL_DROP);
_db.runSQL("drop table UpgradeCTModelTest");
}

@Test
public void testUpgrade() throws Exception {
UpgradeCTModel upgradeCTModel = new UpgradeCTModel(
TestTableClass.class);
"UpgradeCTModelTest");

upgradeCTModel.upgrade();

Expand Down Expand Up @@ -108,7 +107,7 @@ public void testUpgrade() throws Exception {
try (ResultSet rs2 = databaseMetaData.getPrimaryKeys(
dbInspector.getCatalog(), dbInspector.getSchema(),
dbInspector.normalizeName(
TestTableClass.TABLE_NAME, databaseMetaData))) {
"UpgradeCTModelTest", databaseMetaData))) {

Assert.assertTrue("Missing PK", rs2.next());

Expand All @@ -131,36 +130,6 @@ public void testUpgrade() throws Exception {
}
}

@SuppressWarnings("unused")
public static class TestTableClass {

public static final Object[][] TABLE_COLUMNS = {
{"mvccVersion", Types.BIGINT}, {"uuid_", Types.VARCHAR},
{"upgradeCTModelId", Types.BIGINT},
{"ctCollectionId", Types.BIGINT}, {"companyId", Types.BIGINT},
{"createDate", Types.TIMESTAMP}, {"modifiedDate", Types.TIMESTAMP},
{"name", Types.VARCHAR}
};

public static final String TABLE_NAME = "UpgradeCTModelTest";

public static final String[] TABLE_SQL_ADD_INDEXES = {
"create index IX_TEST on UpgradeCTModelTest " +
"(uuid_[$COLUMN_LENGTH:75$], companyId)"
};

public static final String TABLE_SQL_CREATE = StringBundler.concat(
"create table UpgradeCTModelTest (mvccVersion LONG default 0 not ",
"null, uuid_ VARCHAR(75) null, upgradeCTModelId LONG not null, ",
"ctCollectionId LONG not null, companyId LONG, createDate DATE ",
"null, modifiedDate DATE null, name STRING null, primary key ",
"(upgradeCTModelId, ctCollectionId))");

public static final String TABLE_SQL_DROP =
"drop table UpgradeCTModelTest";

}

private DB _db;

}
Expand Up @@ -18,8 +18,6 @@
import com.liferay.portal.kernel.upgrade.UpgradeProcess;
import com.liferay.portal.kernel.version.Version;
import com.liferay.portal.upgrade.util.PortalUpgradeProcessRegistry;
import com.liferay.portal.upgrade.v7_3_x.util.LayoutTable;
import com.liferay.portal.upgrade.v7_3_x.util.ResourcePermissionTable;

import java.util.TreeMap;

Expand All @@ -40,13 +38,12 @@ public void registerUpgradeProcesses(
upgradeProcesses.put(new Version(6, 0, 2), new UpgradeLayoutSet());

upgradeProcesses.put(
new Version(6, 0, 3), new UpgradeCTModel(LayoutTable.class));
new Version(6, 0, 3), new UpgradeCTModel("Layout"));

upgradeProcesses.put(new Version(6, 0, 4), new UpgradeAssetCategory());

upgradeProcesses.put(
new Version(6, 0, 5),
new UpgradeCTModel(ResourcePermissionTable.class));
new Version(6, 0, 5), new UpgradeCTModel("ResourcePermission"));

upgradeProcesses.put(new Version(7, 0, 0), new UpgradeRatingsStats());
}
Expand Down
Expand Up @@ -15,20 +15,22 @@
package com.liferay.portal.kernel.upgrade;

import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.dao.db.DB;
import com.liferay.portal.kernel.dao.db.DBInspector;
import com.liferay.portal.kernel.dao.db.DBManagerUtil;
import com.liferay.portal.kernel.dao.db.DBType;

import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.util.Collection;

/**
* @author Preston Crary
*/
public class UpgradeCTModel extends UpgradeProcess {

public UpgradeCTModel(Class<?> tableClass) {
_tableClass = tableClass;
public UpgradeCTModel(String tableName) {
_tableName = tableName;
}

@Override
Expand All @@ -38,7 +40,7 @@ protected void doUpgrade() throws Exception {
DBInspector dbInspector = new DBInspector(connection);

String tableName = dbInspector.normalizeName(
getTableName(_tableClass), databaseMetaData);
_tableName, databaseMetaData);

try (ResultSet rs = databaseMetaData.getColumns(
dbInspector.getCatalog(), dbInspector.getSchema(), tableName,
Expand Down Expand Up @@ -71,71 +73,53 @@ protected void doUpgrade() throws Exception {
"No primary key column found for " + tableName);
}

alter(
_tableClass, new AlterTableAddCTCollectionIdColumn(),
new DropPrimaryKeyAlterable(),
new AddPrimaryKeyAlterable(primaryKeyColumnName));
}

private final Class<?> _tableClass;
runSQL(
StringBundler.concat(
"alter table ", tableName,
" add ctCollectionId LONG default 0 not null"));

private static class AddPrimaryKeyAlterable
extends BasePrimaryKeyAlterable {
DB db = DBManagerUtil.getDB();

@Override
public String getSQL(String tableName) {
return StringBundler.concat(
"alter table ", tableName, " add primary key (",
_primaryKeyColumnName, ", ctCollectionId)");
}
if (db.getDBType() == DBType.SYBASE) {
String primaryKeyConstraintName = null;

private AddPrimaryKeyAlterable(String primaryKeyColumnName) {
_primaryKeyColumnName = primaryKeyColumnName;
}
try (PreparedStatement ps = connection.prepareStatement(
"sp_helpconstraint " + tableName);
ResultSet rs = ps.executeQuery()) {

private final String _primaryKeyColumnName;
while (rs.next()) {
String definition = rs.getString("definition");

}
if (definition.startsWith("PRIMARY KEY INDEX")) {
primaryKeyConstraintName = rs.getString("name");

private abstract static class BasePrimaryKeyAlterable implements Alterable {
break;
}
}
}

@Override
public boolean shouldAddIndex(Collection<String> columnNames) {
return false;
}
if (primaryKeyConstraintName == null) {
throw new UpgradeException(
"No primary key constraint found for " + tableName);
}

@Override
public boolean shouldDropIndex(Collection<String> columnNames) {
return false;
runSQL(
StringBundler.concat(
"sp_dropkey primary ", tableName, " ",
primaryKeyConstraintName));
}

}

private static class DropPrimaryKeyAlterable
extends BasePrimaryKeyAlterable {

@Override
public String getSQL(String tableName) {
return StringBundler.concat(
"alter table ", tableName, " drop primary key");
else {
runSQL(
StringBundler.concat(
"alter table ", tableName, " drop primary key"));
}

runSQL(
StringBundler.concat(
"alter table ", tableName, " add primary key (",
primaryKeyColumnName, ", ctCollectionId)"));
}

private class AlterTableAddCTCollectionIdColumn
extends AlterTableAddColumn {

@Override
public String getSQL(String tableName) {
return StringBundler.concat(
"alter table ", tableName,
" add ctCollectionId LONG default 0 not null");
}

private AlterTableAddCTCollectionIdColumn() {
super("ctCollectionId");
}

}
private final String _tableName;

}