Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/Migration/Action/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,7 @@ protected function getColumnSize(ColumnInterface $column): null|int|string
$this->adapter === Migration::DB_ADAPTER_MYSQL &&
in_array($columnType, $this->numericColumnTypes)
) {
return $columnsSize ?: match ($columnType) {
Column::TYPE_BIGINTEGER => 8,
Column::TYPE_INTEGER => 4,
Column::TYPE_MEDIUMINTEGER => 3,
Column::TYPE_SMALLINTEGER => 2,
Column::TYPE_TINYINTEGER => 1,
Column::TYPE_DECIMAL => null
};
return $columnsSize ?: null;
}

/**
Expand Down
13 changes: 11 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ vendor/bin/codecept run

Currently, you will need configured MySQL and PostgreSQL databases.

* Copy .env.example file: `cp -p tests/.env.example tests/.env`
* Copy .env.example file:
```bash
cp -p tests/.env.example tests/.env
```
* Edit credentials to the databases
* Create manually databases (if needed)
* Create manually databases and users (if needed) or use these commands matching default ones:

```bash
docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=phalcon-migrations -p 3306:3306 -d mysql:8.0 --default-authentication-plugin=mysql_native_password
docker run --name postgres-container -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -p 5432:5432 -d postgres:latest
docker exec -it postgres-container psql -U postgres -d postgres -c "CREATE SCHEMA migrations;"
```

## Help

Expand Down
63 changes: 59 additions & 4 deletions tests/cli/RunCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,37 @@ public function generateAndRun(CliTester $I): void
}

/**
* @skip Some strange behavior with MySQL Server in Github Actions. Back to this test in some future
*
* @param CliTester $I
*/
public function expectForeignKeyDbError(CliTester $I): void
public function expectForeignKeyDbError1822(CliTester $I): void
{
$table1 = 'z-client';
$table2 = 'skip-foreign-keys';

$this->createTablesWithForeignKey($I, $table1, $table2);

$I->runShellCommand('php phalcon-migrations generate --config=' . $this->configPath);
$I->seeInShellOutput('Success: Version 1.0.0 was successfully generated');
$I->seeResultCodeIs(0);

$migrationContent = file_get_contents(codecept_output_dir('1.0.0/' . $table2 . '.php'));
$I->assertNotFalse(strpos($migrationContent, "'referencedTable' => '" . $table1 . "',"));

$I->getPhalconDb()
->dropTable($table2)
;
$schema = $_ENV['MYSQL_TEST_DB_DATABASE'];
$I->getPhalconDb()->dropPrimaryKey($table1, $schema);

$I->runShellCommand('php phalcon-migrations run --config=' . $this->configPath, false);
$I->seeInShellOutput('SQLSTATE[HY000]: General error: 1822 Failed to add the foreign key constraint');
$I->seeResultCodeIs(1);
}

/**
* @param CliTester $I
*/
public function expectForeignKeyDbError1824(CliTester $I): void
{
$table1 = 'z-client';
$table2 = 'skip-foreign-keys';
Expand All @@ -90,7 +116,36 @@ public function expectForeignKeyDbError(CliTester $I): void
;

$I->runShellCommand('php phalcon-migrations run --config=' . $this->configPath, false);
$I->seeInShellOutput('Fatal Error: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint');
$I->seeInShellOutput('SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table');
$I->seeResultCodeIs(1);
}

/**
* @param CliTester $I
*/
public function expectForeignKeyDbError3734(CliTester $I): void
{
$table1 = 'z-client';
$table2 = 'skip-foreign-keys';

$this->createTablesWithForeignKey($I, $table1, $table2);

$I->runShellCommand('php phalcon-migrations generate --config=' . $this->configPath);
$I->seeInShellOutput('Success: Version 1.0.0 was successfully generated');
$I->seeResultCodeIs(0);

$migrationContent = file_get_contents(codecept_output_dir('1.0.0/' . $table2 . '.php'));
$I->assertNotFalse(strpos($migrationContent, "'referencedTable' => '" . $table1 . "',"));

$I->getPhalconDb()
->dropTable($table2)
;
$schema = $_ENV['MYSQL_TEST_DB_DATABASE'];
$I->getPhalconDb()->addColumn($table1, $schema, new Column('stub', ['type' => Column::TYPE_INTEGER]));
$I->getPhalconDb()->dropColumn($table1, $schema, 'id');

$I->runShellCommand('php phalcon-migrations run --config=' . $this->configPath, false);
$I->seeInShellOutput('SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint');
$I->seeResultCodeIs(1);
}

