-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef41f07
commit 8718354
Showing
5 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...java/com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
.../com/dotmarketing/startup/runonce/Task241013RemoveFullPathLcColumnFromIdentifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |