Browse Source

tested db migrate,rolback,seed

mx 3 years ago
parent
commit
b8b758577f
7 changed files with 2376 additions and 43 deletions
  1. 22 0
      README.md
  2. 5 1
      composer.json
  3. 2238 42
      composer.lock
  4. 38 0
      db/migrations/20220329082210_user.php
  5. 31 0
      db/seeds/UserSeeder.php
  6. 1 0
      phinx
  7. 41 0
      phinx.php

+ 22 - 0
README.md

@@ -8,3 +8,25 @@
 
 `php start.php start -d`
 
+
+### db migration
+
+1. `composer install robmorgan/phinx` [offcial docs](https://tsy12321.gitbooks.io/phinx-doc/content/)
+2. `mkdir -p db/{migration,seeds}`
+3. `vendor/bin/phinx init`
+4. `vendor/bin/phinx create User`
+5. Edit `db/migration/***_user_.php`
+6. `vendor/bin/phinx migration -e dev`
+7. test `vendor/bin/phinx rollback -e dev`
+
+### db seeds
+
+1. `phinx seed:create UserSeeder -e dev`
+2. Edit `db/seeds/UserSeeder.php`
+3. `phinx seed:run -e dev`
+4. 同 laravel 不一樣, created updated 這些字段並不會自動插入值
+
+NOTICE
+1. 重復執行 3 會重復插入數據
+2. 3 所有插入爲同一個事務
+3. 3 事務執行失敗會引起ID自增 (pgsql)

+ 5 - 1
composer.json

@@ -26,7 +26,11 @@
   "require": {
     "php": ">=7.2",
     "workerman/webman-framework": "^1.3.0",
-    "monolog/monolog": "^2.0"
+    "monolog/monolog": "^2.0",
+    "illuminate/database": "^8.83",
+    "illuminate/pagination": "^8.83",
+    "illuminate/events": "^8.83",
+    "robmorgan/phinx": "^0.12.10"
   },
   "suggest": {
     "ext-event": "For better performance. "

File diff suppressed because it is too large
+ 2238 - 42
composer.lock


+ 38 - 0
db/migrations/20220329082210_user.php

@@ -0,0 +1,38 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class User extends AbstractMigration
+{
+    /**
+     * Change Method.
+     *
+     * Write your reversible migrations using this method.
+     *
+     * More information on writing migrations is available here:
+     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
+     *
+     * Remember to call "create()" or "update()" and NOT "save()" when working
+     * with the Table class.
+     */
+    public function change(): void
+    {
+        $table = $this->table('users');
+        $table->addColumn('username', 'string', ['limit'=>64])
+            ->addColumn('password', 'string', ['limit'=>64])
+            ->addColumn('created', 'datetime')
+            ->create();
+
+    }
+
+    public function up(): void
+    {
+
+    }
+
+    public function down(): void
+    {
+
+    }
+}

+ 31 - 0
db/seeds/UserSeeder.php

@@ -0,0 +1,31 @@
+<?php
+
+
+use Phinx\Seed\AbstractSeed;
+
+class UserSeeder extends AbstractSeed
+{
+    /**
+     * Run Method.
+     *
+     * Write your database seeder using this method.
+     *
+     * More information on writing seeders is available here:
+     * https://book.cakephp.org/phinx/0/en/seeding.html
+     */
+    public function run()
+    {
+        $table = $this->table('users');
+        $table->insert([
+            [
+                'username'=>'gooder',
+                'password'=>'xxx',
+                'created' => date('Y-m-d H:i:s'),
+            ],[
+                'username'=>'t2',
+                'password'=>'fdfdf',
+                'created' => date('Y-m-d H:i:s'),
+            ]
+        ])->save();
+    }
+}

+ 1 - 0
phinx

@@ -0,0 +1 @@
+./vendor/bin/phinx

+ 41 - 0
phinx.php

@@ -0,0 +1,41 @@
+<?php
+
+return
+[
+    'paths' => [
+        'migrations' => '%%PHINX_CONFIG_DIR%%/db/migrations',
+        'seeds' => '%%PHINX_CONFIG_DIR%%/db/seeds'
+    ],
+    'environments' => [
+        'default_migration_table' => 'phinxlog',
+        'default_environment' => 'development',
+        'prod' => [
+            'adapter' => 'pgsql',
+            'host' => 'localhost',
+            'name' => 'production_db',
+            'user' => 'root',
+            'pass' => '',
+            'port' => '3306',
+            'charset' => 'utf8',
+        ],
+        'dev' => [
+            'adapter' => 'pgsql',
+            'host' => 'localhost',
+            'name' => 'pgt1',
+            'user' => 'pgt1',
+            'pass' => 'pgt1',
+            'port' => '5432',
+            'charset' => 'utf8',
+        ],
+        'test' => [
+            'adapter' => 'pgsql',
+            'host' => 'localhost',
+            'name' => 'testing_db',
+            'user' => 'root',
+            'pass' => '',
+            'port' => '3306',
+            'charset' => 'utf8',
+        ]
+    ],
+    'version_order' => 'creation'
+];

Some files were not shown because too many files changed in this diff