2
2

More than 3 years have passed since last update.

【Symfony】Migrationコマンドまとめ

Last updated at Posted at 2021-03-03

よく忘れるので自分用にメモしておく。
以下コマンドの前につけるphp bin/consoleは省略する。

1. 事前準備

.envファイルを設定する
/.env
DATABASE_URL="mysql://root:root@db:3306/db_name?serverVersion=13&charset=utf8"
DB作成

以下のコマンドを使うと .env の接続設定で指定した db_name のデータベースを作成する。

$ doctrine:database:create
collateの文字コードを繋いでるDBに合わせる
/config/packages/doctrine.yaml
default_table_options:
            collate: utf8_unicode_ci
Status表示

二つのコマンドが用意されているが、正直どっちを使ってもいいと思う。

$ doctrine:migrations:status
$ doctrine:migrations:list

個人的にはlistの方が簡潔で、かつgetDescriptionメソッドで返している値も表示しれくれるのでよく使っている。

2. 生成

方法1

現在のentityとdatabaseの状態を比べて差分のsqlのmigrationsファイルを生成

doctrine:migrations:diff
方法2

空のmigrationsファイルを生成

doctrine:migrations:generate

3. 実行

1. 全て実行
doctrine:migrations:migrate
2. ファイル指定して実行

ファイル指定してdownもできる

doctrine:migrations:excute --up 'DoctrineMigrations\Version20210103062456'
doctrine:migrations:excute --down 'DoctrineMigrations\Version20210103062456'

--dry-runオプションで実行されるSQL文が確認できるはずだが、なぜか確認されない。。。。
あとで時間ある際に調査、、、、

4. Tips

マイグレーションを実行せずに履歴情報のみを変更する。
doctrine:migrations:version {バージョン} (--add|--delete)
実行確認を出させないオプション

複数行のコマンドを一気に流す時によくつけるオプション

--no-interaction
全てのmigrationsファイルの状態を確認
doctrine:migrations:status --show-versions

*このオプションは最近のバージョンではなくなっているが、doctrine:migrations:listコマンドが対応していないバージョンではすごく助けられていた。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2