Skip to content

Commit d5dc3e0

Browse files
Merge pull request #1 from laravel-doctrine/master
1.0.0
2 parents fd25674 + e9d978d commit d5dc3e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1921
-249
lines changed

.php_cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ return Symfony\CS\Config\Config::create()
88
->setUsingCache(true)
99
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
1010
->fixers(array(
11-
'psr0',
11+
'psr4',
1212
'encoding',
1313
'short_tag',
1414
'blankline_after_open_tag',

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ language: php
33
php:
44
- 5.5
55
- 5.6
6+
- 7.0
7+
- hhvm
68

79
before_script:
810
- travis_retry composer self-update
@@ -12,6 +14,8 @@ after_script:
1214
- wget https://scrutinizer-ci.com/ocular.phar
1315
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
1416

17+
sudo: false
18+
1519
script: phpunit --coverage-clover=coverage.clover
1620

1721
matrix:

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
<img src="https://cloud.githubusercontent.com/assets/7728097/9831721/1ec0fdbc-5962-11e5-8c80-6f03f8275fbd.jpg"/>
44

5-
[![GitHub release](https://img.shields.io/github/release/laravel-doctrine/migrations.svg?style=flat)](https://packagist.org/packages/laravel-doctrine/migrations)
6-
[![Travis](https://img.shields.io/travis/laravel-doctrine/migrations.svg?style=flat)](https://travis-ci.org/laravel-doctrine/migrations)
7-
[![Scrutinizer](https://img.shields.io/scrutinizer/g/laravel-doctrine/migrations.svg?style=flat)](https://github.com/laravel-doctrine/migrations)
8-
[![Packagist](https://img.shields.io/packagist/dd/laravel-doctrine/migrations.svg?style=flat)](https://packagist.org/packages/laravel-doctrine/migrations)
9-
[![Packagist](https://img.shields.io/packagist/dm/laravel-doctrine/migrations.svg?style=flat)](https://packagist.org/packages/laravel-doctrine/migrations)
10-
[![Packagist](https://img.shields.io/packagist/dt/laravel-doctrine/migrations.svg?style=flat)](https://packagist.org/packages/laravel-doctrine/migrations)
5+
[![GitHub release](https://img.shields.io/github/release/laravel-doctrine/migrations.svg?style=flat-square)](https://packagist.org/packages/laravel-doctrine/migrations)
6+
[![Travis](https://img.shields.io/travis/laravel-doctrine/migrations.svg?style=flat-square)](https://travis-ci.org/laravel-doctrine/migrations)
7+
[![StyleCI](https://styleci.io/repos/39036028/shield)](https://styleci.io/repos/39036028)
8+
[![Scrutinizer](https://img.shields.io/scrutinizer/g/laravel-doctrine/migrations.svg?style=flat-square)](https://github.com/laravel-doctrine/migrations)
9+
[![Packagist](https://img.shields.io/packagist/dm/laravel-doctrine/migrations.svg?style=flat-square)](https://packagist.org/packages/laravel-doctrine/migrations)
10+
[![Packagist](https://img.shields.io/packagist/dt/laravel-doctrine/migrations.svg?style=flat-square)](https://packagist.org/packages/laravel-doctrine/migrations)
1111

1212
*Doctrine Migrations for Laravel*

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"illuminate/contracts": "~5.1",
2323
"illuminate/console": "~5.1",
2424
"illuminate/support": "~5.1",
25-
"laravel-doctrine/orm": "dev-develop"
25+
"laravel-doctrine/orm": "dev-master"
2626
},
2727
"require-dev": {
2828
"phpunit/phpunit": "~4.0",

config/migrations.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,47 @@
11
<?php
22

33
return [
4-
'name' => 'Doctrine Migrations',
5-
'namespace' => 'Database\\Migrations',
6-
'table' => 'migrations',
7-
'directory' => database_path('migrations'),
8-
'naming_strategy' => LaravelDoctrine\Migrations\Naming\LaravelNamingStrategy::class
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Migration Repository Table
8+
|--------------------------------------------------------------------------
9+
|
10+
| This table keeps track of all the migrations that have already run for
11+
| your application. Using this information, we can determine which of
12+
| the migrations on disk haven't actually been run in the database.
13+
|
14+
*/
15+
'table' => 'migrations',
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Migration Directory
19+
|--------------------------------------------------------------------------
20+
|
21+
| This directory is where all migrations will be stored
22+
|
23+
*/
24+
'directory' => database_path('migrations'),
25+
/*
26+
|--------------------------------------------------------------------------
27+
| Migration Namespace
28+
|--------------------------------------------------------------------------
29+
|
30+
| This namespace will be used on all migrations
31+
|
32+
*/
33+
'namespace' => 'Database\\Migrations',
34+
/*
35+
|--------------------------------------------------------------------------
36+
| Migration Repository Table
37+
|--------------------------------------------------------------------------
38+
|
39+
| Tables which are filtered by Regular Expression. You optionally
40+
| exclude or limit to certain tables. The default will
41+
| filter all tables.
42+
|
43+
*/
44+
'schema' => [
45+
'filter' => '/^(?).*$/'
46+
]
947
];

src/Configuration/ConfigurationFactory.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Doctrine\DBAL\Connection;
66
use Illuminate\Contracts\Config\Repository;
77
use Illuminate\Contracts\Container\Container;
8-
use LaravelDoctrine\Migrations\Naming\LaravelNamingStrategy;
8+
use LaravelDoctrine\Migrations\Naming\DefaultNamingStrategy;
99

1010
class ConfigurationFactory
1111
{
@@ -22,8 +22,6 @@ class ConfigurationFactory
2222
/**
2323
* @param Repository $config
2424
* @param Container $container
25-
*
26-
* @internal param NamingStrategy $namingStrategy
2725
*/
2826
public function __construct(Repository $config, Container $container)
2927
{
@@ -40,12 +38,16 @@ public function make(Connection $connection)
4038
{
4139
$configuration = new Configuration($connection);
4240

43-
$configuration->setName($this->config->get('migrations.name', 'DoctrineMigrations'));
41+
$configuration->setName($this->config->get('migrations.name', 'Doctrine Migrations'));
4442
$configuration->setMigrationsNamespace($this->config->get('migrations.namespace', 'Database\\Migrations'));
4543
$configuration->setMigrationsTableName($this->config->get('migrations.table', 'migrations'));
4644

45+
$configuration->getConnection()->getConfiguration()->setFilterSchemaAssetsExpression(
46+
$this->config->get('migrations.schema.filter', '/^(?).*$/')
47+
);
48+
4749
$configuration->setNamingStrategy($this->container->make(
48-
$this->config->get('migrations.naming_strategy', LaravelNamingStrategy::class)
50+
$this->config->get('migrations.naming_strategy', DefaultNamingStrategy::class)
4951
));
5052

5153
$configuration->setMigrationFinder($configuration->getNamingStrategy()->getFinder());

src/Console/DiffCommand.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace LaravelDoctrine\Migrations\Console;
4+
5+
use Doctrine\Common\Persistence\ManagerRegistry;
6+
use Doctrine\DBAL\Migrations\Provider\OrmSchemaProvider;
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use Illuminate\Console\Command;
9+
use LaravelDoctrine\Migrations\Configuration\ConfigurationProvider;
10+
use LaravelDoctrine\Migrations\Output\MigrationFileGenerator;
11+
use LaravelDoctrine\Migrations\Output\SqlBuilder;
12+
13+
class DiffCommand extends Command
14+
{
15+
/**
16+
* The name and signature of the console command.
17+
* @var string
18+
*/
19+
protected $signature = 'doctrine:migrations:diff
20+
{--connection= : For a specific connection }
21+
{--filter-expression= : Tables which are filtered by Regular Expression.}';
22+
23+
/**
24+
* @var string
25+
*/
26+
protected $description = 'Generate a migration by comparing your current database to your mapping information.';
27+
28+
/**
29+
* Execute the console command.
30+
*
31+
* @param ConfigurationProvider $provider
32+
* @param ManagerRegistry $registry
33+
* @param SqlBuilder $builder
34+
* @param MigrationFileGenerator $generator
35+
*/
36+
public function fire(
37+
ConfigurationProvider $provider,
38+
ManagerRegistry $registry,
39+
SqlBuilder $builder,
40+
MigrationFileGenerator $generator
41+
) {
42+
$configuration = $provider->getForConnection($this->option('connection'));
43+
$em = $registry->getManager($this->option('connection'));
44+
$connection = $configuration->getConnection();
45+
46+
// Overrule the filter
47+
if ($filterExpr = $this->option('filter-expression')) {
48+
$connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpr);
49+
}
50+
51+
$fromSchema = $connection->getSchemaManager()->createSchema();
52+
$toSchema = $this->getSchemaProvider($em)->createSchema();
53+
54+
// Drop tables which don't suffice to the filter regex
55+
if ($filterExpr = $connection->getConfiguration()->getFilterSchemaAssetsExpression()) {
56+
foreach ($toSchema->getTables() as $table) {
57+
$tableName = $table->getName();
58+
if (!preg_match($filterExpr, $this->resolveTableName($tableName))) {
59+
$toSchema->dropTable($tableName);
60+
}
61+
}
62+
}
63+
64+
$up = $builder->up($configuration, $fromSchema, $toSchema);
65+
$down = $builder->down($configuration, $fromSchema, $toSchema);
66+
67+
if (!$up && !$down) {
68+
return $this->error('No changes detected in your mapping information.');
69+
}
70+
71+
$path = $generator->generate(
72+
$configuration,
73+
false,
74+
false,
75+
$up,
76+
$down
77+
);
78+
79+
$this->line(sprintf('Generated new migration class to "<info>%s</info>" from schema differences.', $path));
80+
}
81+
82+
/**
83+
* @param EntityManagerInterface $em
84+
*
85+
* @return OrmSchemaProvider
86+
*/
87+
protected function getSchemaProvider(EntityManagerInterface $em)
88+
{
89+
return new OrmSchemaProvider($em);
90+
}
91+
92+
/**
93+
* Resolve a table name from its fully qualified name. The `$name` argument
94+
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
95+
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
96+
* the table name from that.
97+
*
98+
* @param string $name
99+
*
100+
* @return string
101+
*/
102+
protected function resolveTableName($name)
103+
{
104+
$pos = strpos($name, '.');
105+
106+
return false === $pos ? $name : substr($name, $pos + 1);
107+
}
108+
}

src/Console/ExecuteCommand.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace LaravelDoctrine\Migrations\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Console\ConfirmableTrait;
7+
use LaravelDoctrine\Migrations\Configuration\ConfigurationProvider;
8+
use LaravelDoctrine\Migrations\Migrator;
9+
10+
class ExecuteCommand extends Command
11+
{
12+
use ConfirmableTrait;
13+
14+
/**
15+
* The name and signature of the console command.
16+
* @var string
17+
*/
18+
protected $signature = 'doctrine:migrations:execute {version : The version to execute }
19+
{--connection= : For a specific connection.}
20+
{--write-sql : The path to output the migration SQL file instead of executing it. }
21+
{--dry-run : Execute the migration as a dry run. }
22+
{--up : Execute the migration up. }
23+
{--down : Execute the migration down. }
24+
{--query-time : Time all the queries individually.}';
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $description = 'Execute a single migration version up or down manually.';
30+
31+
/**
32+
* Execute the console command.
33+
*
34+
* @param ConfigurationProvider $provider
35+
* @param Migrator $migrator
36+
*/
37+
public function fire(ConfigurationProvider $provider, Migrator $migrator)
38+
{
39+
if (!$this->confirmToProceed()) {
40+
return;
41+
}
42+
43+
$configuration = $provider->getForConnection(
44+
$this->option('connection')
45+
);
46+
47+
$version = $this->argument('version');
48+
$direction = $this->option('down') ? 'down' : 'up';
49+
50+
$version = $configuration->getVersion($version);
51+
52+
if ($path = $this->option('write-sql')) {
53+
$migrator->executeToFile($version, $direction, $path);
54+
} else {
55+
$migrator->execute($version, $direction, $this->option('dry-run'), $this->option('query-time'));
56+
}
57+
58+
foreach ($migrator->getNotes() as $note) {
59+
$this->line($note);
60+
}
61+
}
62+
}

src/Console/GenerateCommand.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GenerateCommand extends Command
1212
* The name and signature of the console command.
1313
* @var string
1414
*/
15-
protected $signature = 'doctrine:migrations:generate {name}
15+
protected $signature = 'doctrine:migrations:generate
1616
{--create= : The table to be created.}
1717
{--table= : The table to migrate.}';
1818

@@ -32,10 +32,9 @@ public function fire(ConfigurationProvider $provider, MigrationFileGenerator $ge
3232
$configuration = $provider->getForConnection();
3333

3434
$filename = $generator->generate(
35-
$this->argument('name'),
35+
$configuration,
3636
$this->option('create'),
37-
$this->option('table'),
38-
$configuration
37+
$this->option('table')
3938
);
4039

4140
$this->line(sprintf('<info>Created Migration:</info> %s', $filename));

src/Console/LatestCommand.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace LaravelDoctrine\Migrations\Console;
4+
5+
use Illuminate\Console\Command;
6+
use LaravelDoctrine\Migrations\Configuration\ConfigurationProvider;
7+
8+
class LatestCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
* @var string
13+
*/
14+
protected $signature = 'doctrine:migrations:latest
15+
{--connection= : For a specific connection.}';
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $description = 'Outputs the latest version number';
21+
22+
/**
23+
* Execute the console command.
24+
*
25+
* @param ConfigurationProvider $provider
26+
*/
27+
public function fire(ConfigurationProvider $provider)
28+
{
29+
$configuration = $provider->getForConnection(
30+
$this->option('connection')
31+
);
32+
33+
$this->line('<info>Latest version:</info> ' . $configuration->getLatestVersion());
34+
}
35+
}

0 commit comments

Comments
 (0)