Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix migration #63

Merged
merged 1 commit into from
Oct 21, 2020
Merged
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
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@
"atk4/data": "~2.3.0"
},
"require-dev": {
"atk4/schema": "dev-develop",
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit": "*",
"symfony/contracts": ">=1.1"
},
"require-dev-release": {
"atk4/schema": "~2.3.0",
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit": "*",
"symfony/contracts": ">=1.1"
Expand Down
1 change: 1 addition & 0 deletions demos/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
declare(strict_types=1);

require 'src/App.php';
require 'src/MigratorConsole.php';
//require 'src/Acl.php';
42 changes: 42 additions & 0 deletions demos/src/MigratorConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use atk4\schema\Migration;
use atk4\ui\Console;

/**
* Makes sure your database is adjusted for one or several models,
* that you specify.
*/
class MigratorConsole extends Console
{
/** @var string Name of migrator class to use */
public $migrator_class = Migration::class;

/**
* Provided with array of models, perform migration for each of them.
*
* @param array $models
*/
public function migrateModels($models): void
{
// run inside callback
$this->set(function ($console) use ($models) {
$console->notice('Preparing to migrate models');

foreach ($models as $model) {
if (!is_object($model)) {
$model = $this->factory((array) $model);
$console->getApp()->db->add($model);
}

(new $this->migrator_class($model))->dropIfExists()->create(); // recreate table

$console->debug(' ' . get_class($model) . '.. OK');
}

$console->notice('Done with migration');
});
}
}
38 changes: 4 additions & 34 deletions demos/wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
use Throwable;

include '../vendor/autoload.php';

//include 'db.php';
include 'db.php';

// App without authentication to be able to freely import data
$app = new class(['title' => 'Agile Toolkit - Wizard setup']) extends App {
Expand All @@ -32,7 +31,6 @@ public function dbConnectFromWizard()
{
$this->readConfig('config.php', 'php');
$this->db = Persistence::connect($this->config['dsn']);
//$this->db->setApp($this);
}
};
$app->initLayout([\atk4\ui\Layout\Centered::class]);
Expand Down Expand Up @@ -131,38 +129,11 @@ public function dbConnectFromWizard()
});

$wizard->addStep('Quickly checking if database is OK', function (View $page) {
$console = Console::addTo($page);

/*
$button = $page->add([Button::class, '<< Back', 'huge wide blue'])
->addStyle('display', 'none')
->link(['index']);
*/
$page->getApp()->dbConnectFromWizard();

//@todo migrateModels Is broken and need a fix
//$console->migrateModels([User::class, Role::class, AccessRule::class]);

//@todo imported code from migratedModels function - START
$console->getApp()->db = $page->getApp()->db;

$console->set(function ($console) {
$console->notice('Preparing to migrate models');

$models = [User::class, Role::class, AccessRule::class];

foreach ($models as $model) {
$model = new $model($console->getApp()->db);
$m = Migration::of($model);
$result = $m->run();

$console->debug(' ' . get_class($m) . ': ' . $model->table . ' - ' . $result);
}

$console->notice('Done with migration');
});

//@todo imported code from migratedModels function - END
// do migration
$console = \MigratorConsole::addTo($page);
$console->migrateModels([User::class, Role::class, AccessRule::class]);
});

$wizard->addStep('Populate Sample Data', function (View $page) {
Expand Down Expand Up @@ -210,7 +181,6 @@ public function dbConnectFromWizard()
'model' => '\\atk4\login\\Model\\Role',
'all_visible' => true,
'all_editable' => false,
// 'editable_fields'=>['a','b']
],
]);

Expand Down