com.liferay.commerce.internal.upgrade.v2_1_0.CommerceSubscriptionEntryUpgradeProcess Maven / Gradle / Ivy
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.commerce.internal.upgrade.v2_1_0;
import com.liferay.commerce.model.impl.CommerceSubscriptionEntryModelImpl;
import com.liferay.commerce.product.model.CPDefinition;
import com.liferay.commerce.product.model.CPInstance;
import com.liferay.commerce.product.service.CPDefinitionLocalService;
import com.liferay.commerce.product.service.CPInstanceLocalService;
import com.liferay.portal.kernel.dao.db.DB;
import com.liferay.portal.kernel.dao.db.DBManagerUtil;
import com.liferay.portal.kernel.dao.db.IndexMetadata;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.upgrade.UpgradeProcess;
import com.liferay.portal.kernel.upgrade.UpgradeProcessFactory;
import com.liferay.portal.kernel.upgrade.UpgradeStep;
import com.liferay.portal.kernel.util.ObjectValuePair;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Objects;
/**
* @author Ethan Bustad
*/
public class CommerceSubscriptionEntryUpgradeProcess extends UpgradeProcess {
public CommerceSubscriptionEntryUpgradeProcess(
CPDefinitionLocalService cpDefinitionLocalService,
CPInstanceLocalService cpInstanceLocalService) {
_cpDefinitionLocalService = cpDefinitionLocalService;
_cpInstanceLocalService = cpInstanceLocalService;
}
@Override
protected void doUpgrade() throws Exception {
_addIndexes(CommerceSubscriptionEntryModelImpl.TABLE_NAME);
try (PreparedStatement preparedStatement = connection.prepareStatement(
"update CommerceSubscriptionEntry set CProductId = ?," +
"CPInstanceUUID = ? where CPInstanceId = ?");
Statement s = connection.createStatement();
ResultSet resultSet = s.executeQuery(
"select distinct CPInstanceId from " +
"CommerceSubscriptionEntry")) {
while (resultSet.next()) {
long cpInstanceId = resultSet.getLong("CPInstanceId");
CPInstance cpInstance = _cpInstanceLocalService.getCPInstance(
cpInstanceId);
CPDefinition cpDefinition =
_cpDefinitionLocalService.getCPDefinition(
cpInstance.getCPDefinitionId());
preparedStatement.setLong(1, cpDefinition.getCProductId());
preparedStatement.setString(2, cpInstance.getCPInstanceUuid());
preparedStatement.setLong(3, cpInstanceId);
preparedStatement.execute();
}
}
}
@Override
protected UpgradeStep[] getPostUpgradeSteps() {
return new UpgradeStep[] {
UpgradeProcessFactory.dropColumns(
"CommerceSubscriptionEntry", "CPInstanceId")
};
}
@Override
protected UpgradeStep[] getPreUpgradeSteps() {
return new UpgradeStep[] {
UpgradeProcessFactory.addColumns(
"CommerceSubscriptionEntry", "CPInstanceUUID VARCHAR(75)",
"CProductId LONG")
};
}
private void _addIndexes(String tableName) throws Exception {
Class> clazz = getClass();
List> indexesSQL = getIndexesSQL(
clazz.getClassLoader(), tableName);
for (ObjectValuePair indexSQL : indexesSQL) {
IndexMetadata indexMetadata = indexSQL.getValue();
if (_log.isInfoEnabled()) {
_log.info(
String.format(
"Adding index %s to table %s",
indexMetadata.getIndexName(), tableName));
}
if (!_tableHasIndex(tableName, indexMetadata.getIndexName())) {
runSQL(indexMetadata.getCreateSQL(null));
}
else if (_log.isInfoEnabled()) {
_log.info(
String.format(
"Index %s already exists on table %s",
indexMetadata.getIndexName(), tableName));
}
}
}
private boolean _tableHasIndex(String tableName, String indexName)
throws Exception {
DB db = DBManagerUtil.getDB();
try (ResultSet resultSet = db.getIndexResultSet(
connection, tableName, false)) {
while (resultSet.next()) {
String curIndexName = resultSet.getString("index_name");
if (Objects.equals(indexName, curIndexName)) {
return true;
}
}
}
return false;
}
private static final Log _log = LogFactoryUtil.getLog(
CommerceSubscriptionEntryUpgradeProcess.class);
private final CPDefinitionLocalService _cpDefinitionLocalService;
private final CPInstanceLocalService _cpInstanceLocalService;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy