I managed to resolve this issue myself. I'd already written a console command that generates an empty Propel migration based on a custom template so that I could have data only migrations. I just made it so these generated migration classes extend a common BaseMigration
class elsewhere that contains the following methods: (plus a few more, but they aren't important here)
/**
* Force the version to the version of this migration. Useful for data migrations only using preUp()
*
* @param PropelMigrationManager $manager
*/
protected function updateToCurrentVersion(PropelMigrationManager $manager)
{
$manager->updateLatestMigrationTimestamp($this->getCurrentDatasource($manager), $this->getCurrentVersion());
}
/**
* This method should be called if implementing the down functionality using PDO in the preDown() method
*
* @param PropelMigrationManager $manager
*/
protected function updateToPreviousVersion(PropelMigrationManager $manager)
{
$datasource = $this->getCurrentDatasource($manager);
$currentVersion = $this->getCurrentVersion();
$timestamps = $manager->getAlreadyExecutedMigrationTimestamps();
rsort($timestamps);
$previousVersion = 0;
foreach ($timestamps as $timestamp) {
if ($timestamp < $currentVersion) {
$previousVersion = $timestamp;
break;
}
}
$manager->updateLatestMigrationTimestamp($datasource, $previousVersion);
}
/**
* @return int
*/
protected function getCurrentVersion()
{
$class = get_class($this);
return (int) substr($class, strpos($class, '_') + 1);
}
Now I just need to call $this->updateToCurrentVersion($manager)
from the end of preUp()
method (see note below) and $this->updateToPreviousVersion($manager)
at the end of the preDown() method to force the version numbers to update correctly.
NOTE: I noticed that the version wasn't updated when running the "up" migration command to only perform a single migration, whereas it would be updated when running "migrate"...even if this was applying the same migration! So it's safer to force the current version to be updated all the time.