Skip to content

Instantly share code, notes, and snippets.

@sevein
Last active December 10, 2015 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevein/4423982 to your computer and use it in GitHub Desktop.
Save sevein/4423982 to your computer and use it in GitHub Desktop.
data/fixtures/settings.yml | 7 +-
lib/task/migrate/arUpgradeSqlTask.class.php | 118 +++++++++++++++++++-
lib/task/migrate/arUpgrader130.class.php | 49 --------
.../migrate/migrations/arMigration0093.class.php | 50 +++++++++
.../migrate/migrations/arMigration0094.class.php | 49 ++++++++
5 files changed, 220 insertions(+), 53 deletions(-)
diff --git a/data/fixtures/settings.yml b/data/fixtures/settings.yml
index e292b26..924ac85 100644
--- a/data/fixtures/settings.yml
+++ b/data/fixtures/settings.yml
@@ -3,7 +3,12 @@ QubitSetting:
name: version
editable: 0
deleteable: 0
- value: 92
+ value: 93
+ milestone:
+ name: milestone
+ editable: 0
+ deleteable: 0
+ value: 1
QubitSetting_2:
name: upload_dir
editable: 0
diff --git a/lib/task/migrate/arUpgradeSqlTask.class.php b/lib/task/migrate/arUpgradeSqlTask.class.php
index e5f2d53..41d5592 100644
--- a/lib/task/migrate/arUpgradeSqlTask.class.php
+++ b/lib/task/migrate/arUpgradeSqlTask.class.php
@@ -146,8 +146,7 @@ EOF;
while ($class->up($version, $this->configuration, $options))
{
// Update version in database
- $sql = 'UPDATE setting_i18n SET value = ? WHERE id = (SELECT id FROM setting WHERE name = ?);';
- QubitPdo::modify($sql, array(++$version, 'version'));
+ $this->updateDatabaseVersion(++$version);
}
}
catch (Exception $e)
@@ -158,14 +157,127 @@ EOF;
}
}
+ // The database version in Release 1.3 was v92. After that, AtoM was forked
+ // in two different versions: 1.x and 2.x. Both will be maintained for now.
+ // This task selectively upgrades the user to the latest database version
+ // available for its milestone. In order to keep things simpler, db upgrades
+ // won't be able to target 1.x only. Upgrades targetting only 2.x won't be
+ // applied to 1.x users until they decide to migrate to AtoM 2.x.
+ // See #4494 for more details.
+
+ // Since some db upgrades are applied to both 1.x and 2.x releases, we need
+ // the previous milestone used. This value has been stored in the database
+ // since the fork happened.
+ $previousMilestone = $this->getPreviousMilestone();
+
+ // Upgrades post to Release 1.3 (v92) are located under
+ // task/migrate/migrations and named using the following format:
+ // "arMigration%04d.class.php" (the first one is arMigration0093.class.php)
+ foreach (sfFinder::type('file')
+ ->maxdepth(0)
+ ->sort_by_name()
+ ->name('arMigration*.class.php')
+ ->in(sfConfig::get('sf_lib_dir').'/task/migrate/migrations') as $filename)
+ {
+ // Initialize migration class
+ $className = preg_replace('/.*(arMigration\d+).*/', '$1', $filename);
+ $class = new $className;
+
+ // The following piece of code decides whether the different database
+ // upgrades under task/migrate/migrations should be applied or not:
+
+ // Upgrading to 1.x. The safest way I've found to get this value is
+ // looking at the qubitConfiguration class, which is part of the codebase.
+ if (version_compare(qubitConfiguration::VERSION, '2.0', '<'))
+ {
+ // Ignore upgrades that have already been applied.
+ // Also, ignore upgrades that target only 2.x.
+ if ($class::VERSION <= $version || 1 < $class::MIN_MILESTONE)
+ {
+ continue;
+ }
+ }
+ // Upgrading from 1.x to 2.x
+ else if (1 == $previousMilestone)
+ {
+ // Ignore previous 1.x/2.x because they have been already applied
+ if ($class::VERSION < $version && 1 == $class::MIN_MILESTONE)
+ {
+ continue;
+ }
+
+ // Ignore the current version for the same reason
+ if ($class::VERSION == $version)
+ {
+ continue;
+ }
+ }
+ // Upgrading from 2.x to 2.x
+ else
+ {
+ // Ignore previous upgrades
+ if ($class::VERSION <= $version)
+ {
+ continue;
+ }
+ }
+
+ // Run migration and bump database version
+ $class::up();
+ $this->updateDatabaseVersion(++$version);
+ }
+
if ($this->initialVersion == $version)
{
$this->logSection('upgrade-sql', sprintf('Already at latest version (%s), no upgrades done', $version));
}
else
{
- $this->logSection('upgrade-sql', sprintf('Successfully upgraded to Release %s v%s', $class::MILESTONE, $version));
+ $this->logSection('upgrade-sql', sprintf('Successfully upgraded to Release %s v%s', qubitConfiguration::VERSION, $version));
+ }
+ }
+
+ /**
+ * Figure out what's the last milestone used
+ *
+ * @return int Previous milestone (e.g. 1, 2)
+ */
+ protected function getPreviousMilestone()
+ {
+ // There is no doubt that the user is running 1.x if the initial database
+ // version was 92 or lower (before the fork happened)
+ if ($this->initialVersion <= 92)
+ {
+ $previousMilestone = 1;
+ }
+ // Otherwise, we'll look for the milestone in the database
+ else
+ {
+ $sql = 'SELECT value
+ FROM setting JOIN setting_i18n ON setting.id = setting_i18n.id
+ WHERE name = "milestone";';
+
+ $previousMilestone = QubitPdo::fetchColumn($sql);
}
+
+ return $previousMilestone;
+ }
+
+ /**
+ * Figure out what's the last milestone used
+ *
+ * @return int Previous milestone, e.g. 1 for 1.x or 2 for 2.x
+ */
+ protected function updateDatabaseVersion($version)
+ {
+ $sql = 'UPDATE setting_i18n SET value = ? WHERE id = (SELECT id FROM setting WHERE name = ?);';
+ QubitPdo::modify($sql, array($version, 'version'));
+ }
+
+ protected function updateMilestone($milestone)
+ {
+ $sql = 'UPDATE setting_i18n SET value = ? WHERE id = (SELECT id FROM setting WHERE name = ?);';
+ QubitPdo::modify($sql, array($milestone, 'milestone'));
}
protected function parseDsn($dsn)
diff --git a/lib/task/migrate/arUpgrader130.class.php b/lib/task/migrate/arUpgrader130.class.php
deleted file mode 100644
index d476029..0000000
--- a/lib/task/migrate/arUpgrader130.class.php
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-
-/*
- * This file is part of the Access to Memory (AtoM) software.
- *
- * Access to Memory (AtoM) is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Access to Memory (AtoM) is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * Upgrade Qubit data from Release 1.3
- *
- * @package AccesstoMemory
- * @subpackage migration
- */
-class arUpgrader130
-{
- const
- MILESTONE = '1.3',
- INIT_VERSION = 92;
-
- public function up($version, $configuration, $options)
- {
- if ($options['verbose'])
- {
- echo "up($version)\n";
- }
-
- switch ($version)
- {
- // Return false if no upgrade available
- default:
-
- return false;
- }
-
- return true;
- }
-}
diff --git a/lib/task/migrate/migrations/arMigration0093.class.php b/lib/task/migrate/migrations/arMigration0093.class.php
new file mode 100644
index 0000000..f244880
--- /dev/null
+++ b/lib/task/migrate/migrations/arMigration0093.class.php
@@ -0,0 +1,50 @@
+<?php
+
+/*
+ * This file is part of the Access to Memory (AtoM) software.
+ *
+ * Access to Memory (AtoM) is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Access to Memory (AtoM) is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Add the milestone setting to the database. This is the first database upgrade
+ * after the fork happened.
+ *
+ * @package AccesstoMemory
+ * @subpackage migration
+ */
+class arMigration0093
+{
+ const
+ VERSION = 93, // The new database version
+ MIN_MILESTONE = 1; // The minimum milestone required
+
+ /**
+ * Upgrade the database schema
+ *
+ * @return bool True if the upgrade succeeded, False otherwise
+ */
+ public function up()
+ {
+ // Get current codebase milestone
+ $substrings = preg_split('/\./', qubitConfiguration::VERSION);
+ $milestone = array_shift($substrings);
+
+ // Store it
+ $setting = new QubitSetting;
+ $setting->name = 'milestone';
+ $setting->setValue($milestone, array('sourceCulture' => true));
+ $setting->save();
+ }
+}
diff --git a/lib/task/migrate/migrations/arMigration0094.class.php b/lib/task/migrate/migrations/arMigration0094.class.php
new file mode 100644
index 0000000..a13dc3d
--- /dev/null
+++ b/lib/task/migrate/migrations/arMigration0094.class.php
@@ -0,0 +1,49 @@
+<?php
+
+/*
+ * This file is part of the Access to Memory (AtoM) software.
+ *
+ * Access to Memory (AtoM) is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Access to Memory (AtoM) is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Add source standard column
+ *
+ * @package AccesstoMemory
+ * @subpackage migration
+ */
+class arMigration0094
+{
+ const
+ VERSION = 93, // The new database version
+ MIN_MILESTONE = 1; // The minimum milestone required
+
+ /**
+ * Upgrade the database schema
+ *
+ * @return bool True if the upgrade succeeded, False otherwise
+ */
+ public function up()
+ {
+ // Get current codebase milestone
+ $substrings = preg_split('/\./', qubitConfiguration::VERSION);
+ $milestone = array_shift($substrings);
+
+ // Store it
+ $setting = new QubitSetting;
+ $setting->name = 'milestone';
+ $setting->setValue($milestone, array('sourceCulture' => true));
+ $setting->save();
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment