Skip to content

Commit

Permalink
#30243 include in 24.04.24 LTS
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Oct 23, 2024
1 parent ef41f07 commit 8718354
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
1 change: 1 addition & 0 deletions dotCMS/hotfix_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ This maintenance release includes the following code fixes:
74. https://github.com/dotCMS/core/issues/29535 : Investigate and Resolve Session Already Invalidated Issue for PATCH API Call with Bearer Token #29535
75. https://github.com/dotCMS/core/issues/29938 : Many to One Relationship Not Maintained When Copied to New Host #29938
76. https://github.com/dotCMS/core/issues/30156 : Create Notifications for LTS already EOL or upcoming EOL #30156
77. https://github.com/dotCMS/core/issues/30243 : Intermittent 404 issues for customers who came from another DB engine #30243
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ public static List<Class<?>> getBackportedUpgradeTaskClasses() {
final List<Class<?>> ret = new ArrayList<>();
ret.add(Task240513UpdateContentTypesSystemField.class);
ret.add(Task240530AddDotAIPortletToLayout.class);
ret.add(Task241013RemoveFullPathLcColumnFromIdentifier.class);
return ret.stream().sorted(classNameComparator).collect(Collectors.toList());
}

Expand Down
3 changes: 2 additions & 1 deletion dotcms-integration/src/test/java/com/dotcms/MainSuite2b.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@
SearchToolTest.class,
EmbeddingsToolTest.class,
CompletionsToolTest.class,
EmbeddingContentListenerTest.class
EmbeddingContentListenerTest.class,
Task241013RemoveFullPathLcColumnFromIdentifierTest.class
})

public class MainSuite2b {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 8718354

Please sign in to comment.