Skip to content
Snippets Groups Projects
Unverified Commit 7ba9b4c1 authored by Thomas Flori's avatar Thomas Flori
Browse files

add indexes to the migration table and test the migration

parent 7df371bc
No related branches found
No related tags found
No related merge requests found
......@@ -2,22 +2,12 @@
require_once __DIR__ . '/vendor/autoload.php';
$envFile = __DIR__ . '/.env';
if (file_exists($envFile)) {
$dotenv = new Symfony\Component\Dotenv\Dotenv();
$dotenv->load($envFile);
}
// connect to your database...
$dsn = 'mysql:host=127.0.0.1;dbname=testdb;port=3307';
$username = getenv('DB_USERNAME') ?: 'root';
$password = getenv('DB_PASSWORD') ?: 'password';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
$dsn = 'sqlite:/tmp/breyta_test.sq3';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);
$db = new PDO($dsn, $username, $password, $options);
];
$db = new PDO($dsn, '', '', $options);
$adapter = new Breyta\Adapter\BasicAdapter(function ($statement) use ($db) {
var_dump($statement);
......@@ -26,4 +16,3 @@ $adapter = new Breyta\Adapter\BasicAdapter(function ($statement) use ($db) {
$migration = new Breyta\Migration\CreateMigrationTable($adapter);
$migration->up();
......@@ -8,13 +8,17 @@ class CreateMigrationTable extends AbstractMigration
{
public function up(): void
{
$this->exec('CREATE TABLE migrations (
$this->exec("CREATE TABLE migrations (
file CHARACTER VARYING (64) NOT NULL,
executed TIMESTAMP NOT NULL,
status CHARACTER VARYING (16) NOT NULL DEFAULT \'done\',
status CHARACTER VARYING (16) NOT NULL DEFAULT 'done',
info TEXT,
executionTime DOUBLE PRECISION,
PRIMARY KEY (file)
)');
)");
$this->exec("CREATE INDEX migrations_executed_index ON migrations (executed)");
$this->exec("CREATE INDEX migrations_status_index ON migrations (status)");
$this->exec("CREATE INDEX migrations_execution_time ON migrations (executionTime)");
}
public function down(): void
......
<?php
namespace Breyta\Test\Migration;
use Breyta\Adapter\BasicAdapter;
use Breyta\Migration\CreateMigrationTable;
use Breyta\Test\TestCase;
use Mockery as m;
class CreateMigrationTableTest extends TestCase
{
/** @test */
public function createsATable()
{
$adapter = m::mock(BasicAdapter::class)->shouldIgnoreMissing();
$migration = new CreateMigrationTable($adapter);
$migration->up();
$adapter->shouldHaveReceived('exec')->with(m::pattern('/create table migrations/i'))->once();
}
/** @test */
public function createIndexesOnMigrationTable()
{
$adapter = m::mock(BasicAdapter::class)->shouldIgnoreMissing();
$migration = new CreateMigrationTable($adapter);
$migration->up();
$adapter->shouldHaveReceived('exec')
->with(m::pattern('/create index.* on migrations\s*\(\s*executed\s*\)/i'))
->once();
$adapter->shouldHaveReceived('exec')
->with(m::pattern('/create index.* on migrations\s*\(\s*status\s*\)/i'))
->once();
$adapter->shouldHaveReceived('exec')
->with(m::pattern('/create index.* on migrations\s*\(\s*executionTime\s*\)/i'))
->once();
}
/** @test */
public function dropsTheMigrationTable()
{
$adapter = m::mock(BasicAdapter::class)->shouldIgnoreMissing();
$migration = new CreateMigrationTable($adapter);
$migration->down();
$adapter->shouldHaveReceived('exec')->with(m::pattern('/drop table migrations/i'));
}
}
......@@ -11,11 +11,11 @@ class MigrationTest extends TestCase
/** @test */
public function executesTheStatementOnDb()
{
$pdo = m::mock(BasicAdapter::class);
$migration = new CreateAnimalsTable($pdo);
$adapter = m::mock(BasicAdapter::class);
$migration = new CreateAnimalsTable($adapter);
$pdo->shouldReceive('exec')->andReturn(true)->byDefault();
$pdo->shouldReceive('exec')->with(m::pattern('/^\s*create table animals\s*\(/i'))
$adapter->shouldReceive('exec')->andReturn(true)->byDefault();
$adapter->shouldReceive('exec')->with(m::pattern('/^\s*create table animals\s*\(/i'))
->once()->andReturn(true);
$migration->up();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment