Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record when a migration was executed. #675

Merged
merged 1 commit into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/en/reference/custom_configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ code that would looks like the following:
use Doctrine\DBAL\Migrations\Configuration\Configuration;

$configuration = new Configuration();
$configuration->setMigrationsTableName(...);
$configuration->setMigrationsDirectory(...);
$configuration->setMigrationsNamespace(...);
$configuration->registerMigrationsFromDirectory(...);
$configuration->setMigrationsTableName('doctrine_migration_versions');
$configuration->setMigrationsColumnName('version');
$configuration->setMigrationsColumnLength(255);
$configuration->setMigrationsExecutedAtColumnName('executed_at');
$configuration->setMigrationsDirectory('/path/to/project/src/App/Migrations');
$configuration->setMigrationsNamespace('App/Migrations');
$configuration->registerMigrationsFromDirectory('/path/to/project/src/App/Migrations');

// My command that extends Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand
$command->setMigrationConfiguration($configuration);
11 changes: 6 additions & 5 deletions docs/en/reference/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Install it with composer:

composer require doctrine/migrations


PHP Binary / PHAR
~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -67,7 +66,7 @@ file like the following:

<migrations-namespace>DoctrineMigrations</migrations-namespace>

<table name="doctrine_migration_versions" />
<table name="doctrine_migration_versions" column="version" executed_at_column="executed_at" />

<migrations-directory>/path/to/migrations/classes/DoctrineMigrations</migrations-directory>

Expand All @@ -80,6 +79,8 @@ Of course you could do the same thing with a *configuration.yml* file:
name: Doctrine Sandbox Migrations
migrations_namespace: DoctrineMigrations
table_name: doctrine_migration_versions
column_name: version
executed_at_column_name: executed_at
migrations_directory: /path/to/migrations/classes/DoctrineMigrations

With the above example, the migrations tool will search the ``migrations_directory``
Expand All @@ -105,9 +106,9 @@ And if you want to specify each migration manually in YAML you can:
table_name: doctrine_migration_versions
migrations_directory: /path/to/migrations/classes/DoctrineMigrations
migrations:
migration1:
version: 20100704000000
class: DoctrineMigrations\NewMigration
migration1:
version: 20100704000000
class: DoctrineMigrations\NewMigration

If you specify your own migration classes (like `DoctrineMigrations\NewMigration` in the previous
example) you will need an autoloader unless all those classes begin with the prefix Version*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class AbstractFileConfiguration extends Configuration
'migrations_namespace',
'table_name',
'column_name',
'executed_at_column_name',
'organize_migrations',
'name',
'migrations_directory',
Expand Down Expand Up @@ -87,6 +88,10 @@ protected function setConfiguration(array $config) : void
$this->setMigrationsColumnName($config['column_name']);
}

if (isset($config['executed_at_column_name'])) {
$this->setMigrationsExecutedAtColumnName($config['executed_at_column_name']);
}

if (isset($config['organize_migrations'])) {
$this->setMigrationOrganization($config['organize_migrations']);
}
Expand Down
49 changes: 40 additions & 9 deletions lib/Doctrine/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Configuration
/** @var int */
private $migrationsColumnLength = 255;

/** @var string */
private $migrationsExecutedAtColumnName = 'executed_at';

/** @var string|null */
private $migrationsDirectory;

Expand Down Expand Up @@ -72,19 +75,21 @@ class Configuration
/** @var QueryWriter|null */
private $queryWriter;

/** @var DependencyFactory */
/** @var DependencyFactory|null */
private $dependencyFactory;

public function __construct(
Connection $connection,
?OutputWriter $outputWriter = null,
?MigrationFinder $migrationFinder = null,
?QueryWriter $queryWriter = null
?QueryWriter $queryWriter = null,
?DependencyFactory $dependencyFactory = null
) {
$this->connection = $connection;
$this->outputWriter = $outputWriter;
$this->migrationFinder = $migrationFinder;
$this->queryWriter = $queryWriter;
$this->connection = $connection;
$this->outputWriter = $outputWriter;
$this->migrationFinder = $migrationFinder;
$this->queryWriter = $queryWriter;
$this->dependencyFactory = $dependencyFactory;
}

public function setName(string $name) : void
Expand Down Expand Up @@ -125,7 +130,7 @@ public function getMigrationsColumnName() : string
public function getQuotedMigrationsColumnName() : string
{
return $this->getDependencyFactory()
->getMigrationTableCreator()
->getMigrationTable()
->getMigrationsColumn()
->getQuotedName($this->connection->getDatabasePlatform());
}
Expand All @@ -140,6 +145,24 @@ public function getMigrationsColumnLength() : int
return $this->migrationsColumnLength;
}

public function setMigrationsExecutedAtColumnName(string $migrationsExecutedAtColumnName) : void
{
$this->migrationsExecutedAtColumnName = $migrationsExecutedAtColumnName;
}

public function getMigrationsExecutedAtColumnName() : string
{
return $this->migrationsExecutedAtColumnName;
}

