Skip to content

Commit

Permalink
#2 - Add test case with 'SET FOREIGN_KEY_CHECKS'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Feb 1, 2020
1 parent cbb6ac6 commit c3e8b94
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/Integration/Issue2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace Phalcon\Migrations\Tests\Integration;

use Phalcon\Migrations\Migrations;
use function Phalcon\Migrations\Tests\root_path;

class Issue2Test extends IntegrationTestCase
{
public function testDisableEnableForeignKeyChecks(): void
{
Migrations::run([
'migrationsDir' => root_path('tests/var/issues/2'),
'config' => self::$generateConfig,
'migrationsInDb' => true,
]);

$this->assertTrue($this->db->tableExists('accessToken'));
$this->assertTrue($this->db->tableExists('client'));
$this->assertArrayHasKey('fk_accessToken_client_1', $this->db->describeReferences('accessToken'));
}
}
90 changes: 90 additions & 0 deletions tests/var/issues/2/0.0.1/accessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Db\Reference;
use Phalcon\Migrations\Mvc\Model\Migration;

class AccessTokenMigration_1 extends Migration
{
/**
* Define the table structure
*
* @return void
* @throws \Phalcon\Db\Exception
*/
public function morph()
{
$this->getConnection()->execute('SET FOREIGN_KEY_CHECKS=0;');

$this->morphTable('accessToken', [
'columns' => [
new Column(
'token',
[
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 80,
'first' => true,
]
),
new Column(
'clientId',
[
'type' => Column::TYPE_VARCHAR,
'size' => 80,
'after' => 'token',
]
),
new Column(
'userId',
[
'type' => Column::TYPE_VARCHAR,
'size' => 80,
'after' => 'clientId',
]
),

],
'indexes' => [
new Index('accessToken_pkey', ['token'], 'PRIMARY'),
],
'references' => [
new Reference(
'fk_accessToken_client_1',
[
'referencedTable' => 'client',
'referencedSchema' => 'public',
'columns' => ['clientId'],
'referencedColumns' => ['id'],
'onUpdate' => 'NO ACTION',
'onDelete' => 'NO ACTION',
]
),
],
]
);

$this->getConnection()->execute('SET FOREIGN_KEY_CHECKS=1;');
}

/**
* Run the migrations
*
* @return void
*/
public function up()
{

}

/**
* Reverse the migrations
*
* @return void
*/
public function down()
{
$this->getConnection()->dropTable('accessToken');
}
}
64 changes: 64 additions & 0 deletions tests/var/issues/2/0.0.1/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Migrations\Mvc\Model\Migration;

class ClientMigration_1 extends Migration
{
/**
* Define the table structure
*
* @return void
* @throws \Phalcon\Db\Exception
*/
public function morph()
{
$this->morphTable('client', [
'columns' => [
new Column(
'id',
[
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 80,
'first' => true,
]
),
new Column(
'name',
[
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 80,
'first' => true,
]
),
],
'indexes' => [
new Index('client_pkey', ['id'], 'PRIMARY'),
],
]
);
}

/**
* Run the migrations
*
* @return void
*/
public function up()
{

}

/**
* Reverse the migrations
*
* @return void
*/
public function down()
{
$this->getConnection()->dropTable('client');
}
}

0 comments on commit c3e8b94

Please sign in to comment.