phoenix-migrations: update

This commit is contained in:
Rico Sta. Cruz 2017-09-04 10:59:05 +08:00
parent cd0ea99629
commit 60ef552392
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 23 additions and 9 deletions

View File

@ -1,27 +1,37 @@
--- ---
title: "Phoenix: Migrations" title: "Phoenix: Ecto migrations"
category: Elixir category: Elixir
layout: 2017/sheet
weight: -1
updated: 2017-09-04
--- ---
## Creating ### Creating
``` ```bash
$ mix ecto.gen.migration update_posts_table $ mix ecto.gen.migration update_posts_table
creating priv/repo/migrations/20160602085927_update_posts_table.exs creating priv/repo/migrations/20160602085927_update_posts_table.exs
... ...
```
```bash
$ mix ecto.migrate $ mix ecto.migrate
$ mix ecto.rollback $ mix ecto.rollback
``` ```
Creates a migration (no models).
### Creating models ### Creating models
``` ```bash
$ mix phoenix.gen.model Message messages user_id:integer content:text $ mix phoenix.gen.model Message messages user_id:integer content:text
# creates models and tests
``` ```
## Tables Creates models and tests.
## Migration functions
### Creating tables
```elixir ```elixir
create table(:documents) do create table(:documents) do
@ -44,6 +54,8 @@ end
create_if_not_exists table(:documents) do: ... end create_if_not_exists table(:documents) do: ... end
``` ```
### Other operations
```elixir ```elixir
alter table(:posts) do alter table(:posts) do
add :summary, :text add :summary, :text
@ -60,12 +72,14 @@ rename table(:posts), to: table(:new_posts)
```elixir ```elixir
drop table(:documents) drop table(:documents)
drop_if_exists table(:documents) drop_if_exists table(:documents)
```
```elixir
table(:documents) table(:documents)
table(:weather, prefix: :north_america) table(:weather, prefix: :north_america)
``` ```
## Indices ### Indices
```elixir ```elixir
create index(:posts, [:slug], concurrently: true) create index(:posts, [:slug], concurrently: true)
@ -73,7 +87,7 @@ create unique_index(:posts, [:slug])
drop index(:posts, [:name]) drop index(:posts, [:name])
``` ```
## Execute ### Execute SQL
```elixir ```elixir
execute "UPDATE posts SET published_at = NULL" execute "UPDATE posts SET published_at = NULL"
@ -82,4 +96,4 @@ execute create: "posts", capped: true, size: 1024
## References ## References
* [Ecto.Migration](http://devdocs.io/phoenix/ecto/ecto.migration) - [Ecto.Migration](http://devdocs.io/phoenix/ecto/ecto.migration)