Skip to content

Commit

Permalink
Merge pull request #98 from phalcon/#94-fix-engine-declaration
Browse files Browse the repository at this point in the history
#94 - Fixed engine and another table options declaration
  • Loading branch information
Jeckerson authored Aug 31, 2020
2 parents 0046c5b + c3ec322 commit 286988c
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# [2.1.4](https://github.com/phalcon/migrations/releases/tag/v2.1.4) (2020-08-31)
- Fixed 'options' table definition ([#94](https://github.com/phalcon/migrations/issues/94))

# [2.1.3](https://github.com/phalcon/migrations/releases/tag/v2.1.3) (2020-08-29)
- Improved tests codebase ([#86](https://github.com/phalcon/migrations/issues/86))
- Fixed `.phar` compilation ([#91](https://github.com/phalcon/migrations/issues/85))
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ composer require --dev phalcon/migrations

What you need for quick start:

* Configuration file in root of your project (you can also pass them as parameters inside CLI environment)
* Configuration file (ex: `migrations.php`) in root of your project (you can also pass them as parameters inside CLI environment)
* Create database tables structure
* Execute command to generate migrations

After that you can execute that migrations (run) in another environment to create same DB structure.

### Create configuration file

Configuration filename can be whatever you want.

```php
<?php

Expand Down Expand Up @@ -72,12 +74,22 @@ return new Config([
vendor/bin/phalcon-migrations generate
```

Or if you have ready to use configuration file.
```bash
vendor/bin/phalcon-migrations generate --config=migrations.php
```

### Run migrations

```bash
vendor/bin/phalcon-migrations run
```

Or if you have ready to use configuration file.
```bash
vendor/bin/phalcon-migrations run --config=migrations.php
```

### List existing migrations

```bash
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
bootstrap: %rootDir%/../../../vendor/autoload.php
bootstrapFiles:
- %rootDir%/../../../vendor/autoload.php

ignoreErrors:
- '#Parameter \#1 \$eventsManager of method Phalcon#'
Expand Down
6 changes: 5 additions & 1 deletion src/Migration/Action/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ public function getOptions(bool $skipAI): array
{
$options = [];
foreach ($this->options as $name => $value) {
if ($skipAI && strtoupper($name) == 'AUTO_INCREMENT') {
/**
* All options keys must be UPPERCASE!
*/
$name = strtoupper($name);
if ($skipAI && $name == 'AUTO_INCREMENT') {
$value = '';
}

Expand Down
6 changes: 3 additions & 3 deletions tests/_data/issues/76/1.0.0/user_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public function morph()
new Index('PRIMARY', ['user_id'], 'PRIMARY')
],
'options' => [
'table_type' => 'BASE TABLE',
'engine' => 'InnoDB',
'table_collation' => 'latin1_swedish_ci'
'TABLE_TYPE' => 'BASE TABLE',
'ENGINE' => 'InnoDB',
'TABLE_COLLATION' => 'latin1_swedish_ci'
],
]);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/_data/issues/94/1.0.0/memory_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class MemoryTableMigration_100 extends Migration
{
/**
* Define the table structure
*
* @return void
* @throws \Phalcon\Db\Exception
*/
public function morph()
{
$this->morphTable('memory_table', [
'columns' => [
new Column(
'id',
[
'type' => Column::TYPE_INTEGER,
'notNull' => true,
'autoIncrement' => true,
'size' => 11,
'first' => true
]
),
],
'indexes' => [
new Index('PRIMARY', ['id'], 'PRIMARY')
],
'options' => [
'TABLE_TYPE' => 'BASE TABLE',
'ENGINE' => 'MEMORY',
'TABLE_COLLATION' => 'latin1_swedish_ci'
],
]);
}
}
89 changes: 89 additions & 0 deletions tests/mysql/Issue94Cest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Phalcon\Migrations\Tests\Mysql;

use MysqlTester;
use Phalcon\Db\Column;
use Phalcon\Db\Exception;
use Phalcon\Db\Index;
use Phalcon\Migrations\Migrations;

/**
* @see https://github.com/phalcon/migrations/issues/94
*/
final class Issue94Cest
{
/**
* @param MysqlTester $I
* @throws Exception
*/
public function testIssue94(MysqlTester $I): void
{
$I->wantToTest('Issue #94 - Engine "MEMORY"');

ob_start();
Migrations::run([
'migrationsDir' => codecept_data_dir('issues/94'),
'config' => $I->getMigrationsConfig(),
'migrationsInDb' => true,
]);
ob_clean();

$options = $I->getPhalconDb()->tableOptions('memory_table');

$I->assertSame('MEMORY', $options['engine']);
}

/**
* @param MysqlTester $I
* @throws Exception
*/
public function testGenerateIssue94(MysqlTester $I): void
{
$I->wantToTest('Issue #94 - Correct options generation case (uppercase)');

$engine = 'MyISAM';
$tableName = 'options_uppercase';
$migrationsDir = codecept_output_dir(__FUNCTION__);

$I->getPhalconDb()->createTable($tableName, '', [
'columns' => [
new Column('id', [
'type' => Column::TYPE_INTEGER,
'size' => 20,
'notNull' => true,
'autoIncrement' => true,
]),
],
'indexes' => [
new Index('PRIMARY', ['id'], 'PRIMARY')
],
'options' => [
'TABLE_TYPE' => 'BASE TABLE',
'ENGINE' => $engine,
'TABLE_COLLATION' => 'utf8mb4_general_ci',
],
]);

/**
* Generate | Drop | Run
*/
ob_start();
Migrations::generate([
'migrationsDir' => $migrationsDir,
'config' => $I->getMigrationsConfig(),
'tableName' => $tableName,
]);
$I->getPhalconDb()->dropTable($tableName);
Migrations::run([
'migrationsDir' => $migrationsDir,
'config' => $I->getMigrationsConfig(),
'migrationsInDb' => true,
]);
ob_clean();

$I->assertSame($engine, $I->getPhalconDb()->tableOptions($tableName)['engine']);
}
}
4 changes: 2 additions & 2 deletions tests/mysql/MigrationsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function generateWithAutoIncrement(MysqlTester $I): void

$I->assertEquals(4, $autoIncrement);
$I->assertContains(
"'auto_increment' => '4'",
"'AUTO_INCREMENT' => '4'",
file_get_contents($migrationsDir . '/1.0.0/' . $tableName . '.php')
);
}
Expand Down Expand Up @@ -323,7 +323,7 @@ public function generateWithoutAutoIncrement(MysqlTester $I): void

$I->assertEquals(4, $autoIncrement);
$I->assertContains(
"'auto_increment' => ''",
"'AUTO_INCREMENT' => ''",
file_get_contents($migrationsDir . '/1.0.0/' . $tableName . '.php')
);
}
Expand Down

0 comments on commit 286988c

Please sign in to comment.