From 33cbe3815130dd9493977ea9e20de89e6be0a207 Mon Sep 17 00:00:00 2001 From: erickgonzalez Date: Wed, 23 Oct 2024 15:41:22 -0600 Subject: [PATCH] #30243 include in 23.10.24 LTS --- dotCMS/hotfix_tracking.md | 1 + .../java/com/dotcms/MainSuite2b.java | 3 +- ...oveFullPathLcColumnFromIdentifierTest.java | 86 +++++++++++++++++++ ...3RemoveFullPathLcColumnFromIdentifier.java | 60 +++++++++++++ .../dotmarketing/util/TaskLocatorUtil.java | 1 + 5 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 dotCMS/src/integration-test/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifierTest.java create mode 100644 dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifier.java diff --git a/dotCMS/hotfix_tracking.md b/dotCMS/hotfix_tracking.md index 47e03272630c..bc20e86fb614 100644 --- a/dotCMS/hotfix_tracking.md +++ b/dotCMS/hotfix_tracking.md @@ -167,3 +167,4 @@ This maintenance release includes the following code fixes: 160. https://github.com/dotCMS/core/issues/29535 : Investigate and Resolve Session Already Invalidated Issue for PATCH API Call with Bearer Token #29535 161. https://github.com/dotCMS/core/issues/29938 : Many to One Relationship Not Maintained When Copied to New Host #29938 162. https://github.com/dotCMS/core/issues/30156 : Create Notifications for LTS already EOL or upcoming EOL #30156 +163. https://github.com/dotCMS/core/issues/30243 : Intermittent 404 issues for customers who came from another DB engine #30243 diff --git a/dotCMS/src/integration-test/java/com/dotcms/MainSuite2b.java b/dotCMS/src/integration-test/java/com/dotcms/MainSuite2b.java index 4f159ec314d0..2e20645da38c 100644 --- a/dotCMS/src/integration-test/java/com/dotcms/MainSuite2b.java +++ b/dotCMS/src/integration-test/java/com/dotcms/MainSuite2b.java @@ -277,7 +277,8 @@ DropOldContentVersionsJobTest.class, Task231109AddPublishDateToContentletVersionInfoTest.class, Task240513UpdateContentTypesSystemFieldTest.class, - EmailActionletTest.class + EmailActionletTest.class, + Task241013RemoveFullPathLcColumnFromIdentifierTest.class }) public class MainSuite2b { diff --git a/dotCMS/src/integration-test/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifierTest.java b/dotCMS/src/integration-test/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifierTest.java new file mode 100644 index 000000000000..e495a747e6b4 --- /dev/null +++ b/dotCMS/src/integration-test/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifierTest.java @@ -0,0 +1,86 @@ +package com.dotmarketing.startup.runonce; + +import com.dotcms.util.IntegrationTestInitService; +import com.dotmarketing.common.db.DotConnect; +import com.dotmarketing.exception.DotDataException; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.sql.SQLException; + +public class Task241013RemoveFullPathLcColumnFromIdentifierTest { + + private static final String ADD_FULL_PATH_LC_COLUMN = "ALTER TABLE identifier ADD COLUMN full_path_lc varchar(255)"; + private static final String DROP_FUNCTION = "DROP FUNCTION IF EXISTS full_path_lc(identifier)"; + private static final String CHECK_INDEX_QUERY = + "SELECT 1 FROM pg_indexes WHERE tablename = ? AND indexname = ?"; + private static final String CHECK_FUNCTION_QUERY = + "SELECT 1 FROM information_schema.routines WHERE routine_name = ? AND routine_schema = ?"; + + @BeforeClass + public static void prepare() throws Exception { + IntegrationTestInitService.getInstance().init(); + } + + + @Test + public void test_upgradeTask_indexNorFunctionExists_success() throws DotDataException, SQLException { + final DotConnect dc = new DotConnect(); + //Add the column to the table + dc.executeStatement(ADD_FULL_PATH_LC_COLUMN); + //Remove the index + dc.executeStatement(Task241013RemoveFullPathLcColumnFromIdentifier.DROP_INDEX); + //Remove the function + dc.executeStatement(DROP_FUNCTION); + + //Run UT + final Task241013RemoveFullPathLcColumnFromIdentifier upgradeTask = new Task241013RemoveFullPathLcColumnFromIdentifier(); + Assert.assertTrue("Column Not Exists and it should exists",upgradeTask.forceRun()); + if(upgradeTask.forceRun()) { + upgradeTask.executeUpgrade(); + } + + //Check that the column was removed + Assert.assertFalse("Column Exists and it shouldn't exists",upgradeTask.forceRun()); + //Check that the index was created + dc.setSQL(CHECK_INDEX_QUERY).addParam("identifier").addParam("idx_ident_uniq_asset_name"); + Assert.assertFalse(dc.loadResults().isEmpty()); + //Check that the function was created + dc.setSQL(CHECK_FUNCTION_QUERY).addParam("full_path_lc").addParam("public"); + Assert.assertFalse(dc.loadResults().isEmpty()); + } + + @Test + public void test_upgradeTask_indexDoesNotExists_success() throws DotDataException, SQLException { + final DotConnect dc = new DotConnect(); + //Add the column to the table + dc.executeStatement(ADD_FULL_PATH_LC_COLUMN); + //Remove the index + dc.executeStatement(Task241013RemoveFullPathLcColumnFromIdentifier.DROP_INDEX); + + //Run UT + final Task241013RemoveFullPathLcColumnFromIdentifier upgradeTask = new Task241013RemoveFullPathLcColumnFromIdentifier(); + Assert.assertTrue("Column Not Exists and it should exists",upgradeTask.forceRun()); + if(upgradeTask.forceRun()) { + upgradeTask.executeUpgrade(); + } + + //Check that the column was removed + Assert.assertFalse("Column Exists and it shouldn't exists",upgradeTask.forceRun()); + //Check that the index was created + dc.setSQL(CHECK_INDEX_QUERY).addParam("identifier").addParam("idx_ident_uniq_asset_name"); + Assert.assertFalse(dc.loadResults().isEmpty()); + //Check that the function was created + dc.setSQL(CHECK_FUNCTION_QUERY).addParam("full_path_lc").addParam("public"); + Assert.assertFalse(dc.loadResults().isEmpty()); + } + + @Test + public void test_upgradeTask_columnNotPresent_success() throws DotDataException, SQLException { + //Run UT + final Task241013RemoveFullPathLcColumnFromIdentifier upgradeTask = new Task241013RemoveFullPathLcColumnFromIdentifier(); + Assert.assertFalse("Column Exists and it shouldn't exists",upgradeTask.forceRun()); + } + +} diff --git a/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifier.java b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifier.java new file mode 100644 index 000000000000..137e1db9c50e --- /dev/null +++ b/dotCMS/src/main/java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifier.java @@ -0,0 +1,60 @@ +package com.dotmarketing.startup.runonce; + +import com.dotmarketing.common.db.DotConnect; +import com.dotmarketing.common.db.DotDatabaseMetaData; +import com.dotmarketing.exception.DotDataException; +import com.dotmarketing.exception.DotRuntimeException; +import com.dotmarketing.startup.StartupTask; +import com.dotmarketing.util.Logger; + +import java.sql.SQLException; + +/** + * This task removes the full_path_lc column from the identifier table and creates the full_path_lc function + * if it doesn't exist. It also creates an index on the identifier table. + */ +public class Task241013RemoveFullPathLcColumnFromIdentifier implements StartupTask { + + private static final String DROP_FULL_PATH_LC_COLUMN = "ALTER TABLE identifier DROP COLUMN full_path_lc;"; + + private static final String CREATE_FULL_PATH_LC_FUNCTION = + "CREATE OR REPLACE FUNCTION full_path_lc(identifier) RETURNS text\n" + + " AS ' SELECT CASE WHEN $1.parent_path = ''/System folder'' then ''/'' else LOWER($1.parent_path || $1.asset_name) end; '\n" + + "LANGUAGE SQL;\n"; + + protected static final String DROP_INDEX = "DROP INDEX IF EXISTS idx_ident_uniq_asset_name CASCADE"; + + private static final String CREATE_INDEX = "CREATE UNIQUE INDEX idx_ident_uniq_asset_name on identifier (full_path_lc(identifier),host_inode)"; + + /** + * Checks if the full_path_lc column exists in the identifier table + * @return true if the column does exist, false otherwise + */ + @Override + public boolean forceRun() { + try { + return new DotDatabaseMetaData().hasColumn("identifier", "full_path_lc") ; + } catch (SQLException e) { + Logger.error(this, e.getMessage(),e); + return false; + } + } + + @Override + public void executeUpgrade() throws DotDataException, DotRuntimeException { + final DotConnect dc = new DotConnect(); + try { + //Removes full_path_lc column from identifier table + dc.executeStatement(DROP_FULL_PATH_LC_COLUMN); + + //Creates full_path_lc function if it doesn't exist + dc.executeStatement(CREATE_FULL_PATH_LC_FUNCTION); + + //Creates index on identifier table + dc.executeStatement(DROP_INDEX); + dc.executeStatement(CREATE_INDEX); + } catch (SQLException e) { + throw new DotDataException(e.getMessage(), e); + } + } +} diff --git a/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java b/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java index bfa73c356b72..b830833c7a6a 100644 --- a/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java +++ b/dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java @@ -383,6 +383,7 @@ public static List> getBackportedUpgradeTaskClasses() { ret.add(Task240102AlterVarcharLengthOfRelationType.class); ret.add(Task240131UpdateLanguageVariableContentType.class); ret.add(Task240513UpdateContentTypesSystemField.class); + ret.add(Task241013RemoveFullPathLcColumnFromIdentifier.class); return ret.stream().sorted(classNameComparator).collect(Collectors.toList()); }