Skip to content

A number of different changes that were needed for our use of AutoPatch #34

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ target
*.iws
.idea
*.externalToolBuilders
*.bak
*.tmp
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,12 @@
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>[2.0,)</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>[3.0,)</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,16 @@ public final LinkedHashMap getMigrationTasksWithLaunchers() throws MigrationExce
if (log.isDebugEnabled())
{
Iterator launchers = subLauncher.getContexts().keySet().iterator();
String systemName = ((JdbcMigrationContext) launchers.next()).getSystemName();
log.debug("\tMigration+Launcher binder found subtask " + task.getName()
if (!launchers.hasNext()) {
log.debug("\tMigration+Launcher binder found subtask " + task.getName()
+ " with no contexts defined");

} else {
JdbcMigrationContext next = (JdbcMigrationContext) launchers.next();
String systemName = next.getSystemName();
log.debug("\tMigration+Launcher binder found subtask " + task.getName()
+ " for launcher context " + systemName);
}
}

// store the task, related to its launcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

package com.tacitknowledge.util.migration;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -35,23 +35,24 @@ public class MigrationRunnerFactory
public static MigrationRunnerStrategy getMigrationRunnerStrategy(String strategy)
{

log.info("Strategy received '" + strategy + "'");

if (StringUtils.isBlank(strategy))
{
log.info("No migration strategy specified. Using default: '" + DEFAULT_MIGRATION_STRATEGY + "'");
return new OrderedMigrationRunnerStrategy();

}

try
{
log.info("Using specified migration strategy: '" + strategy + "'");
Class c = Class.forName(strategy.trim());
MigrationRunnerStrategy runnerStrategy = (MigrationRunnerStrategy) c.newInstance();
return runnerStrategy;
}
catch (Exception e)
{
throw new IllegalArgumentException("Strategy selected " + strategy + " cannot be instantiated ", e);
throw new IllegalArgumentException("Specified strategy (" + strategy + ") cannot be instantiated ", e);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.tacitknowledge.util.migration;


import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -38,6 +38,7 @@ public boolean shouldMigrationRun(int migrationLevel, PatchInfoStore patchInfoSt
throw new IllegalArgumentException("Patch Info Store should not be null");
}

// This is the crux of this migration strategy. As long as a patch has not yet been executed, it can be.
return !patchInfoStore.isPatchApplied(migrationLevel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class OrderedMigrationRunnerStrategy implements MigrationRunnerStrategy
{
public boolean shouldMigrationRun(int migrationLevel, PatchInfoStore patchInfoStore) throws MigrationException
{
// This is the crux of this migration strategy. Only patches with a patch level higher than the highest one executed so far are eligible to be excuted.
return migrationLevel > patchInfoStore.getPatchLevel();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class AutoPatchSupport
*/
public AutoPatchSupport(String systemName) throws MigrationException
{
//TODO should be injected
this(new JdbcMigrationLauncherFactoryLoader().createFactory(), systemName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Defines a type of database (e.g. <code>oracle</code> or <code>postgres</code>. This
* is used to help define the SQL that is used to update the patch table and as a hint
Expand Down Expand Up @@ -51,6 +54,8 @@
*/
public class DatabaseType
{
private static Log log = LogFactory.getLog(DatabaseType.class);

/**
* The SQL statements and properties that are unique to this database flavor.
*/
Expand Down Expand Up @@ -89,6 +94,16 @@ public DatabaseType(String databaseType)
// this is okay, in this class, migration.properties is only used to override SQL
}
this.databaseType = databaseType;
log.debug("AutoPatch instantiated and loaded with properties per " + databasePropertiesFilename);
if (isMultipleStatementsSupported())
{
log.debug("AutoPatch thinks that Multiple SQL Statements can be executed at once.");
}
else
{
log.debug("AutoPatch thinks that Multiple SQL Statements must be executed separately.");
}

}

protected Properties loadProperties(String propertiesFilename, ClassLoader loader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,13 @@ public void rollbackSuccessful(RollbackableMigrationTask task, int rollbackLevel

public void setMigrationStrategy(String migrationStrategy)
{
log.debug("Setting migration strategy to: "+migrationStrategy+ " (was: "+this.migrationStrategy+")");
this.migrationStrategy = migrationStrategy;
}

public String getMigrationStrategy()
{
log.debug("Get-accessor says migration strategy is: "+migrationStrategy);
return migrationStrategy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.tacitknowledge.util.migration.MigrationListener;
import com.tacitknowledge.util.migration.jdbc.util.ConfigurationUtil;
import com.tacitknowledge.util.migration.jdbc.util.NonPooledDataSource;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -123,6 +123,7 @@ public JdbcMigrationLauncher createMigrationLauncher(String systemName)
public JdbcMigrationLauncher createMigrationLauncher(ServletContextEvent sce)
throws MigrationException
{
log.info("Creating JdbcMigrationLauncher for web-application.");
JdbcMigrationLauncher launcher = getJdbcMigrationLauncher();
configureFromServletContext(launcher, sce);
return launcher;
Expand All @@ -139,6 +140,14 @@ public JdbcMigrationLauncher createMigrationLauncher(ServletContextEvent sce)
private void configureFromServletContext(JdbcMigrationLauncher launcher,
ServletContextEvent sce) throws MigrationException
{
log.debug("Configuring launcher from Servlet Context (web.xml)");

String migrationStrategy = sce.getServletContext().getInitParameter("migration.strategy");
log.debug("Servlet Container says migration.strategy = "+migrationStrategy);
if (!StringUtils.isBlank(migrationStrategy)) {
launcher.setMigrationStrategy(migrationStrategy);
}

String readOnly = sce.getServletContext().getInitParameter("migration.readonly");
launcher.setReadOnly(false);
if ("true".equals(readOnly))
Expand All @@ -155,6 +164,7 @@ private void configureFromServletContext(JdbcMigrationLauncher launcher,
}

String patchPath = ConfigurationUtil.getRequiredParam("migration.patchpath", sce, this);
log.debug("migration.patchpath (required) = "+patchPath);
launcher.setPatchPath(patchPath);

String postPatchPath = sce.getServletContext().getInitParameter("migration.postpatchpath");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.tacitknowledge.util.migration.MigrationTaskSupport;
import com.tacitknowledge.util.migration.jdbc.util.SqlUtil;
import com.tacitknowledge.util.migration.jdbc.util.SybaseUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.tacitknowledge.util.migration.MigrationException;
import com.tacitknowledge.util.migration.jdbc.util.ConfigurationUtil;
import com.tacitknowledge.util.migration.jdbc.util.MigrationUtil;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.picocontainer.PicoContainer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void testDoRollbacksActionMockingAsIfOrderedStrategyWereUsed() throws Mig
boolean forceRollback=false;
int rollbacksApplied = distributedMigrationProcess.doRollbacks(patchInfoStoreMock,rollbackLevels,migrationContextMock,forceRollback);

assertEquals("Two rollbacks should be applied", 3, rollbacksApplied);
assertEquals("Rollbacks should have been applied", 3, rollbacksApplied);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tacitknowledge.util.migration;

import junit.framework.TestCase;

public class MigrationRunnerFactoryTest extends TestCase
{

protected void setUp() throws Exception
{
super.setUp();
}

public void testGetMigrationRunnerStrategy_Default()
{
MigrationRunnerStrategy strategy = MigrationRunnerFactory.getMigrationRunnerStrategy("");
assertEquals("com.tacitknowledge.util.migration.OrderedMigrationRunnerStrategy",strategy.getClass().getName());
}

public void testGetMigrationRunnerStrategy_MissingPatchMigrationRunnerStrategy()
{
MigrationRunnerStrategy strategy = MigrationRunnerFactory.getMigrationRunnerStrategy("com.tacitknowledge.util.migration.MissingPatchMigrationRunnerStrategy");
assertEquals("com.tacitknowledge.util.migration.MissingPatchMigrationRunnerStrategy",strategy.getClass().getName());
}

public void testGetMigrationRunnerStrategy_NoSuchStrategy()
{
try
{
MigrationRunnerFactory.getMigrationRunnerStrategy("com.tacitknowledge.util.migration.NoSuchStrategy");
fail("MigrationRunnerFactory.getMigrationRunnerStrategy() Should have raised an IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
// expected
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void testDistributedReadOnlyMode() throws Exception
log.debug("got exception: " + me.getMessage());
}

currentPatchLevel = 8;
currentPatchLevel = 13;
// need to mock the patch info stores to return the expected patch levels
setReportedPatchLevel(controlledSystems.values(), currentPatchLevel);

Expand Down
Loading