Skip to content

Commit

Permalink
Allow a way to insert demo data only for empty table.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 13, 2020
1 parent da812b9 commit 5d7556a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
17 changes: 16 additions & 1 deletion docs/en/seeding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ within your seed class and then use the `insert()` method to insert data:
],[
'body' => 'bar',
'created' => date('Y-m-d H:i:s'),
]
],
];
$posts = $this->table('posts');
Expand Down Expand Up @@ -193,6 +193,21 @@ Then use it in your seed classes:
}
}
Executing seeds only once
-------------------------

If you want to make sure your seed data doesn't get added multiple times, a
basic check on if the table is empty or contains already some data can help.

.. code-block:: php
public function run()
{
if ($this->hasData('posts')) {
return;
}
...
}
Truncating Tables
-----------------

Expand Down
20 changes: 15 additions & 5 deletions src/Phinx/Seed/AbstractSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,21 @@ public function fetchAll($sql)
*/
public function insert($table, $data)
{
// convert to table object
if (is_string($table)) {
$table = new Table($table, [], $this->getAdapter());
}
$table->insert($data)->save();
$tableInstance = new Table($table, [], $this->getAdapter());
$tableInstance->insert($data)->save();
}

/**
* @param string $tableName
*
* @return bool
*/
public function hasData(string $tableName): bool
{
$countQuery = $this->getAdapter()->query('SELECT COUNT(*) FROM ' . $tableName);
$res = $countQuery->fetchAll();

return $res[0]['count'] > 0;
}

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/Phinx/Seed/SeedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Test\Phinx\Seed;

use PDOStatement;
use Phinx\Db\Adapter\PdoAdapter;
use Phinx\Seed\AbstractSeed;
use PHPUnit\Framework\TestCase;

class SeedTest extends TestCase
{
/**
* @return void
*/
public function testHasData(): void
{
$queryStub = $this->getMockBuilder(PDOStatement::class)->disableOriginalConstructor()->getMock();
$queryStub->expects($this->once())
->method('fetchAll')
->will($this->returnValue([0 => ['count' => 0]]));

$adapterStub = $this->getMockBuilder(PdoAdapter::class)
->setConstructorArgs([[]])
->getMock();
$adapterStub->expects($this->once())
->method('query')
->will($this->returnValue($queryStub));

$stub = $this->getMockForAbstractClass(AbstractSeed::class);
$stub->setAdapter($adapterStub);
$result = $stub->hasData('foo');

$this->assertFalse($result);
}

/**
* @return void
*/
public function testHasDataTrue(): void
{
$queryStub = $this->getMockBuilder(PDOStatement::class)->disableOriginalConstructor()->getMock();
$queryStub->expects($this->once())
->method('fetchAll')
->will($this->returnValue([0 => ['count' => 1]]));

$adapterStub = $this->getMockBuilder(PdoAdapter::class)
->setConstructorArgs([[]])
->getMock();
$adapterStub->expects($this->once())
->method('query')
->will($this->returnValue($queryStub));

$stub = $this->getMockForAbstractClass(AbstractSeed::class);
$stub->setAdapter($adapterStub);
$result = $stub->hasData('foo');

$this->assertTrue($result);
}
}

0 comments on commit 5d7556a

Please sign in to comment.