Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Update migration classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nielssp committed Jun 21, 2016
1 parent 020c1e1 commit 442621b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
18 changes: 9 additions & 9 deletions src/Database/Common/PostgresqlTypeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,23 @@ public function getDefinition($table)
$sql .= " AND a.attnum = ANY(ix.indkey) AND t.relkind = 'r'";
$sql .= " AND t.relname = '" . $this->db->tableName($table) . "'";
$result = $this->db->query($sql);
$indexes = array();
$keys = array();
while ($row = $result->fetchAssoc()) {
$index = $row['index_name'];
$key = $row['index_name'];
$column = $row['column_name'];
$unique = $row['indisunique'] != 0;
if (isset($indexes[$index])) {
$indexes[$index]['columns'][] = $column;
if (isset($keys[$key])) {
$keys[$key]['columns'][] = $column;
} else {
$indexes[$index] = array(
$keys[$key] = array(
'columns' => array(
$column
),
'unique' => $unique
);
}
}
foreach ($indexes as $name => $index) {
foreach ($keys as $name => $key) {
$name = preg_replace(
'/^' . preg_quote($this->db->tableName($table) . '_', '/') . '/',
'',
Expand All @@ -272,10 +272,10 @@ public function getDefinition($table)
if ($count == 0) {
continue;
}
if ($index['unique']) {
$definition->addUnique($index['columns'], $name);
if ($key['unique']) {
$definition->addUnique($key['columns'], $name);
} else {
$definition->addKey($index['columns'], $name);
$definition->addKey($key['columns'], $name);
}
}
return $definition;
Expand Down
67 changes: 34 additions & 33 deletions src/Migration/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ abstract class Migration
{

/**
* @var MigratableDatabase Database.
* @var MigratableDatabase
*/
private $db = null;

/**
* @var MigrationDefinition Schema.
* @var MigrationDefinition
*/
private $schema = null;
private $definition = null;

/**
* @var bool Whether to ignore exceptions.
Expand All @@ -35,13 +35,13 @@ abstract class Migration
*
* @param MigratableDatabase $db
* Database to run migration on.
* @param MigrationDefinition $schema
* A migration schema.
* @param MigrationDefinition $definition
* A migration definition.
*/
final public function __construct(MigratableDatabase $db, MigrationDefinition $schema)
final public function __construct(MigratableDatabase $db, MigrationDefinition $definition)
{
$this->db = $db;
$this->schema = $schema;
$this->definition = $definition;
}

/**
Expand All @@ -65,20 +65,20 @@ public function __get($table)
*/
public function __isset($table)
{
return isset($this->db->table);
return isset($this->db->$table);
}

/**
* Create a table.
*
* @param DefinitionBuilder $definition
* Schema for table.
* @param string $table Table name.
* @param DefinitionBuilder $definition Table definition.
*/
protected function createTable(DefinitionBuilder $definition)
protected function createTable($table, DefinitionBuilder $definition)
{
try {
$this->db->createTable($definition);
$this->schema->createTable($definition);
$this->db->createTable($table, $definition);
$this->definition->createTable($table, $definition);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -96,7 +96,7 @@ protected function dropTable($table)
{
try {
$this->db->dropTable($table);
$this->schema->dropTable($table);
$this->definition->dropTable($table);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -118,7 +118,7 @@ protected function addColumn($table, $column, DataType $type)
{
try {
$this->db->addColumn($table, $column, $type);
$this->schema->addColumn($table, $column, $type);
$this->definition->addColumn($table, $column, $type);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -138,7 +138,7 @@ protected function deleteColumn($table, $column)
{
try {
$this->db->deleteColumn($table, $column);
$this->schema->deleteColumn($table, $column);
$this->definition->deleteColumn($table, $column);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -160,7 +160,7 @@ protected function alterColumn($table, $column, DataType $type)
{
try {
$this->db->alterColumn($table, $column, $type);
$this->schema->alterColumn($table, $column, $type);
$this->definition->alterColumn($table, $column, $type);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -182,7 +182,7 @@ protected function renameColumn($table, $column, $newName)
{
try {
$this->db->renameColumn($table, $column, $newName);
$this->schema->renameColumn($table, $column, $newName);
$this->definition->renameColumn($table, $column, $newName);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -197,15 +197,16 @@ protected function renameColumn($table, $column, $newName)
* Table name.
* @param string $key
* Key name.
* @param array $options
* Associative array of index options, with keys
* 'unique' and 'columns'.
* @param string[] $columns
* Columns.
* @param bool $unique
* Uniqueness.
*/
protected function createKey($table, $key, $options = array())
protected function createKey($table, $key, array $columns, $unique = true)
{
try {
$this->db->createKey($table, $key, $options);
$this->schema->createKey($table, $key, $options);
$this->db->createKey($table, $key, $columns, $unique);
$this->definition->createKey($table, $key, $columns, $unique);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -225,7 +226,7 @@ protected function deleteKey($table, $key)
{
try {
$this->db->deleteKey($table, $key);
$this->schema->deleteKey($table, $key);
$this->definition->deleteKey($table, $key);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand All @@ -239,17 +240,17 @@ protected function deleteKey($table, $key)
* @param string $table
* Table name.
* @param string $key
* Ket name.
* @param array $options
* Associative array of index options, with keys
* 'unique' and 'columns'.
* @throws \Exception
* Key name.
* @param string[] $columns
* Columns.
* @param bool $unique
* Uniqueness.
*/
protected function alterKey($table, $key, $options = array())
protected function alterKey($table, $key, array $columns, $unique = true)
{
try {
$this->alterKey($table, $key, $options);
$this->schema->alterKey($table, $key, $options);
$this->db->alterKey($table, $key, $columns, $unique);
$this->definition->alterKey($table, $key, $columns, $unique);
} catch (\Exception $e) {
if (! $this->ignoreExceptions) {
throw $e;
Expand Down
2 changes: 1 addition & 1 deletion src/Migration/MigrationDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getDefinition($table)
/**
* {@inheritdoc}
*/
public function createTable($table, DefinitionBuilder $definition)
public function createTable($table, \Jivoo\Data\Definition $definition)
{
$this->tables[] = $table;
$this->definitions[$table] = $definition;
Expand Down
16 changes: 8 additions & 8 deletions src/Migration/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class MigrationRunner
private $definition;

/**
* @var MigrationDefinition[] Array of schemas.
* @var MigrationDefinition[] Array of definitions.
*/
private $migrationSchemas = array();
private $migrationDefinitions = array();

/**
* @var string[] Associative array of database names and migration dirs.
Expand Down Expand Up @@ -249,11 +249,11 @@ public function run($dbName, $migrationName)
$this->logger->info('Initializing migration ' . $migrationName);
Utilities::assumeSubclassOf($migrationName, 'Jivoo\Data\Migration\Migration');

// The migration schema keeps track of the state of the database
if (! isset($this->migrationSchemas[$dbName])) {
$this->migrationSchemas[$dbName] = new MigrationDefinition($db);
// The migration definition keeps track of the state of the database
if (! isset($this->migrationDefinitions[$dbName])) {
$this->migrationDefinitions[$dbName] = new MigrationDefinition($db);
}
$migrationSchema = $this->migrationSchemas[$dbName];
$migrationSchema = $this->migrationDefinitions[$dbName];

$migration = new $migrationName($db, $migrationSchema);
try {
Expand All @@ -275,8 +275,8 @@ public function run($dbName, $migrationName)
*/
public function finalize($name)
{
if (isset($this->migrationSchemas[$name])) {
$this->migrationSchemas[$name]->finalize();
if (isset($this->migrationDefinitions[$name])) {
$this->migrationDefinitions[$name]->finalize();
}
$mtime = filemtime($this->migrationDirs[$name] . '/.');
$this->config['mtimes'][$name] = $mtime;
Expand Down

0 comments on commit 442621b

Please sign in to comment.