atk4 / schema
Agile Schema
Installs: 102 940
Dependents: 7
Suggesters: 0
Security: 0
Stars: 5
Watchers: 7
Forks: 5
Open Issues: 7
Requires
- php: >=7.3.0
- atk4/data: ~2.3.0
Requires (Dev)
- atk4/ui: ~2.3.0
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpcov: *
- phpunit/phpunit: >=9.0
Suggests
- atk4/ui: ~2.3.0
This package is auto-updated.
Last update: 2020-11-14 20:34:33 UTC
README
https://github.com/atk4/data
Agile Data - SQL Schema Management Add-on
This extension for Agile Data implements ability to work with SQL schema, execute migrations, perform DB-tests in PHPUnit (used by other ATK frameworks) and sync up "Model" structure to the database.
Basic Usage:
// Add the following code on your setup page / wizard: $app->add('MigratorConsole') ->migrateModels([ new Model\User($app->db), new Model\Order($app->db), new Model\Payment($app->db) ]);
The user will see a console which would adjust database to contain required tables / fields for the models:
Of course it's also possible to perform migration without visual feedback:
$changes = \atk4\schema\Migration::of(new User($app->db))->run();
If you need a more fine-graned migration, you can define them in great detail.
// create table $migrator = \atk4\schema\Migration::of($app->db); $migrator->table('user') ->id() ->field('name') ->field('address', ['type'=>'text']); ->create(); // or alter $migrator = \atk4\schema\Migration::of($app->db); $migrator->table('user') ->newField('age', ['type'=>'integer']) ->alter();
Currently atk4/schema fully supports MySQL and SQLite databases and partly PostgreSQL and Oracle. Other SQL databases are not yet natively supported but you can register your migrator class at runtime.
// $dbDriver is the connection driver name // MyCustomMigrator::class should be extending \atk4\schema\Migration \atk4\schema\Migration::register($platformClass, MyCustomMigrator::class);
Field declaration uses same types as ATK Data.
Examples
schema\Migration
is a simple class for building schema-related
queries using DSQL.
<?php $migrator = \atk4\data\schema\Migration::of($connection); $migrator->table('user')->drop(); $migrator->field('id'); $migrator->field('name', ['type'=>'string']); $migrator->field('age', ['type'=>'integer']); $migrator->field('bio'); $migrator->create();
schema\Snapshot
(NOT IMPLEMENTED) is a simple class that can record and restore
table contents:
<?php $s = new \atk4\data\schema\Snapshot($connection); $tables = $s->getDb($tables); // do anything with tables $s->setDb($tables);
Integration with PHPUnit
You can now automate your database testing by setting and checking your
database contents easier. First, extend your test-script from
\atk4\schema\PhpunitTestCase
.
Next, you need to set your schema
$q = ['user' => [ ['name' => 'John', 'surname' => 'Smith'], ['name' => 'Steve', 'surname' => 'Jobs'], ]]; $this->setDb($q);
Perform any changes, then execute:
$this->assertEquals($q, $this->getDb('user'));
To ensure that database remained the same. Of course you can compare against any other state.
- Automatically add 'id' field by default
- Create tables for you
- Detect types (int, string, date, boolean etc)
- Hides ID values if you don't pass them
Installation
Add the following inside your composer.json
file:
composer require atk4/schema