Skip to content

Commit 4fd8528

Browse files
Allow run migrations without truncating (#24) (#25)
* Allow run migrations without truncating (#24) * Allow run migrations without truncating * Truncate all connections by default * Update Documentation * Require migration plugin version ^3.1 Co-authored-by: Raúl Arellano <[email protected]>
1 parent 280abc2 commit 4fd8528

File tree

4 files changed

+88
-18
lines changed

4 files changed

+88
-18
lines changed

README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ With the CakePHP Test Migrator, the schema of both default and test DB are handl
2020

2121
The package proposes a tool to run your [migrations](https://book.cakephp.org/migrations/3/en/index.html) once prior to the tests. In order to do so,
2222
you may place the following in your `tests/bootstrap.php`:
23-
```$xslt
23+
```php
2424
\CakephpTestMigrator\Migrator::migrate();
2525
```
2626
This command will ensure that your migrations are well run and keeps the test DB(s) up to date. Since tables are truncated but never dropped by the present package's fixture manager, migrations will be run strictly when needed, namely only after a new migration was created by the developer.
@@ -30,23 +30,38 @@ The `Migrator`approach presents the following advantages:
3030
* it eases the maintenance of your tests, since regular and test DBs are managed the same way,
3131
* it indirectly tests your migrations.
3232

33-
You may pass `true` as the second argument for a verbose output on the console.
33+
You may pass `true` as the second argument for a verbose output on the console.
34+
35+
### Options
36+
37+
| name | type | default | description |
38+
| ---- | ---- | ------- | ----------- |
39+
| verbose | *bool* | `false` | Print info about migrations done |
40+
| truncate | *bool* | `true` | Truncate all tables after migrations are done. You can call `truncate()` manually |
41+
42+
You can pass a boolean as options, it will be used as `verbose`
43+
44+
```php
45+
\CakephpTestMigrator\Migrator::migrate([], true);
46+
// is the same as
47+
\CakephpTestMigrator\Migrator::migrate([], ['verbose' => true]);
48+
```
3449

3550
### Multiple migrations settings
3651

3752
You can pass the various migrations directly in the Migrator instantiation:
38-
```$xslt
53+
```php
3954
\CakephpTestMigrator\Migrator::migrate([
4055
['connection' => 'test', 'source' => 'TestFolder'],
4156
['plugin' => 'FooPlugin', 'connection' => 'FooConnection'],
4257
['source' => 'BarFolder'],
4358
...
44-
], true);
59+
], ['verbose' => true]);
4560
```
4661

4762
You can also pass the various migrations directly in your Datasource configuration, under the key `migrations`:
48-
```$xslt
49-
In config/app.php
63+
```php
64+
// In config/app.php
5065
'test' => [
5166
'className' => Connection::class,
5267
'driver' => Mysql::class,
@@ -68,14 +83,24 @@ You can set `migrations` simply to `true` if you which to use the default migrat
6883
### Migrations status
6984

7085
Information on a connection's migration status will be obtained as follows:
71-
```
86+
```php
7287
$migrator = Migrator::migrate();
7388
$connectionsWithModifiedStatus = $migrator->getConnectionsWithModifiedStatus();
7489
```
7590

7691
the method `getConnectionsWithModifiedStatus` returning a list of the connections with down
7792
migrations prior to running the migrations.
7893

94+
### Data truncation
95+
96+
Use **truncate** option to truncate (or not) after migrations are done. This is useful if you need to do other tasks in the test suite bootstrap
97+
98+
```php
99+
$migrator = Migrator::migrate([], ['truncate' => false]);
100+
// do something with the migrated database (bake fixtures, etc)
101+
$migrator->truncate();
102+
```
103+
79104
### What happens if I switch branches?
80105

81106
If you ever switched to a branch with nonexistent up migrations, you've moved to a branch in a past state.
@@ -86,18 +111,18 @@ does not require any intervention on your side.
86111

87112
The `Migrator::dump()` will help you import any schema from one or several sql file. Run for example:
88113

89-
```$xslt
114+
```php
90115
Migrator::dump('test', 'path/to/file.sql')
91116
```
92117
or with multiples files
93-
```$xslt
118+
```php
94119
Migrator::dump('test', [
95120
'path1/to/file1.sql',
96121
'path2/to/file2.sql',
97122
])
98123
```
99124
or for a verbose output
100-
```$xslt
125+
```php
101126
Migrator::dump('test', 'path/to/file.sql', true)
102127
```
103128

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"require-dev": {
1616
"cakephp/cakephp-codesniffer": "^4.0",
17-
"cakephp/migrations": "^3.0",
17+
"cakephp/migrations": "^3.1",
1818
"josegonzalez/dotenv": "dev-master",
1919
"phpunit/phpunit": "^8.0"
2020
},

src/Migrator.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,33 @@ final public function __construct(bool $verbose, ?ConfigReader $configReader = n
5555
* General command to run before your tests run
5656
* E.g. in tests/bootstrap.php
5757
*
58+
* Options:
59+
* - verbose | bool | Set to true to display messages
60+
* - truncate | bool | Truncate tables after migrations are done.
61+
*
5862
* @param array $config
59-
* @param bool $verbose Set to true to display messages
63+
* @param array{verbose?:bool,truncate?:bool}|bool $options Options for migrations
6064
* @return Migrator
6165
*/
62-
public static function migrate(array $config = [], $verbose = false): Migrator
66+
public static function migrate(array $config = [], $options = []): Migrator
6367
{
64-
$migrator = new static($verbose);
68+
if ($options === true || $options === false) {
69+
$options = ['verbose' => $options];
70+
}
71+
$options += [
72+
'verbose' => false,
73+
'truncate' => true,
74+
];
75+
$migrator = new static($options['verbose']);
6576

6677
$migrator->configReader->readMigrationsInDatasources();
6778
$migrator->configReader->readConfig($config);
6879
$migrator->handleMigrationsStatus();
6980

81+
if ($options['truncate']) {
82+
$migrator->truncate();
83+
}
84+
7085
return $migrator;
7186
}
7287

@@ -129,6 +144,23 @@ protected function runMigrations(array $config): void
129144
}
130145
}
131146

147+
/**
148+
* Truncates connections on demand.
149+
*
150+
* @param string[]|null $connections Connections names to truncate. Defaults to modified connections
151+
* @return void
152+
*/
153+
public function truncate(?array $connections = null): void
154+
{
155+
if ($connections === null) {
156+
$connections = $this->configReader->getActiveConnections();
157+
}
158+
$schemaCleaner = new SchemaCleaner($this->io);
159+
foreach ($connections as $connectionName) {
160+
$schemaCleaner->truncate($connectionName);
161+
}
162+
}
163+
132164
/**
133165
* If a migration is missing or down, all tables of the considered connection are dropped.
134166
*
@@ -163,10 +195,6 @@ protected function handleMigrationsStatus(): self
163195
$this->runMigrations($config);
164196
}
165197

166-
foreach ($this->connectionsWithModifiedStatus as $connectionName) {
167-
$schemaCleaner->truncate($connectionName);
168-
}
169-
170198
return $this;
171199
}
172200

tests/TestCase/MigratorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,21 @@ public function testDropTablesForMissingMigrations(): void
107107
$count = $connection->newQuery()->select('version')->from('phinxlog')->execute()->count();
108108
$this->assertSame(3, $count);
109109
}
110+
111+
public function testMigrateWithoutTruncate(): void
112+
{
113+
$connection = ConnectionManager::get('test');
114+
$cleaner = new SchemaCleaner();
115+
$cleaner->drop('test');
116+
117+
$migrator = Migrator::migrate([], ['truncate' => false]);
118+
119+
$count = $connection->newQuery()->select('title')->from('articles')->execute()->count();
120+
$this->assertSame(1, $count);
121+
122+
$migrator->truncate();
123+
124+
$count = $connection->newQuery()->select('title')->from('articles')->execute()->count();
125+
$this->assertSame(0, $count);
126+
}
110127
}

0 commit comments

Comments
 (0)