Skip to content

Commit 85a6bc4

Browse files
authored
Fix snapshots for plugins. (#949)
* Fix snapshots for plugins. * Fixes.
1 parent c705be2 commit 85a6bc4

10 files changed

+2507
-1
lines changed

src/Util/TableFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getTablesToBake(CollectionInterface $collection, array $options
6969
return $tables;
7070
}
7171

72-
if ($options['require-table'] === true || $options['plugin']) {
72+
if ($options['require-table'] === true) {
7373
$tableNamesInPlugin = $this->getTableNames($options['plugin']);
7474

7575
if (!$tableNamesInPlugin) {

tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,34 @@ public function testPluginBlog()
217217
$this->runSnapshotTest('PluginBlog', '-p TestBlog');
218218
}
219219

220+
/**
221+
* Test baking a snapshot for a plugin with custom connection (issue #463).
222+
* This tests that when using both --plugin and --connection options,
223+
* the migration includes all tables from the connection, not just those
224+
* with Table classes in the plugin.
225+
*
226+
* @return void
227+
*/
228+
public function testPluginWithCustomConnection()
229+
{
230+
$this->loadPlugins(['SimpleSnapshot']);
231+
$this->migrationPath = ROOT . DS . 'Plugin' . DS . 'SimpleSnapshot' . DS . 'config' . DS . 'Migrations' . DS;
232+
233+
$bakeName = $this->getBakeName('TestSnapshotPluginCustomConnection');
234+
$this->exec("bake migration_snapshot {$bakeName} -c test -p SimpleSnapshot");
235+
236+
$generatedMigration = glob($this->migrationPath . '*_TestSnapshotPluginCustomConnection*.php');
237+
$this->generatedFiles = $generatedMigration;
238+
$this->generatedFiles[] = $this->migrationPath . 'schema-dump-test.lock';
239+
240+
$this->assertNotEmpty($generatedMigration, 'Migration file should be generated');
241+
242+
$content = file_get_contents($generatedMigration[0]);
243+
$this->assertStringContainsString('function up()', $content);
244+
$this->assertStringNotContainsString('public function up(): void {}', $content, 'up() method should not be empty');
245+
$this->assertStringContainsString('->create()', $content, 'Migration should contain table creation statements');
246+
}
247+
220248
protected function runSnapshotTest(string $scenario, string $arguments = ''): void
221249
{
222250
if ($arguments) {

tests/comparisons/Migration/pgsql/test_snapshot_plugin_blog_pgsql.php

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,57 @@ public function up(): void
104104
)
105105
->create();
106106

107+
$this->table('composite_pks', ['id' => false, 'primary_key' => ['id', 'name']])
108+
->addColumn('id', 'uuid', [
109+
'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7',
110+
'limit' => null,
111+
'null' => false,
112+
])
113+
->addColumn('name', 'string', [
114+
'default' => '',
115+
'limit' => 10,
116+
'null' => false,
117+
])
118+
->create();
119+
120+
$this->table('events')
121+
->addColumn('title', 'string', [
122+
'default' => null,
123+
'limit' => null,
124+
'null' => true,
125+
])
126+
->addColumn('description', 'text', [
127+
'default' => null,
128+
'limit' => null,
129+
'null' => true,
130+
])
131+
->addColumn('published', 'string', [
132+
'default' => 'N',
133+
'limit' => 1,
134+
'null' => true,
135+
])
136+
->create();
137+
138+
$this->table('orders')
139+
->addColumn('product_category', 'integer', [
140+
'default' => null,
141+
'limit' => 10,
142+
'null' => false,
143+
])
144+
->addColumn('product_id', 'integer', [
145+
'default' => null,
146+
'limit' => 10,
147+
'null' => false,
148+
])
149+
->addIndex(
150+
$this->index([
151+
'product_category',
152+
'product_id',
153+
])
154+
->setName('orders_product_category_idx')
155+
)
156+
->create();
157+
107158
$this->table('parts')
108159
->addColumn('name', 'string', [
109160
'default' => null,
@@ -117,6 +168,143 @@ public function up(): void
117168
])
118169
->create();
119170

171+
$this->table('products')
172+
->addColumn('title', 'string', [
173+
'default' => null,
174+
'limit' => 255,
175+
'null' => true,
176+
])
177+
->addColumn('slug', 'string', [
178+
'default' => null,
179+
'limit' => 100,
180+
'null' => true,
181+
])
182+
->addColumn('category_id', 'integer', [
183+
'default' => null,
184+
'limit' => 10,
185+
'null' => true,
186+
])
187+
->addColumn('created', 'timestamp', [
188+
'default' => null,
189+
'limit' => null,
190+
'null' => true,
191+
'precision' => 6,
192+
'scale' => 6,
193+
])
194+
->addColumn('modified', 'timestamp', [
195+
'default' => null,
196+
'limit' => null,
197+
'null' => true,
198+
'precision' => 6,
199+
'scale' => 6,
200+
])
201+
->addIndex(
202+
$this->index([
203+
'id',
204+
'category_id',
205+
])
206+
->setName('products_category_unique')
207+
->setType('unique')
208+
)
209+
->addIndex(
210+
$this->index('slug')
211+
->setName('products_slug_unique')
212+
->setType('unique')
213+
)
214+
->addIndex(
215+
$this->index('title')
216+
->setName('products_title_idx')
217+
)
218+
->create();
219+
220+
$this->table('special_pks', ['id' => false, 'primary_key' => ['id']])
221+
->addColumn('id', 'uuid', [
222+
'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7',
223+
'limit' => null,
224+
'null' => false,
225+
])
226+
->addColumn('name', 'string', [
227+
'default' => null,
228+
'limit' => 256,
229+
'null' => true,
230+
])
231+
->create();
232+
233+
$this->table('special_tags')
234+
->addColumn('article_id', 'integer', [
235+
'default' => null,
236+
'limit' => 10,
237+
'null' => false,
238+
])
239+
->addColumn('author_id', 'integer', [
240+
'default' => null,
241+
'limit' => 10,
242+
'null' => true,
243+
])
244+
->addColumn('tag_id', 'integer', [
245+
'default' => null,
246+
'limit' => 10,
247+
'null' => false,
248+
])
249+
->addColumn('highlighted', 'boolean', [
250+
'default' => null,
251+
'limit' => null,
252+
'null' => true,
253+
])
254+
->addColumn('highlighted_time', 'timestamp', [
255+
'default' => null,
256+
'limit' => null,
257+
'null' => true,
258+
'precision' => 6,
259+
'scale' => 6,
260+
])
261+
->addIndex(
262+
$this->index('article_id')
263+
->setName('special_tags_article_unique')
264+
->setType('unique')
265+
)
266+
->create();
267+
268+
$this->table('texts', ['id' => false])
269+
->addColumn('title', 'string', [
270+
'default' => null,
271+
'limit' => null,
272+
'null' => true,
273+
])
274+
->addColumn('description', 'text', [
275+
'default' => null,
276+
'limit' => null,
277+
'null' => true,
278+
])
279+
->create();
280+
281+
$this->table('users')
282+
->addColumn('username', 'string', [
283+
'default' => null,
284+
'limit' => 256,
285+
'null' => true,
286+
])
287+
->addColumn('password', 'string', [
288+
'default' => null,
289+
'limit' => 256,
290+
'null' => true,
291+
])
292+
->addColumn('created', 'timestamp', [
293+
'default' => null,
294+
'limit' => null,
295+
'null' => true,
296+
'precision' => 6,
297+
'scale' => 6,
298+
])
299+
->addColumn('updated', 'timestamp', [
300+
'default' => null,
301+
'limit' => null,
302+
'null' => true,
303+
'precision' => 6,
304+
'scale' => 6,
305+
])
306+
->create();
307+
120308
$this->table('articles')
121309
->addForeignKey(
122310
$this->foreignKey('category_id')
@@ -127,6 +315,34 @@ public function up(): void
127315
->setName('articles_category_fk')
128316
)
129317
->update();
318+
319+
$this->table('orders')
320+
->addForeignKey(
321+
$this->foreignKey([
322+
'product_category',
323+
'product_id',
324+
])
325+
->setReferencedTable('products')
326+
->setReferencedColumns([
327+
'category_id',
328+
'id',
329+
])
330+
->setOnDelete('CASCADE')
331+
->setOnUpdate('CASCADE')
332+
->setName('orders_product_fk')
333+
)
334+
->update();
335+
336+
$this->table('products')
337+
->addForeignKey(
338+
$this->foreignKey('category_id')
339+
->setReferencedTable('categories')
340+
->setReferencedColumns('id')
341+
->setOnDelete('CASCADE')
342+
->setOnUpdate('CASCADE')
343+
->setName('products_category_fk')
344+
)
345+
->update();
130346
}
131347

132348
/**
@@ -144,8 +360,29 @@ public function down(): void
144360
'category_id'
145361
)->save();
146362

363+
$this->table('orders')
364+
->dropForeignKey(
365+
[
366+
'product_category',
367+
'product_id',
368+
]
369+
)->save();
370+
371+
$this->table('products')
372+
->dropForeignKey(
373+
'category_id'
374+
)->save();
375+
147376
$this->table('articles')->drop()->save();
148377
$this->table('categories')->drop()->save();
378+
$this->table('composite_pks')->drop()->save();
379+
$this->table('events')->drop()->save();
380+
$this->table('orders')->drop()->save();
149381
$this->table('parts')->drop()->save();
382+
$this->table('products')->drop()->save();
383+
$this->table('special_pks')->drop()->save();
384+
$this->table('special_tags')->drop()->save();
385+
$this->table('texts')->drop()->save();
386+
$this->table('users')->drop()->save();
150387
}
151388
}

0 commit comments

Comments
 (0)