Skip to content

Commit

Permalink
Merge pull request #20 from okvpn/feature/configure-path
Browse files Browse the repository at this point in the history
Allow configure path & table name for migrations
This closes #17
  • Loading branch information
vtsykun authored Dec 31, 2017
2 parents a33401b + 431e88a commit 292d9d8
Show file tree
Hide file tree
Showing 24 changed files with 232 additions and 119 deletions.
4 changes: 1 addition & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
The Oro Platform Software

The MIT License (MIT)

Copyright (c) 2013, Oro, Inc.
Copyright (c) 2013-2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 11 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ command `okvpn:migration:data:load` were marked as deprecated. Fixtures package
# From 1.2 to 2.0

* Changed minimum required php version to 7.0

## IMPORTANT BC Break

* Fixtures package was removed from migration bundle and moved to separate composer installable [package](https://github.com/okvpn/fixture-bundle)

#### Constructor signature was change:

- `Okvpn\Bundle\MigrationBundle\EventListener\PreUpMigrationListener`
- `Okvpn\Bundle\MigrationBundle\Migration\Loader\MigrationsLoader`
- `Okvpn\Bundle\MigrationBundle\Tools\SchemaDiffDumper`
- `Okvpn\Bundle\MigrationBundle\Tools\SchemaDumper`
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
"name": "okvpn/migration-bundle",
"type": "symfony-bundle",
"description": "Database Schema migrations using Doctrine",
"keywords": ["Migration", "Schema"],
"keywords": ["Migration", "Schema", "Symfony Migration"],
"homepage": "https://github.com/okvpn/migration-bundle",
"license": "MIT",
"authors": [
{
"name": "Oro, Inc",
"homepage": "http://www.orocrm.com"
},
{
"name": "Tsykun Vladimir",
"email": "vtsykun@okvpn.org"
}
],
"require": {
"php": ">=7.0",
"symfony/framework-bundle": "~2.7|~3.0",
"symfony/doctrine-bridge": "~2.7|~3.0",
"symfony/twig-bundle": "~2.7|~3.0",
"symfony/framework-bundle": "~2.7|~3.0|~4.0",
"symfony/doctrine-bridge": "~2.7|~3.0|~4.0",
"symfony/twig-bundle": "~2.7|~3.0|~4.0",
"doctrine/dbal": "^2.1",
"doctrine/orm": "^2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DiffMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function configure()
public function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getOption('bundle')) {
throw new \Exception('Wrong bundle');
throw new \InvalidArgumentException('The "bundle" option can not be empty');
}

$this->version = $input->getOption('migration-version');
Expand Down
25 changes: 25 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Okvpn\Bundle\MigrationBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('okvpn_migration');

$rootNode
->children()
->scalarNode('migrations_path')->end()
->scalarNode('migrations_table')->end()
->end();
return $treeBuilder;
}
}
17 changes: 15 additions & 2 deletions src/DependencyInjection/OkvpnMigrationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@

class OkvpnMigrationExtension extends Extension
{
public function load(array $config, ContainerBuilder $container)
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

if (isset($config['migrations_path']) && null !== $config['migrations_path']) {
$container->setParameter('okvpn.migrations_path', $config['migrations_path']);
}

if (isset($config['migrations_table']) && null !== $config['migrations_table']) {
$container->setParameter('okvpn.migrations_table', $config['migrations_table']);
}
}
}
15 changes: 7 additions & 8 deletions src/Entity/DataMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table("okvpn_migrations", indexes={
* @ORM\Index(name="idx_okvpn_migrations", columns={"bundle"})
* })
* @ORM\Table("okvpn_migrations")
* @ORM\Entity()
*
* @internal
*/
class DataMigration
{
Expand All @@ -17,28 +17,27 @@ class DataMigration
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public $id;

/**
* @var string
*
* @ORM\Column(name="bundle", type="string", length=250)
*/
protected $bundle;
public $bundle;

/**
* @var string
*
* @ORM\Column(name="version", type="string", length=250)
*/
protected $version;
public $version;

/**
* @var \DateTime
*
* @ORM\Column(name="loaded_at", type="datetime")
*/
protected $loadedAt;
public $loadedAt;
}
38 changes: 38 additions & 0 deletions src/EventListener/DoctrineMetadataListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types = 1);

