Skip to content

Commit

Permalink
Refactoring Configuration (and tests), and build
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Feb 15, 2011
1 parent 44941f0 commit 1148231
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 330 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ build/
logs/
reports/
dist/
tests/phpunit.xml

4 changes: 3 additions & 1 deletion build.properties.dev
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ test.phpunit_configuration_file=
test.phpunit_generate_coverage=0
test.pmd_reports=0
test.pdepend_exec=
test.phpmd_exec=
test.phpmd_exec=
test.phpunit_configuration_option=phpunit-dest.xml
test.phpunit_coverage_option=
8 changes: 1 addition & 7 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
-->

<project name="DoctrineMigrations" default="build" basedir=".">

<taskdef classname="NativePhpunitTask" classpath="./tests/" name="nativephpunit" />

<property file="build.properties" />

<!--
Expand Down Expand Up @@ -120,10 +117,7 @@
</else>
</if>

<nativephpunit
testfile="./tests/Doctrine/DBAL/Migrations/Tests/AllTests.php" junitlogfile="${build.dir}/logs/testsuites.xml"
testdirectory="./tests" coverageclover="${test.phpunit_coverage_file}" configuration="${test.phpunit_configuration_file}"
/>
<exec command="phpunit --configuration ${test.phpunit_configuration_option} ${test.phpunit_coverage_option} --log-junit ${project.basedir}/${build.dir}/logs/testsuites.xml Doctrine/DBAL/Migrations/Tests/" dir="./tests" passthru="true" />
<phpunitreport infile="${build.dir}/logs/testsuites.xml" format="frames" todir="${report.dir}/tests" />