public function getQuotedMigrationsExecutedAtColumnName() : string
{
return $this->getDependencyFactory()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, this thing is just a service locator now... Wiring everything should imho be done in the SF Bundle / Zend Module and/or in a factory/builder object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be done, but not without a big BC break. The only place it is being used like a service locator is in places where I wanted to maintain BC. Like here. Also, these are all internal services so it feels weird to put the burden on the end user to wire all these classes up that they should not even know about or have access to.

Copy link
Member Author

@jwage jwage May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, I am referring to developers who don't use Symfony, Zend, etc. and have their app integrated with Doctrine DBAL and Migrations. Today all that is required to configure migrations is setting up one service that is passed your DBAL connection:

$configuration = new Configuration($connection);
// configure your migrations
$configuration->...;

In 2.0 this is the same. All the existing setups will just continue to work. All the services that make up the internals are not meant to be public facing services. If I put the wiring in the framework integrations, wouldn't this mean that when I refactor the internals, it'd be a BC break in the framework integrations due to the wiring needing to be updated?

->getMigrationTable()
->getExecutedAtColumn()
->getQuotedName($this->connection->getDatabasePlatform());
}

public function setMigrationsDirectory(string $migrationsDirectory) : void
{
$this->migrationsDirectory = $migrationsDirectory;
Expand Down Expand Up @@ -243,6 +266,14 @@ public function hasVersionMigrated(Version $version) : bool
return $this->getDependencyFactory()->getMigrationRepository()->hasVersionMigrated($version);
}

/**
* @return mixed[]
*/
public function getVersionData(Version $version) : ?array
{
return $this->getDependencyFactory()->getMigrationRepository()->getVersionData($version);
}

public function resolveVersionAlias(string $alias) : ?string
{
return $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias($alias);
Expand All @@ -260,12 +291,12 @@ public function isDryRun() : bool

public function isMigrationTableCreated() : bool
{
return $this->getDependencyFactory()->getMigrationTableCreator()->isMigrationTableCreated();
return $this->getDependencyFactory()->getMigrationTableStatus()->isCreated();
}

public function createMigrationTable() : bool
{
return $this->getDependencyFactory()->getMigrationTableCreator()->createMigrationTable();
return $this->getDependencyFactory()->getMigrationTableManipulator()->createMigrationTable();
}

public function getDateTime(string $version) : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<xs:complexType>
<xs:attribute type="xs:string" name="name"/>
<xs:attribute type="xs:string" name="column"/>
<xs:attribute type="xs:string" name="executed_at_column"/>
</xs:complexType>
</xs:element>
<xs:element name="organize-migrations" minOccurs="0" maxOccurs="1">
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/Migrations/Configuration/XmlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ protected function doLoad(string $file) : void
$config['column_name'] = (string) $xml->table['column'];
}

if (isset($xml->table['executed_at_column'])) {
$config['executed_at_column_name'] = (string) $xml->table['executed_at_column'];
}

if (isset($xml->{'migrations-namespace'})) {
$config['migrations_namespace'] = (string) $xml->{'migrations-namespace'};
}
Expand Down
79 changes: 59 additions & 20 deletions lib/Doctrine/Migrations/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(Configuration $configuration)