Expand Down
98 changes: 89 additions & 9 deletions tests/mysql/ColumnTypesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function columnsDataProvider(): array
{
return [
[
'column_int',
'column_uint',
[
'type' => Column::TYPE_INTEGER,
'size' => 10,
Expand All @@ -44,6 +44,13 @@ protected function columnsDataProvider(): array
],
[0, 1, 123, 9000],
],
[
'column_bigint',
[
'type' => Column::TYPE_BIGINTEGER,
],
[PHP_INT_MIN, PHP_INT_MIN + 1, 0, PHP_INT_MAX - 1, PHP_INT_MAX],
],
[
'column_int_primary',
[
Expand All @@ -52,8 +59,78 @@ protected function columnsDataProvider(): array
'first' => true,
'primary' => true,
],
[-2147483648, 0, 2147483647],
],
[
'column_mediumint_size',
[
'type' => Column::TYPE_MEDIUMINTEGER,
'size' => 1,
],
[8388607, 0, -8388608],
],
[
'column_mediumint',
[
'type' => Column::TYPE_MEDIUMINTEGER,
],
[8388607, 0, -8388608],
],
[
'column_mediumint_small_display_size',
[
'type' => Column::TYPE_MEDIUMINTEGER,
'size' => 1,
],
[8388607, 0, -8388608],
],
[
'column_medium_uint',
[
'type' => Column::TYPE_MEDIUMINTEGER,
'size' => 123,
'unsigned' => true,
],
[16777215, 0],
],
[
'column_smallint',
[
'type' => Column::TYPE_SMALLINTEGER,
'size' => 11,
'first' => true,
'primary' => true,
],
[1, 2, 3, 4],
],
[
'column_tinyint_big_display_size',
[
'type' => Column::TYPE_TINYINTEGER,
'size' => 255,
'first' => true,
'primary' => true,
],
[-128, 0, 127],
],
[
'column_tiny_uint',
[
'type' => Column::TYPE_TINYINTEGER,
'unsigned' => true,
],
[255, 0],
],
[
'column_bigint_primary',
[
'type' => Column::TYPE_BIGINTEGER,
'size' => 7,
'first' => true,
'primary' => true,
],
[PHP_INT_MIN, PHP_INT_MIN + 1, 0, PHP_INT_MAX - 1, PHP_INT_MAX],
],
[
'column_int_pri_inc',
[
Expand Down Expand Up @@ -143,20 +220,23 @@ public function columnDefinition(MysqlTester $I, Example $example): void
]);
ob_clean();

/**
* Insert values
*/
foreach ($values as $value) {
$I->getPhalconDb()->insert($tableName, [$value], [$columnName]);
try {
/**
* Insert values
*/
foreach ($values as $value) {
$I->getPhalconDb()->insert($tableName, [$value], [$columnName]);
}
} finally {
Migrations::resetStorage();
$I->removeDir($migrationsDir);
}

Migrations::resetStorage();
$I->removeDir($migrationsDir);

/** @var Column $column */
$column = $I->getPhalconDb()->describeColumns($tableName)[0];
$rows = $I->grabColumnFromDatabase($tableName, $columnName);

$I->assertSame($definition['unsigned'] ?? false, $column->isUnsigned());
$I->assertSame($definition['type'], $column->getType());
$I->assertSame($definition['notNull'] ?? true, $column->isNotNull());
$I->assertEquals($values, $rows);
Expand Down