Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[#13002] - Introducing migrations
  • Loading branch information
niden committed May 15, 2019
1 parent 2e29518 commit 816a152
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 33 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -51,6 +51,7 @@
"friendsofphp/php-cs-fixer": "~2.0",
"mustache/mustache": "^2.12",
"predis/predis": "^1.1",
"robmorgan/phinx": "^0.10.7",
"squizlabs/php_codesniffer": "^3.4",
"twig/twig": "~1.36",
"vlucas/phpdotenv": "^2.5"
Expand Down
16 changes: 8 additions & 8 deletions phalcon/Mvc/Model.zep
Expand Up @@ -3440,18 +3440,18 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
source, schema, snapshot, lastInsertedId, manager;
bool useExplicitIdentity;

let bindSkip = Column::BIND_SKIP;
let manager = <ManagerInterface> this->modelsManager;
let bindSkip = Column::BIND_SKIP,
manager = <ManagerInterface> this->modelsManager;

let fields = [],
values = [],
snapshot = [],
bindTypes = [];

let attributes = metaData->getAttributes(this),
bindDataTypes = metaData->getBindTypes(this),
let attributes = metaData->getAttributes(this),
bindDataTypes = metaData->getBindTypes(this),
automaticAttributes = metaData->getAutomaticCreateAttributes(this),
defaultValues = metaData->getDefaultValues(this);
defaultValues = metaData->getDefaultValues(this);

if globals_get("orm.column_renaming") {
let columnMap = metaData->getColumnMap(this);
Expand Down Expand Up @@ -3626,12 +3626,12 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
/**
* If we want auto casting
*/
if unlikely globals_get("orm.cast_last_insert_id_to_int") {
if lastInsertedId && globals_get("orm.cast_last_insert_id_to_int") {
let lastInsertedId = intval(lastInsertedId, 10);
}

let this->{attributeField} = lastInsertedId;
let snapshot[attributeField] = lastInsertedId;
let this->{attributeField} = lastInsertedId,
snapshot[attributeField] = lastInsertedId;

/**
* Since the primary key was modified, we delete the uniqueParams
Expand Down
36 changes: 36 additions & 0 deletions phinx.php
@@ -0,0 +1,36 @@
<?php

return [
'paths' => [
'migrations' => getenv('PHINX_CONFIG_DIR') . 'tests/_data/assets/db/migrations',
'seeds' => getenv('PHINX_CONFIG_DIR') . 'tests/_data/assets/db/seeds',
],
'environments' => [
'default_migration_table' => 'phinxlog',
'default_database' => 'mysql',
'mysql' => [
'adapter' => 'mysql',
'host' => getenv('DATA_MYSQL_HOST'),
'name' => 'gonano',
'user' => getenv('DATA_MYSQL_USER'),
'pass' => getenv('DATA_MYSQL_PASS'),
'port' => 3306,
'charset' => 'utf8',
],
'postgres' => [
'adapter' => 'pgsql',
'host' => getenv('DATA_POSTGRES_HOST'),
'name' => 'gonano',
'user' => getenv('DATA_POSTGRES_USER'),
'pass' => getenv('DATA_POSTGRES_PASS'),
// 'port' => 3306,
'charset' => 'utf8',
],
'sqlite' => [
'adapter' => 'sqlite',
'name' => getenv('PHINX_CONFIG_DIR') . 'tests/_data/assets/db/phalcon',
'suffix' => '.db',
],
],
'version_order' => 'creation',
];
2 changes: 1 addition & 1 deletion tests/_ci/.env.default
Expand Up @@ -43,6 +43,6 @@ DATA_BEANSTALKD_HOST="127.0.0.1"
DATA_BEANSTALKD_PORT=11300

# SQLite
DATA_SQLITE_NAME="${PROJECT_PATH}tests/_output/phalcon_test.sqlite"
DATA_SQLITE_NAME="${PROJECT_PATH}tests/_data/assets/db/phalcon.db"
DATA_SQLITE_I18N_NAME="${PROJECT_PATH}tests/_output/translations.sqlite"

@@ -0,0 +1,63 @@
<?php

use Phinx\Migration\AbstractMigration;

class AddEmployeesTable extends AbstractMigration
{
public function up()
{
$table = $this->table(
'co_employees',
[
'id' => 'empId',
'signed' => false,
]
);
$table
->addColumn(
'empNameFirst',
'string',
[
'limit' => 64,
'null' => false,
'default' => '',
]
)
->addColumn(
'empNameLast',
'string',
[
'limit' => 128,
'null' => false,
'default' => '',
]
)
->addColumn(
'empCreatedDate',
'datetime',
[
'null' => true,
]
)
->addColumn(
'empCreatedEmpId',
'integer',
[
'limit' => 11,
'null' => false,
'signed' => false,
'default' => 0,
]
)
->addIndex('empNameFirst')
->addIndex('empNameLast')
->addIndex('empCreatedDate')
->addIndex('empCreatedEmpId')
->save();
}

public function down()
{
$this->table('co_employees')->drop()->save();
}
}
Binary file added tests/_data/assets/db/phalcon.db
Binary file not shown.
34 changes: 34 additions & 0 deletions tests/_data/fixtures/models/Employees.php
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Models;

use Phalcon\Mvc\Model;

/**
* Class Employees
*
* @package Phalcon\Test\Models
*
* @property int $empId;
* @property string $empNameFirst;
* @property string $empNameLast;
* @property string $empCreatedDate;
* @property int $empCreatedEmpId;
*/
class Employees extends Model
{

public function initialize()
{
$this->setSource("co_employees");
}
}
74 changes: 50 additions & 24 deletions tests/integration/Mvc/Model/SaveCest.php
Expand Up @@ -14,6 +14,7 @@

use IntegrationTester;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Models\Employees;
use Phalcon\Test\Models\Robots;
use function is_int;
use function is_string;
Expand All @@ -35,15 +36,21 @@ public function _before(IntegrationTester $I)
/**
* Tests Phalcon\Mvc\Model :: save()
*
* @param IntegrationTester $I
* @dataProvider getFunctions
*
<<<<<<< HEAD
* @author Balázs Németh <https://github.com/zsilbi>
* @since 2019-04-30
=======
* @author Phalcon Team <team@phalconphp.com>
* @since 2019-05-10
>>>>>>> [#13002] - Introducing migrations
*/
public function mvcModelSave(IntegrationTester $I)
public function mvcModelSave(IntegrationTester $I, Example $function)
{
$I->wantToTest('Mvc\Model - save()');

<<<<<<< HEAD
/**
* New model
*/
Expand Down Expand Up @@ -248,6 +255,24 @@ public function mvcModelSaveAfterUsingRelatedGetters(IntegrationTester $I)
$songs = $album->getSongs();

$I->assertTrue($album->save());
=======
$method = $function[0];
$this->$method();

$first = uniqid();
$last = uniqid();
$employee = new Employees();
$employee->empNameFirst = $first;
$employee->empNameLast = $last;
$employee->empCreatedDate = '2019-05-10 00:00:00';
$employee->empCreatedEmpId = 1;

$result = $employee->save();
$I->assertTrue($result);

$result = $employee->delete();
$I->assertTrue($result);
>>>>>>> [#13002] - Introducing migrations
}

/**
Expand All @@ -256,7 +281,7 @@ public function mvcModelSaveAfterUsingRelatedGetters(IntegrationTester $I)
* @dataProvider getFunctions
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
* @since 2019-05-10
*/
public function mvcModelSaveCastLastInsertId(IntegrationTester $I, Example $function)
{
Expand All @@ -271,24 +296,24 @@ public function mvcModelSaveCastLastInsertId(IntegrationTester $I, Example $func
]
);

$name = uniqid();
$robot = new Robots();
$robot->name = $name;
$robot->type = 'Mechanical';
$robot->year = 2019;
$robot->datetime = '2019-05-10 00:00:00';
$robot->text = 'Some Text';
$first = uniqid();
$last = uniqid();
$employee = new Employees();
$employee->empNameFirst = $first;
$employee->empNameLast = $last;
$employee->empCreatedDate = '2019-05-10 00:00:00';
$employee->empCreatedEmpId = 1;

$result = $robot->save();
$result = $employee->save();
$I->assertTrue($result);

$actual = $robot->id;
$actual = $employee->empId;
$I->assertTrue(is_int($actual));

$expected = intval($actual, 10);
$I->assertEquals($expected, $actual);

$result = $robot->delete();
$result = $employee->delete();
$I->assertTrue($result);

Robots::setup(
Expand All @@ -297,24 +322,24 @@ public function mvcModelSaveCastLastInsertId(IntegrationTester $I, Example $func
]
);

$name = uniqid();
$robot = new Robots();
$robot->name = $name;
$robot->type = 'mechanical';
$robot->year = 2019;
$robot->datetime = '2019-05-10 00:00:00';
$robot->text = 'Some Text';
$first = uniqid();
$last = uniqid();
$employee = new Employees();
$employee->empNameFirst = $first;
$employee->empNameLast = $last;
$employee->empCreatedDate = '2019-05-10 00:00:00';
$employee->empCreatedEmpId = 1;

$result = $robot->save();
$result = $employee->save();
$I->assertTrue($result);

$actual = $robot->id;
$actual = $employee->empId;
$I->assertTrue(is_string($actual));

$expected = (string) $actual;
$I->assertEquals($expected, $actual);

$result = $robot->delete();
$result = $employee->delete();
$I->assertTrue($result);
}

Expand All @@ -325,7 +350,8 @@ public function getFunctions(): array
{
return [
['setDiMysql'],
['setDiPostgresql'],
// ['setDiPostgresql'],
['setDiSqlite'],
];
}
}

0 comments on commit 816a152

Please sign in to comment.