public function getEventDispatcher() : EventDispatcher
{
return $this->getDependency(EventDispatcher::class, function () {
return $this->getDependency(EventDispatcher::class, function () : EventDispatcher {
return new EventDispatcher(
$this->configuration,
$this->getConnection()->getEventManager()
Expand All @@ -40,7 +40,7 @@ public function getEventDispatcher() : EventDispatcher

public function getSchemaDiffProvider() : SchemaDiffProviderInterface
{
return $this->getDependency(SchemaDiffProviderInterface::class, function () {
return $this->getDependency(SchemaDiffProviderInterface::class, function () : LazySchemaDiffProvider {
return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration(
new SchemaDiffProvider(
$this->getConnection()->getSchemaManager(),
Expand All @@ -52,24 +52,26 @@ public function getSchemaDiffProvider() : SchemaDiffProviderInterface

public function getMigrationFileBuilder() : MigrationFileBuilder
{
return $this->getDependency(MigrationFileBuilder::class, function () {
return $this->getDependency(MigrationFileBuilder::class, function () : MigrationFileBuilder {
return new MigrationFileBuilder(
$this->getConnection()->getDatabasePlatform(),
$this->configuration->getMigrationsTableName(),
$this->configuration->getQuotedMigrationsColumnName()
$this->configuration->getQuotedMigrationsColumnName(),
$this->configuration->getQuotedMigrationsExecutedAtColumnName()
);
});
}

public function getParameterFormatter() : ParameterFormatterInterface
{
return $this->getDependency(ParameterFormatter::class, function () {
return $this->getDependency(ParameterFormatter::class, function () : ParameterFormatter {
return new ParameterFormatter($this->getConnection());
});
}

public function getMigrationRepository() : MigrationRepository
{
return $this->getDependency(MigrationRepository::class, function () {
return $this->getDependency(MigrationRepository::class, function () : MigrationRepository {
return new MigrationRepository(
$this->configuration,
$this->getConnection(),
Expand All @@ -79,19 +81,56 @@ public function getMigrationRepository() : MigrationRepository
});
}

public function getMigrationTableCreator() : MigrationTableCreator
public function getMigrationTableManipulator() : MigrationTableManipulator
{
return $this->getDependency(MigrationTableCreator::class, function () {
return new MigrationTableCreator(
return $this->getDependency(MigrationTableManipulator::class, function () : MigrationTableManipulator {
return new MigrationTableManipulator(
$this->configuration,
$this->getConnection()->getSchemaManager()
$this->getConnection()->getSchemaManager(),
$this->getMigrationTable(),
$this->getMigrationTableStatus(),
$this->getMigrationTableUpdater()
);
});
}

public function getMigrationTable() : MigrationTable
{
return $this->getDependency(MigrationTable::class, function () : MigrationTable {
return new MigrationTable(
$this->configuration->getMigrationsTableName(),
$this->configuration->getMigrationsColumnName(),
$this->configuration->getMigrationsColumnLength(),
$this->configuration->getMigrationsExecutedAtColumnName()
);
});
}

public function getMigrationTableStatus() : MigrationTableStatus
{
return $this->getDependency(MigrationTableStatus::class, function () : MigrationTableStatus {
return new MigrationTableStatus(
$this->getConnection()->getSchemaManager(),
$this->getMigrationTable()
);
});
}

public function getMigrationTableUpdater() : MigrationTableUpdater
{
return $this->getDependency(MigrationTableUpdater::class, function () : MigrationTableUpdater {
return new MigrationTableUpdater(
$this->getConnection(),
$this->getConnection()->getSchemaManager(),
$this->getMigrationTable(),
$this->getConnection()->getDatabasePlatform()
);
});
}

public function getVersionExecutor() : VersionExecutor
{
return $this->getDependency(VersionExecutor::class, function () {
return $this->getDependency(VersionExecutor::class, function () : VersionExecutor {
return new VersionExecutor(
$this->configuration,
$this->getConnection(),
Expand All @@ -104,7 +143,7 @@ public function getVersionExecutor() : VersionExecutor

public function getQueryWriter() : FileQueryWriter
{
return $this->getDependency(FileQueryWriter::class, function () {
return $this->getDependency(FileQueryWriter::class, function () : FileQueryWriter {
return new FileQueryWriter(
$this->getOutputWriter(),
$this->getMigrationFileBuilder()
Expand All @@ -114,14 +153,14 @@ public function getQueryWriter() : FileQueryWriter

public function getOutputWriter() : OutputWriter
{
return $this->getDependency(OutputWriter::class, function () {
return $this->getDependency(OutputWriter::class, function () : OutputWriter {
return new OutputWriter();
});
}

public function getVersionAliasResolver() : VersionAliasResolver
{
return $this->getDependency(VersionAliasResolver::class, function () {
return $this->getDependency(VersionAliasResolver::class, function () : VersionAliasResolver {
return new VersionAliasResolver(
$this->getMigrationRepository()
);
Expand All @@ -130,28 +169,28 @@ public function getVersionAliasResolver() : VersionAliasResolver

public function getMigrationPlanCalculator() : MigrationPlanCalculator
{
return $this->getDependency(MigrationPlanCalculator::class, function () {
return $this->getDependency(MigrationPlanCalculator::class, function () : MigrationPlanCalculator {
return new MigrationPlanCalculator($this->getMigrationRepository());
});
}

public function getRecursiveRegexFinder() : RecursiveRegexFinder
{
return $this->getDependency(RecursiveRegexFinder::class, function () {
return $this->getDependency(RecursiveRegexFinder::class, function () : RecursiveRegexFinder {
return new RecursiveRegexFinder();
});
}

public function getMigrationGenerator() : MigrationGenerator
{
return $this->getDependency(MigrationGenerator::class, function () {
return $this->getDependency(MigrationGenerator::class, function () : MigrationGenerator {
return new MigrationGenerator($this->configuration);
});
}

public function getMigrationSqlGenerator() : MigrationSqlGenerator
{
return $this->getDependency(MigrationSqlGenerator::class, function () {
return $this->getDependency(MigrationSqlGenerator::class, function () : MigrationSqlGenerator {
return new MigrationSqlGenerator(
$this->configuration,
$this->getConnection()->getDatabasePlatform()
Expand All @@ -161,7 +200,7 @@ public function getMigrationSqlGenerator() : MigrationSqlGenerator

public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper
{
return $this->getDependency(MigrationStatusInfosHelper::class, function () {
return $this->getDependency(MigrationStatusInfosHelper::class, function () : MigrationStatusInfosHelper {
return new MigrationStatusInfosHelper(
$this->configuration,
$this->getMigrationRepository()
Expand All @@ -171,7 +210,7 @@ public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper

public function getMigrator() : Migrator
{
return $this->getDependency(Migrator::class, function () {
return $this->getDependency(Migrator::class, function () : Migrator {
return new Migrator(
$this->configuration,
$this->getMigrationRepository(),
Expand Down
Loading