Author: sevein
Date: Thu Jul 5 17:17:40 2012
New Revision: 11866
Log:
This is my first try to allow adding new constants (term or taxonomies) in the
new SQL upgrade system (see issue 2270). If a new term/taxonomy exists when a
new constant is trying to be placed in its position, it is bumped to the top
updating its ID value and its FK columns. It may not be the best solution but
it works, it could be quickly implemented and it does not require much
maintenance.
Modified:
trunk/lib/task/migrate/QubitMigrate.class.php
trunk/lib/task/migrate/arUpgradeSqlTask.class.php
trunk/lib/task/migrate/arUpgrader120.class.php
Modified: trunk/lib/task/migrate/QubitMigrate.class.php
==============================================================================
--- trunk/lib/task/migrate/QubitMigrate.class.php Thu Jul 5 16:54:51
2012 (r11865)
+++ trunk/lib/task/migrate/QubitMigrate.class.php Thu Jul 5 17:17:40
2012 (r11866)
@@ -380,4 +380,156 @@
return $objectList;
}
+
+ public static function findForeignKeys(array $tables, $configuration)
+ {
+ $finder = sfFinder::type('file')->name('*schema.yml')->prune('doctrine');
+ $dirs = array_merge(array(sfConfig::get('sf_config_dir')),
$configuration->getPluginSubPaths('/config'));
+ $schemas = $finder->in($dirs);
+ if (!count($schemas))
+ {
+ throw new sfCommandException('You must create a schema.yml file.');
+ }
+
+ $dbSchema = new sfPropelDatabaseSchema();
+
+ foreach ($schemas as $schema)
+ {
+ $schemaArray = sfYaml::load($schema);
+
+ if (!is_array($schemaArray))
+ {
+ continue; // No defined schema here, skipping
+ }
+
+ if (!isset($schemaArray['classes']))
+ {
+ // Old schema syntax: we convert it
+ $schemaArray = $dbSchema->convertOldToNewYaml($schemaArray);
+ }
+
+ foreach ($schemaArray['classes'] as $classKey => $class)
+ {
+ foreach ($class['columns'] as $columnKey => $column)
+ {
+ if ($columnKey == 'id')
+ {
+ continue;
+ }
+
+ if ($column['type'] != 'integer')
+ {
+ continue;
+ }
+
+ if (!in_array($column['foreignTable'], $tables))
+ {
+ continue;
+ }
+
+ $className = 'Qubit'.$classKey;
+
+ $columns[] = array(
+ 'table' => $className::TABLE_NAME,
+ 'column' => $columnKey);
+ }
+ }
+ }
+
+ foreach ($tables as $item)
+ {
+ switch ($item)
+ {
+ case 'object':
+ $columns[] = array('table' => 'object', 'column' => 'id');
+
+ break;
+
+ case 'term':
+ case 'taxonomy':
+ $columns[] = array('table' => $item, 'column' => 'id');
+ $columns[] = array('table' => $item.'_i18n', 'column' => 'id');
+ }
+ }
+
+ return $columns;
+ }
+
+ public static function addTermConstant(QubitTerm $object, $configuration)
+ {
+ $connection = Propel::getConnection();
+
+ $connection->beginTransaction();
+
+ try
+ {
+ // Check if it already exists
+ if (null !== QubitTerm::getById($object->id))
+ {
+ $connection->exec('SET FOREIGN_KEY_CHECKS = 0');
+
+ // Get new autonumeric
+ $last = QubitPdo::fetchOne('SELECT (MAX(id) + 1) AS last FROM
object')->last;
+
+ $foreignKeys = self::findForeignKeys(array(QubitObject::TABLE_NAME,
QubitTerm::TABLE_NAME), $configuration);
+
+ foreach ($foreignKeys as $item)
+ {
+ QubitPdo::modify(
+ "UPDATE $item[table] SET $item[column] = ? WHERE $item[column] =
?", array(
+ $last,
+ $object->id));
+ }
+
+ $connection->exec('SET FOREIGN_KEY_CHECKS = 1');
+ }
+
+ $object->save();
+ }
+ catch (Exception $e)
+ {
+ $connection->rollback();
+ }
+
+ $connection->commit();
+ }
+
+ public static function addTaxonomyConstant(QubitTaxonomy $object,
$configuration)
+ {
+ $connection = Propel::getConnection();
+
+ $connection->beginTransaction();
+
+ try
+ {
+ // Check if it already exists
+ if (null !== QubitTaxonomy::getById($object->id))
+ {
+ $connection->exec('SET FOREIGN_KEY_CHECKS = 0');
+
+ // Get new autonumeric
+ $last = QubitPdo::fetchOne('SELECT (MAX(id) + 1) AS last FROM
object')->last;
+
+ $foreignKeys = self::findForeignKeys(array(QubitObject::TABLE_NAME,
QubitTaxonomy::TABLE_NAME), $configuration);
+
+ foreach ($foreignKeys as $item)
+ {
+ QubitPdo::modify(
+ "UPDATE $item[table] SET $item[column] = ? WHERE $item[column] =
?", array(
+ $last,
+ $object->id));
+ }
+
+ $connection->exec('SET FOREIGN_KEY_CHECKS = 1');
+ }
+
+ $object->save();
+ }
+ catch (Exception $e)
+ {
+ $connection->rollback();
+ }
+
+ $connection->commit();
+ }
}
Modified: trunk/lib/task/migrate/arUpgradeSqlTask.class.php
==============================================================================
--- trunk/lib/task/migrate/arUpgradeSqlTask.class.php Thu Jul 5 16:54:51
2012 (r11865)
+++ trunk/lib/task/migrate/arUpgradeSqlTask.class.php Thu Jul 5 17:17:40
2012 (r11866)
@@ -129,11 +129,13 @@
if ($class::INIT_VERSION <= $version)
{
$this->logSection('upgrade-sql', sprintf('Upgrading from Release %s',
$class::MILESTONE));
- while ($class->up($version))
+ while ($class->up($version, $this->configuration))
{
+ $this->logSection('upgrade-sql', sprintf('Upgrading to %s',
++$version));
+
// Update version in database
- $sql = 'UPDATE setting_i18n SET value=? WHERE id = (SELECT id FROM
setting WHERE name=?);';
- QubitPdo::modify($sql, array(++$version, 'version'));
+ $sql = 'UPDATE setting_i18n SET value = ? WHERE id = (SELECT id FROM
setting WHERE name = ?);';
+ QubitPdo::modify($sql, array($version, 'version'));
}
}
}
Modified: trunk/lib/task/migrate/arUpgrader120.class.php
==============================================================================
--- trunk/lib/task/migrate/arUpgrader120.class.php Thu Jul 5 16:54:51
2012 (r11865)
+++ trunk/lib/task/migrate/arUpgrader120.class.php Thu Jul 5 17:17:40
2012 (r11866)
@@ -30,9 +30,9 @@
MILESTONE = '1.2',
INIT_VERSION = 75;
- public function up($version)
+ public function up($version, $configuration)
{
- switch($version)
+ switch ($version)
{
// Add setting for job_scheduling
case 75:
@@ -252,18 +252,26 @@
break;
- // TODO Add refId column to terms table???
+ // TODO Add accrual constant to term table
case 84:
- // $sql = sprintf('ALTER TABLE %s ADD COLUMN ref_id varchar(255);',
QubitTaxonomy::TABLE_NAME);
- // $sql = sprintf('ALTER TABLE %s ADD COLUMN ref_id varchar(255);',
QubitTerm::TABLE_NAME);
- return false;
+ $term = new QubitTerm;
+ $term->id = QubitTerm::ACCRUAL_ID;
+ $term->parentId = QubitTerm::ROOT_ID;
+ $term->taxonomyId = QubitTaxonomy::RELATION_TYPE_ID;
+ $term->sourceCulture = 'en';
- // TODO Add accrual constant to term table
- case 85:
- return false;
+ $termI18n = new QubitTermI18n;
+ $termI18n->culture = 'en';
+ $termI18n->name = 'Accrual';
+ $term->termI18ns[] = $termI18n;
+
+ QubitMigrate::addTermConstant($term, $configuration);
+
+ break;
// Return false if no upgrade available
default:
+
return false;
}
--
You received this message because you are subscribed to the Google Groups
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/qubit-commits?hl=en.