Giter Club home page Giter Club logo

schema's Introduction

!!! repo was integrated into 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.

Build CodeCov GitHub release Code Climate

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:

migrator-console

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

schema's People

Contributors

abbadon1334 avatar darkside666 avatar georgehristov avatar mvorisek avatar romaninsh avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

schema's Issues

Add Simulation and/or Manual mode

I like that add-on quite much but I would really love a simulation mode or a manual mode.

Simulation:
I want to add a page "Update DB" which checks all models just like it does now and shows me the differences or migrations to be done. I want to have a look at then and then have another button "Approve" or "Migrate" to start the actual migration to have more control over what is happening.
I had the situation yesterday that it deleted a column and I could not even see which one - so whatever was in there is lost now and I don't even know what it was...

Manual mode:
A bit like the simulation but even more control. It does the diff and just outputs all "to be done" SQL statements to have the user run them by himself.

I don't really know where to start actually. First thought was to use the Snapshot feature to have it recorded, then do a migration, then roll it back and somehow log the statements in between.
But does this make sense or is there an easier way?

Model hasOne type

when type is not defined for referenceOne and their_field don't have a type, it will get integer as default even if in atk4/field default type is string and atk4/schema SQL default type is string.

'type' => $ref_type !== self::REF_TYPE_NONE && empty($persist_field->type) ? 'integer' : $persist_field->type,

I discovered it today, while i add download feature to atk4/filestore in this pr : atk4/filestore#14

The demos/basic.php of that pr is a perfect example of the problem.

if type is removed from the definition here : https://github.com/atk4/filestore/blob/ccb9aadef79c27d4ef1bd6011d1bf2e04ae01c86/src/Model/File.php#L19

the table will be created with field token as integer.

Discuss the problem even here : atk4/filestore#13

I know schema is dangerous to be changed, but this probably is just an alignment of functionality between atk4/data and atk4/schema.

Make 2.0.1 release

Is it possible to make 2.0.1 release (to include the 29 and 31 issues)?

PDOException on SHOW TABLES

if (!$this->connection->expr('show tables like []', [$table])->get()) {

For some reason PDO does not accept parameter for the expression and throws exception when preparing the statement

Exception text:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1

MariaDB 10.1.38
PHP 7.3.11

$field->persist attribute

$this->field($field->actual ?: $field->short_name, ['type' => $type]); // todo add more options here

Suggest to introduce $persist attribute in the atk4\data\Field class with functionality as follows:

  • $persist = false takes over $never_persist
  • $persist = ['length' => 5] array as list of parameters which atk4\schema\Migration class will use
  • $persist = ['type' => 'text'] type key will determine field individual persistence type

Getting exceptions with Migration object

Hi,

$app = new App();
$migrator = new Migration($app->db);
$migrator->table('test')
    ->id()
    ->create();

This code (and similar operations) gives me exceptions.

error:"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '\"test\" (\"id\" bigint unsigned not null primary key autoincrement)' at line 1"

query:"create table \"test\" (\"id\" bigint unsigned not null primary key autoincrement)"

I'm using MariaDB 10.2.31 and PHP 7.4.5, with all ATK packages to latest stable versions.

Can anyone reproduce this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.