namespace Okvpn\Bundle\MigrationBundle\EventListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
use Okvpn\Bundle\MigrationBundle\Entity\DataMigration;

class DoctrineMetadataListener
{
const DEFAULT_TABLE = 'okvpn_fixture_data';

private $migrationTable;

/**
* @param string $migrationTable
*/
public function __construct(string $migrationTable)
{
$this->migrationTable = $migrationTable;
}

/**
* @param LoadClassMetadataEventArgs $eventArgs
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
/** @var ClassMetadata $metadata */
$metadata = $eventArgs->getClassMetadata();
if ($metadata->getName() === DataMigration::class) {
$table = $metadata->table;
$table['name'] = $this->migrationTable;
$metadata->setPrimaryTable($table);
}
}
}
20 changes: 16 additions & 4 deletions src/EventListener/PreUpMigrationListener.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
<?php

declare(strict_types = 1);

namespace Okvpn\Bundle\MigrationBundle\EventListener;

use Okvpn\Bundle\MigrationBundle\Event\PreMigrationEvent;
use Okvpn\Bundle\MigrationBundle\Migration\CreateMigrationTableMigration;

class PreUpMigrationListener
{
private $migrationTable;

/**
* @param string $migrationTable
*/
public function __construct(string $migrationTable)
{
$this->migrationTable = $migrationTable;
}

/**
* @param PreMigrationEvent $event
*/
public function onPreUp(PreMigrationEvent $event)
{
if ($event->isTableExist(CreateMigrationTableMigration::MIGRATION_TABLE)) {
if ($event->isTableExist($this->migrationTable)) {
$data = $event->getData(
sprintf(
'select * from %s where id in (select max(id) from %s group by bundle)',
CreateMigrationTableMigration::MIGRATION_TABLE,
CreateMigrationTableMigration::MIGRATION_TABLE
$this->migrationTable,
$this->migrationTable
)
);
foreach ($data as $val) {
$event->setLoadedVersion($val['bundle'], $val['version']);
}
} else {
$event->addMigration(new CreateMigrationTableMigration());
$event->addMigration(new CreateMigrationTableMigration($this->migrationTable));
}
}
}
2 changes: 1 addition & 1 deletion src/Exception/InvalidNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Okvpn\Bundle\MigrationBundle\Exception;

class InvalidNameException extends \Exception
class InvalidNameException extends \Exception implements MigrationExceptionInterface
{
}
7 changes: 7 additions & 0 deletions src/Exception/MigrationExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Okvpn\Bundle\MigrationBundle\Exception;

interface MigrationExceptionInterface extends \Throwable
{
}
2 changes: 1 addition & 1 deletion src/Exception/UnsupportedDatabasePlatformException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Okvpn\Bundle\MigrationBundle\Exception;

class UnsupportedDatabasePlatformException extends \Exception
class UnsupportedDatabasePlatformException extends \Exception implements MigrationExceptionInterface
{
}
21 changes: 19 additions & 2 deletions src/Migration/CreateMigrationTableMigration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace Okvpn\Bundle\MigrationBundle\Migration;

use Doctrine\DBAL\Schema\Schema;
Expand All @@ -8,17 +10,32 @@ class CreateMigrationTableMigration implements Migration
{
const MIGRATION_TABLE = 'okvpn_migrations';

/**
* @var string
*/
protected $migrationTable = self::MIGRATION_TABLE;

/**
* @param null|string $migrationTable
*/
public function __construct(string $migrationTable = null)
{
if ($migrationTable !== null) {
$this->migrationTable = $migrationTable;
}
}

/**
* @inheritdoc
*/
public function up(Schema $schema, QueryBag $queries)
{
$table = $schema->createTable(self::MIGRATION_TABLE);
$table = $schema->createTable($this->migrationTable);
$table->addColumn('id', 'integer', ['notnull' => true, 'autoincrement' => true]);
$table->addColumn('bundle', 'string', ['notnull' => true, 'length' => 250]);
$table->addColumn('version', 'string', ['notnull' => true, 'length' => 250]);
$table->addColumn('loaded_at', 'datetime', ['notnull' => true]);
$table->setPrimaryKey(['id']);
$table->addIndex(['bundle'], 'idx_okvpn_migrations', []);
$table->addIndex(['bundle'], 'idx_' . $this->migrationTable, []);
}
}
Loading

0 comments on commit 292d9d8

Please sign in to comment.