LoginSignup
5

More than 5 years have passed since last update.

elixir on phoenix Mix Tasksについて

Last updated at Posted at 2016-09-24

phoenixでは標準のコマンドでいろいろとできるようになっている


$ mix help | grep -i phoenix
mix phoenix.digest       # Digests and compress static files
mix phoenix.gen.channel  # Generates a Phoenix channel
mix phoenix.gen.html     # Generates controller, model and views for an HTML based resource
mix phoenix.gen.json     # Generates a controller and model for a JSON based resource
mix phoenix.gen.model    # Generates an Ecto model
mix phoenix.gen.presence # Generates a Presence tracker
mix phoenix.gen.secret   # Generates a secret
mix phoenix.routes       # Prints all routes
mix phoenix.server       # Starts applications and their servers

server(サーバーの起動)

mix phoenix.server

Ctrl + cを2回おすとサーバーを停止

new(アプリケーションの生成)

mix phoenix.new {アプリケーションの生成名}

DBは標準はpostgresを使用するmysqlを使いたい場合はmix phoenix.new --database mysql {プロジェクト名}
のように指定する

mix phoenix.newのオプション

  • --app - the name of the OTP application

  • --module - the name of the base module in

    the generated skeleton

  • --database - specify the database adapter for ecto.

    Values can be postgres, mysql, mssql, or mongodb.
    Defaults to postgres.

  • --no-brunch - do not generate brunch files

    for static asset building. When choosing this
    option, you will need to manually handle
    JavaScript dependencies if building HTML apps

  • --no-ecto - do not generate ecto files for the model layer

  • --no-html - do not generate HTML views.

  • --binary-id - use binary_id as primary key type in ecto models

routes(ルーティングの確認)

設定されているルーティングの確認

mix phoenix.routes

ルートの追加

4.elixir on phoenix ルーティングの書き方についてまとめてみた

gen.model(モデルの生成)

mix phoenix.gen.model User users username:string first_name:string last_name:string

このコマンドでモデル、マイグレーション、テストが生成される

マイグレーション

mix ecto.gen.migration create_{table名} # マイグレーションのファイル作成
mix ecto.gen.migration add_column # カラム追加
mix ecto.gen.migration remove_column # カラムの削除
mix ecto.rollback # ロールバック
mix ecto.create 
mix ecto.migrate # 実行

マイグレーションサンプル

defmodule App.Repo.Migrations.Create{テーブル名} do
  use Ecto.Migration

  def change do
    create table(:{テーブル名}) do
      add :name, :string

      timestamps
    end

    create unique_index(:{テーブル名}, [:{カラム名}])
  end
end

defmodule App.Repo.Migrations.AddColumn do
  use Ecto.Migration

  def change do
    alter table(:users) do
      add :age, :integer
    end
  end
end

defmodule App.Repo.Migrations.RemoveColumn do
  use Ecto.Migration

  def change do
    alter table(:users) do
      remove :age
    end
  end
end

gen.html(コントローラーなどなどの生成)

mix phoenix.gen.html User users name:string age:integer

このコマンドでmodels、views、controllers、migration file 、templates、test filesが生成される

gen.json(templatesがいらないときAPIとか)

mix phoenix.gen.json User users name:string age:integer

このコマンドでmodels、views、controllers、migration file 、test filesが生成される

gen.channel(チャンネルの生成)

mix phoenix.gen.channel {チャンネル名}

gen.secret(乱数の生成)

mix phoenix.gen.secret {桁数:32桁以上}

$ mix phoenix.gen.secret 32
oWZisADZLcxctBasWcLW0zsltoexBrHn

リンク

1.elixir on phoenix セットアップ
2.elixir on phoenix Mix Tasksについて
3.elixir on phoenix ectoを使っていろいろなSELECT文
4.elixir on phoenix ルーティングの書き方についてまとめてみた
5.elixir on phoenix changesetでのValidates

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
5