<tstamp/>
Expand Down
138 changes: 85 additions & 53 deletions lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,61 @@
*/
class Configuration
{
/** Name of this set of migrations */
private $_name;
/**
* Name of this set of migrations
*
* @var string
*/
private $name;

/** Flag for whether or not the migration table has been created */
private $_migrationTableCreated = false;
/**
* Flag for whether or not the migration table has been created
*
* @var bool
*/
private $migrationTableCreated = false;

/** Connection instance to use for migrations */
private $_connection;
/**
* Connection instance to use for migrations
*
* @var Connection
*/
private $connection;

/** OutputWriter instance for writing output during migrations */
private $_outputWriter;
/**
* OutputWriter instance for writing output during migrations
*
* @var OutputWriter
*/
private $outputWriter;

/** The migration table name to track versions in */
private $_migrationsTableName = 'doctrine_migration_versions';
/**
* The migration table name to track versions in
*
* @var string
*/
private $migrationsTableName = 'doctrine_migration_versions';

/** The path to a directory where new migration classes will be written */
private $_migrationsDirectory;
/**
* The path to a directory where new migration classes will be written
*
* @var string
*/
private $migrationsDirectory;

/** Namespace the migration classes live in */
private $_migrationsNamespace;
/**
* Namespace the migration classes live in
*
* @var string
*/
private $migrationsNamespace;

/** Array of the registered migrations */
private $_migrations = array();
/**
* Array of the registered migrations
*
* @var array
*/
private $migrations = array();

/**
* Construct a migration configuration object.
Expand All @@ -74,11 +106,11 @@ class Configuration
*/
public function __construct(Connection $connection, OutputWriter $outputWriter = null)
{
$this->_connection = $connection;
$this->connection = $connection;
if ($outputWriter === null) {
$outputWriter = new OutputWriter();
}
$this->_outputWriter = $outputWriter;
$this->outputWriter = $outputWriter;
}

/**
Expand All @@ -89,10 +121,10 @@ public function __construct(Connection $connection, OutputWriter $outputWriter =
*/
public function validate()
{
if ( ! $this->_migrationsNamespace) {
if ( ! $this->migrationsNamespace) {
throw MigrationException::migrationsNamespaceRequired();
}
if ( ! $this->_migrationsDirectory) {
if ( ! $this->migrationsDirectory) {
throw MigrationException::migrationsDirectoryRequired();
}
}
Expand All @@ -104,7 +136,7 @@ public function validate()
*/
public function setName($name)
{
$this->_name = $name;
$this->name = $name;
}

/**
Expand All @@ -114,7 +146,7 @@ public function setName($name)
*/
public function getName()
{
return $this->_name;
return $this->name;
}

/**
Expand All @@ -124,7 +156,7 @@ public function getName()
*/
public function getOutputWriter()
{
return $this->_outputWriter;
return $this->outputWriter;
}

/**
Expand Down Expand Up @@ -152,7 +184,7 @@ public function formatVersion($version)
*/
public function getConnection()
{
return $this->_connection;
return $this->connection;
}

/**
Expand All @@ -162,7 +194,7 @@ public function getConnection()
*/
public function setMigrationsTableName($tableName)
{
$this->_migrationsTableName = $tableName;
$this->migrationsTableName = $tableName;
}

/**
Expand All @@ -172,7 +204,7 @@ public function setMigrationsTableName($tableName)
*/
public function getMigrationsTableName()
{
return $this->_migrationsTableName;
return $this->migrationsTableName;
}

/**
Expand All @@ -182,7 +214,7 @@ public function getMigrationsTableName()
*/
public function setMigrationsDirectory($migrationsDirectory)
{
$this->_migrationsDirectory = $migrationsDirectory;
$this->migrationsDirectory = $migrationsDirectory;
}

/**
Expand All @@ -192,7 +224,7 @@ public function setMigrationsDirectory($migrationsDirectory)
*/
public function getMigrationsDirectory()
{
return $this->_migrationsDirectory;
return $this->migrationsDirectory;
}

/**
Expand All @@ -202,7 +234,7 @@ public function getMigrationsDirectory()
*/
public function setMigrationsNamespace($migrationsNamespace)
{
$this->_migrationsNamespace = $migrationsNamespace;
$this->migrationsNamespace = $migrationsNamespace;
}

/**
Expand All @@ -212,7 +244,7 @@ public function setMigrationsNamespace($migrationsNamespace)
*/
public function getMigrationsNamespace()
{
return $this->_migrationsNamespace;
return $this->migrationsNamespace;
}

/**
Expand All @@ -233,7 +265,7 @@ public function registerMigrationsFromDirectory($path)
require_once($file);
$info = pathinfo($file);
$version = substr($info['filename'], 7);
$class = $this->_migrationsNamespace . '\\' . $info['filename'];
$class = $this->migrationsNamespace . '\\' . $info['filename'];
$versions[] = $this->registerMigration($version, $class);
}
return $versions;
Expand All @@ -250,12 +282,12 @@ public function registerMigration($version, $class)
{
$version = (string) $version;
$class = (string) $class;
if (isset($this->_migrations[$version])) {
throw MigrationException::duplicateMigrationVersion($version, get_class($this->_migrations[$version]));
if (isset($this->migrations[$version])) {
throw MigrationException::duplicateMigrationVersion($version, get_class($this->migrations[$version]));
}
$version = new Version($this, $version, $class);
$this->_migrations[$version->getVersion()] = $version;
ksort($this->_migrations);
$this->migrations[$version->getVersion()] = $version;
ksort($this->migrations);
return $version;
}

Expand Down Expand Up @@ -283,7 +315,7 @@ public function registerMigrations(array $migrations)
*/
public function getMigrations()
{
return $this->_migrations;
return $this->migrations;
}

/**
Expand All @@ -295,10 +327,10 @@ public function getMigrations()
*/
public function getVersion($version)
{
if ( ! isset($this->_migrations[$version])) {
if ( ! isset($this->migrations[$version])) {
MigrationException::unknownMigrationVersion($version);
}
return $this->_migrations[$version];
return $this->migrations[$version];
}

/**
Expand All @@ -309,7 +341,7 @@ public function getVersion($version)
*/
public function hasVersion($version)
{
return isset($this->_migrations[$version]) ? true : false;
return isset($this->migrations[$version]) ? true : false;
}

/**
Expand All @@ -322,7 +354,7 @@ public function hasVersionMigrated(Version $version)
{
$this->createMigrationTable();

$version = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationsTableName . " WHERE version = '" . $version->getVersion() . "'");
$version = $this->connection->fetchColumn("SELECT version FROM " . $this->migrationsTableName . " WHERE version = '" . $version->getVersion() . "'");
return $version !== false ? true : false;
}

Expand All @@ -335,7 +367,7 @@ public function getCurrentVersion()
{
$this->createMigrationTable();

$result = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationsTableName . " ORDER BY version DESC LIMIT 1");
$result = $this->connection->fetchColumn("SELECT version FROM " . $this->migrationsTableName . " ORDER BY version DESC LIMIT 1");
return $result !== false ? (string) $result : '0';
}

Expand All @@ -348,7 +380,7 @@ public function getNumberOfExecutedMigrations()
{
$this->createMigrationTable();

$result = $this->_connection->fetchColumn("SELECT COUNT(version) FROM " . $this->_migrationsTableName);
$result = $this->connection->fetchColumn("SELECT COUNT(version) FROM " . $this->migrationsTableName);
return $result !== false ? $result : 0;
}

Expand All @@ -359,7 +391,7 @@ public function getNumberOfExecutedMigrations()
*/
public function getNumberOfAvailableMigrations()
{
return count($this->_migrations);
return count($this->migrations);
}

/**
Expand All @@ -369,7 +401,7 @@ public function getNumberOfAvailableMigrations()
*/
public function getLatestVersion()
{
$versions = array_keys($this->_migrations);
$versions = array_keys($this->migrations);
$latest = end($versions);
return $latest !== false ? (string) $latest : '0';
}
Expand All @@ -383,20 +415,20 @@ public function createMigrationTable()
{
$this->validate();

if ($this->_migrationTableCreated) {
if ($this->migrationTableCreated) {
return false;
}

$schema = $this->_connection->getSchemaManager()->createSchema();
if ( ! $schema->hasTable($this->_migrationsTableName)) {
$schema = $this->connection->getSchemaManager()->createSchema();
if ( ! $schema->hasTable($this->migrationsTableName)) {
$columns = array(
'version' => new Column('version', Type::getType('string'), array('length' => 14)),
);
$table = new Table($this->_migrationsTableName, $columns);
$table = new Table($this->migrationsTableName, $columns);
$table->setPrimaryKey(array('version'));
$this->_connection->getSchemaManager()->createTable($table);
$this->connection->getSchemaManager()->createTable($table);

$this->_migrationTableCreated = true;
$this->migrationTableCreated = true;

return true;
}
Expand All @@ -414,11 +446,11 @@ public function createMigrationTable()
public function getMigrationsToExecute($direction, $to)
{
if ($direction === 'down') {
$allVersions = array_reverse(array_keys($this->_migrations));
$classes = array_reverse(array_values($this->_migrations));
$allVersions = array_reverse(array_keys($this->migrations));
$classes = array_reverse(array_values($this->migrations));
$allVersions = array_combine($allVersions, $classes);
} else {
$allVersions = $this->_migrations;
$allVersions = $this->migrations;
}
$versions = array();
foreach ($allVersions as $version) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand Down Expand Up @@ -38,7 +36,6 @@
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Jonathan Wage <[email protected]>
*/
abstract class AbstractCommand extends Command
Expand Down
Loading

0 comments on commit 1148231

Please sign in to comment.