「Php/phpmig」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
|||
| 125行目: | 125行目: | ||
} | } | ||
</pre> | </pre> | ||
==マイグレーション実行方法== | ==マイグレーション実行方法== | ||
| 135行目: | 132行目: | ||
== 20241022021157 AddTest migrated 0.0282s | == 20241022021157 AddTest migrated 0.0282s | ||
</pre> | </pre> | ||
==中身の書き方== | |||
公式:https://github.com/davedevelopment/phpmig | |||
==ステータス確認== | ==ステータス確認== | ||
2024年10月21日 (月) 17:07時点における版
phpmigインストール(composer)
composer require davedevelopment/phpmig composer require pimple/pimple
参考:https://qiita.com/hideiwa1/items/98f74d95806f8d43cef9
参考:https://labor.ewigleere.net/2020/01/24/phpmig-phpmyadmin-migrate/
composer.jsonから作成の場合
composer.json
{
"require": {
"davedevelopment/phpmig": "^1.7",
"pimple/pimple": "^3.5"
}
}
$ composer update
phpmig初期設定
% vendor/bin/phpmig init +d ./migrations Place your migration files in here +f ./phpmig.php Create services in here
migrationsディレクトリと、phpmig.phpファイルができる。
phpmig.php
<?php
use \Phpmig\Adapter;
$container = new ArrayObject();
// replace this with a better Phpmig\Adapter\AdapterInterface
$container['phpmig.adapter'] = new Adapter\File\Flat(__DIR__ . DIRECTORY_SEPARATOR . 'migrations/.migrations.log');
$container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations';
// You can also provide an array of migration files
// $container['phpmig.migrations'] = array_merge(
// glob('migrations_1/*.php'),
// glob('migrations_2/*.php')
// );
return $container;
中身を以下参考ページを見ながら、接続できるように修正。
参考:https://qiita.com/hideiwa1/items/98f74d95806f8d43cef9
db設定
config/database/の下にファイルを作成
マイグレーション実行
% vendor/bin/phpmig generate AddTest +f ./migrations/20241022021157_AddTest.php
以下ファイルだけが、作成される(これ以外の更新はない)
migrations/20241022021157_AddTest.php
use Phpmig\Migration\Migration;
class AddTests extends Migration
{
/**
* Do the migration
*/
public function up()
{
}
/**
* Undo the migration
*/
public function down()
{
}
}
以下のように修正
class AddTest extends Migration
{
/**
* Do the migration
*/
public function up()
{
$sql =<<<EOF
CREATE TABLE tests(
`id` integer(15) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`is_delete` boolean NOT NULL DEFAULT false,
`created_at` datetime DEFAULT CURRENT_TIMESTAMP(),
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP(),
PRIMARY KEY (`id`)
);
EOF;
$container = $this -> getContainer();
$container['db']->query($sql);
}
/**
* Undo the migration
*/
public function down()
{
$sql =<<<EOF
DROP TABLE tests
EOF;
$container = $this->getContainer();
$container['db']->query($sql);
}
}
マイグレーション実行方法
$ vendor/bin/phpmig migrate m == 20241022021157 AddTest migrating == 20241022021157 AddTest migrated 0.0282s
中身の書き方
公式:https://github.com/davedevelopment/phpmig
ステータス確認
$ vendor/bin/phpmig status Status Migration ID Migration Name ----------------------------------------- down 20241022021157 AddTest