From 849e6581ec7b1ba486599c60530df6e0ebb47b7c Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Wed, 6 Sep 2023 09:30:33 +0200 Subject: [PATCH] Integrated Phinx DB migrations. Requires running `composer install` --- bin/phinx | 27 ++++++ composer.json | 3 +- config/phinx.php | 38 ++++++++ .../20230905130832_add_can_permissions.php | 88 +++++++++++++++++++ db/template/migrate.change.template | 29 ++++++ db/template/migrate.up_down.template | 29 ++++++ 6 files changed, 213 insertions(+), 1 deletion(-) create mode 100755 bin/phinx create mode 100644 config/phinx.php create mode 100644 db/migrations/20230905130832_add_can_permissions.php create mode 100644 db/template/migrate.change.template create mode 100644 db/template/migrate.up_down.template diff --git a/bin/phinx b/bin/phinx new file mode 100755 index 000000000..360b73867 --- /dev/null +++ b/bin/phinx @@ -0,0 +1,27 @@ +#!/usr/bin/env php + 1) { + $command = $argv[1]; + $arguments = array_slice($argv, 1); + + //var_dump($arguments); + if(in_array($command, ["breakpoint", "create", "migrate", "rollback", "status", "test", "seed:create", "seed:run", "list:aliases"])) { + $arguments = array_merge($default_args, $arguments); + } + //var_dump($arguments); +} + +pcntl_exec($phinx_path, $arguments); diff --git a/composer.json b/composer.json index c177ad0e5..9af302862 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "require": { - "phpoffice/phpspreadsheet": "^1.23" + "phpoffice/phpspreadsheet": "^1.23", + "robmorgan/phinx": "^0.13.4" } } diff --git a/config/phinx.php b/config/phinx.php new file mode 100644 index 000000000..e9f5869fc --- /dev/null +++ b/config/phinx.php @@ -0,0 +1,38 @@ + [ + 'migrations' => BASEDIR."/db/migrations", + 'seeds' => BASEDIR."/db/seeds" + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_environment' => 'thetool', + 'thetool' => [ + 'adapter' => 'mysql', + 'host' => FRONKDB_DBHOST, + 'name' => FRONKDB_DBNAME, + 'user' => FRONKDB_DBUSER, + 'pass' => FRONKDB_DBPASS, + 'port' => '3306', + 'charset' => 'utf8', + ], + 'addressdb' => [ + 'adapter' => 'mysql', + 'host' => ADDRESSDB_DBHOST, + 'name' => ADDRESSDB_DBNAME, + 'user' => ADDRESSDB_DBUSER, + 'pass' => ADDRESSDB_DBPASS, + 'port' => '3306', + 'charset' => 'utf8', + ], + + ], + 'version_order' => 'creation', + 'templates' => [ + "file" => BASEDIR."/db/template/migrate.up_down.template" + ] +]; diff --git a/db/migrations/20230905130832_add_can_permissions.php b/db/migrations/20230905130832_add_can_permissions.php new file mode 100644 index 000000000..c8b553d4f --- /dev/null +++ b/db/migrations/20230905130832_add_can_permissions.php @@ -0,0 +1,88 @@ +getEnvironment() == "thetool") { + $new_perms = [ + "canBuilding", + "canPipework", + "canLinework", + "canPatching", + "canFilestore", + "canCpeprovisioning", + "canCpeshipping", + "canVoipnumbering", + "canPreorder", + "canOrder" + ]; + + $table = $this->table("WorkerPermission"); + + $setAllTrue = ""; + + $after = "preorderaddressreporting"; + foreach($new_perms as $p) { + $table->addColumn($p, "enum", ["values" => 'false,true', "default" => "false", "after" => $after]); + $after = $p; + $setAllTrue .= "`$p` = 'true', "; + } + $table->update(); + + $setAllTrue = substr($setAllTrue, 0, strlen($setAllTrue) -2); + + //echo "$setAllTrue\n"; + + $builder = $this->getQueryBuilder(); + $q = $builder->select("*")->from("WorkerPermission")->execute(); + + while($perm = $q->fetch('assoc')) { + $id = $perm["id"]; + + if($perm["admin"] == "true") { + $this->query("UPDATE WorkerPermission SET $setAllTrue WHERE id=$id"); + } elseif($perm["preorderfront"] == "false" && $perm['preorderaddressreporting'] == "false") { + $this->query("UPDATE WorkerPermission SET $setAllTrue WHERE id=$id"); + } else { + $this->query("UPDATE WorkerPermission SET `canPreorder` = 'true' WHERE id=$id"); + } + } + + } + + + + if($this->getEnvironment() == "addressdb") { + // do nothing in addressdb Env + } + + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + + $table = $this->table("WorkerPermission"); + $table->removeColumn("canOrder"); + $table->removeColumn("canPreorder"); + $table->removeColumn("canVoipnumbering"); + $table->removeColumn("canCpeshipping"); + $table->removeColumn("canCpeprovisioning"); + $table->removeColumn("canFilestore"); + $table->removeColumn("canPatching"); + $table->removeColumn("canLinework"); + $table->removeColumn("canPipework"); + $table->removeColumn("canBuilding"); + $table->update(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/db/template/migrate.change.template b/db/template/migrate.change.template new file mode 100644 index 000000000..9e8055534 --- /dev/null +++ b/db/template/migrate.change.template @@ -0,0 +1,29 @@ +getEnvironment() == "thetool") { + + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} \ No newline at end of file diff --git a/db/template/migrate.up_down.template b/db/template/migrate.up_down.template new file mode 100644 index 000000000..ef0374268 --- /dev/null +++ b/db/template/migrate.up_down.template @@ -0,0 +1,29 @@ +getEnvironment() == "thetool") { + + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + + } + + if($this->getEnvironment() == "addressdb") { + + } + } +}