2016-06-02 09:39:31 +00:00
|
|
|
---
|
2017-09-04 02:59:05 +00:00
|
|
|
title: "Phoenix: Ecto migrations"
|
2016-06-02 09:39:31 +00:00
|
|
|
category: Elixir
|
2017-09-04 02:59:05 +00:00
|
|
|
weight: -1
|
2020-07-04 13:33:09 +00:00
|
|
|
updated: 2020-02-23
|
2016-06-02 09:39:31 +00:00
|
|
|
---
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
### Creating
|
2016-06-02 09:39:31 +00:00
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
```bash
|
2016-06-02 10:07:58 +00:00
|
|
|
$ mix ecto.gen.migration update_posts_table
|
|
|
|
creating priv/repo/migrations/20160602085927_update_posts_table.exs
|
2017-09-04 03:08:40 +00:00
|
|
|
···
|
2017-09-04 02:59:05 +00:00
|
|
|
```
|
2016-06-02 09:39:31 +00:00
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
```bash
|
2016-06-02 09:39:31 +00:00
|
|
|
$ mix ecto.migrate
|
|
|
|
$ mix ecto.rollback
|
|
|
|
```
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
Creates a migration (no models).
|
|
|
|
|
2016-06-02 10:07:58 +00:00
|
|
|
### Creating models
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
```bash
|
2016-06-02 10:07:58 +00:00
|
|
|
$ mix phoenix.gen.model Message messages user_id:integer content:text
|
|
|
|
```
|
2016-06-02 09:39:31 +00:00
|
|
|
|
2017-09-04 03:08:40 +00:00
|
|
|
This is only for Phoenix 1.2 or older; models aren't available in Phoenix 1.3+.
|
2017-09-04 02:59:05 +00:00
|
|
|
|
2019-08-15 21:19:56 +00:00
|
|
|
### Creating context
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ mix phx.gen.context Images Album albums title:string subtitle:string privacy:string
|
|
|
|
```
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
## Migration functions
|
|
|
|
|
|
|
|
### Creating tables
|
2016-06-02 09:39:31 +00:00
|
|
|
|
|
|
|
```elixir
|
|
|
|
create table(:documents) do
|
|
|
|
add :title, :string
|
|
|
|
add :title, :string, size: 40
|
|
|
|
add :title, :string, default: "Hello"
|
|
|
|
add :title, :string, default: fragment("now()")
|
|
|
|
add :title, :string, null: false
|
|
|
|
add :body, :text
|
|
|
|
add :age, :integer
|
|
|
|
add :price, :float
|
2020-07-08 15:38:20 +00:00
|
|
|
add :price, :float
|
|
|
|
add :price, :decimal, precision: 10, scale: 2
|
2019-08-03 19:57:44 +00:00
|
|
|
add :published_at, :utc_datetime
|
2016-06-02 09:39:31 +00:00
|
|
|
add :group_id, references(:groups)
|
|
|
|
add :object, :json
|
|
|
|
|
|
|
|
timestamps # inserted_at and updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
create_if_not_exists table(:documents) do: ... end
|
|
|
|
```
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
### Other operations
|
|
|
|
|
2016-06-02 09:39:31 +00:00
|
|
|
```elixir
|
|
|
|
alter table(:posts) do
|
|
|
|
add :summary, :text
|
|
|
|
modify :title, :text
|
|
|
|
remove :views
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
rename table(:posts), :title, to: :summary
|
|
|
|
rename table(:posts), to: table(:new_posts)
|
|
|
|
```
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
drop table(:documents)
|
|
|
|
drop_if_exists table(:documents)
|
2017-09-04 02:59:05 +00:00
|
|
|
```
|
2016-06-02 09:39:31 +00:00
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
```elixir
|
2016-06-02 09:39:31 +00:00
|
|
|
table(:documents)
|
|
|
|
table(:weather, prefix: :north_america)
|
|
|
|
```
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
### Indices
|
2016-06-02 09:39:31 +00:00
|
|
|
|
|
|
|
```elixir
|
|
|
|
create index(:posts, [:slug], concurrently: true)
|
|
|
|
create unique_index(:posts, [:slug])
|
|
|
|
drop index(:posts, [:name])
|
|
|
|
```
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
### Execute SQL
|
2016-06-02 09:39:31 +00:00
|
|
|
|
|
|
|
```elixir
|
|
|
|
execute "UPDATE posts SET published_at = NULL"
|
|
|
|
execute create: "posts", capped: true, size: 1024
|
|
|
|
```
|
|
|
|
|
|
|
|
## References
|
|
|
|
|
2017-09-04 02:59:05 +00:00
|
|
|
- [Ecto.Migration](http://devdocs.io/phoenix/ecto/ecto.migration)
|