Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xerions/ecto_migrate
Automatic migrations for ecto
https://github.com/xerions/ecto_migrate
Last synced: 4 days ago
JSON representation
Automatic migrations for ecto
- Host: GitHub
- URL: https://github.com/xerions/ecto_migrate
- Owner: xerions
- License: other
- Created: 2015-03-16T11:22:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-05-30T11:56:33.000Z (over 1 year ago)
- Last Synced: 2024-10-03T13:22:51.370Z (about 1 month ago)
- Language: Elixir
- Size: 83 KB
- Stars: 35
- Watchers: 8
- Forks: 11
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Ecto auto migration library. It allows to generate and run migrations for initial and update migrations. (ORM and Datamapping)
- fucking-awesome-elixir - ecto_migrate - Ecto auto migration library. It allows to generate and run migrations for initial and update migrations. (ORM and Datamapping)
- awesome-elixir - ecto_migrate - Ecto auto migration library. It allows to generate and run migrations for initial and update migrations. (ORM and Datamapping)
README
Ecto Migrate [![Build Status](https://travis-ci.org/xerions/ecto_migrate.svg)](https://travis-ci.org/xerions/ecto_migrate)
============Ecto migrate brings automatic migrations to ecto. Instead of defining and writting manuall diffing
from actual model and old model. The `ecto_migrate` do it for you. It save actual represantation of
a model model in database and checks, if actual model have the same format as saved in database.To test, use EctoIt (is depended on it for tests purposes):
```
iex -S mix
```After, it should be possible:
```elixir
:application.start(:ecto_it)
alias EctoIt.Repoimport Ecto.Query
defmodule Weather do # is for later at now
use Ecto.Modelschema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
end
endEcto.Migration.Auto.migrate(Repo, Weather)
%Weather{city: "Berlin", temp_lo: 20, temp_hi: 25} |> Repo.insert
Repo.all(from w in Weather, where: w.city == "Berlin")```
Lets redefine the same model in a shell and migrate it
```elixir
defmodule Weather do # is for later at now
use Ecto.Modelschema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
field :wind, :float, default: 0.0
end
endEcto.Migration.Auto.migrate(Repo, Weather)
Repo.all(from w in Weather, where: w.city == "Berlin")```
Lets use references
```elixir
defmodule Post do
use Ecto.Modelschema "posts" do
field :title, :string
field :public, :boolean, default: true
field :visits, :integer
has_many :comments, Comment
end
enddefmodule Comment do
use Ecto.Modelschema "comments" do
field :text, :string
belongs_to :post, Post
end
endEcto.Migration.Auto.migrate(Repo, Post)
Ecto.Migration.Auto.migrate(Repo, Comment)```
`ecto_migrate` also provides additional `migrate/3` API. For using with custom source defined models. Example:
```elixir
defmodule Taggable do
use Ecto.Modelschema "this is not a valid schema name and it will never be used" do
field :tag_id, :integer
end
enddefmodule MyModel do
use Ecto.Model
schema "my_model" do
field :a, :string
has_many :my_model_tags, {"my_model_tags", Taggable}, [foreign_key: :tag_id]
end
end
```Now we can migrate `my_model_tags` table with:
```elixir
Ecto.Migration.Auto.migrate(Repo, MyModel)
Ecto.Migration.Auto.migrate(Repo, Taggable, [for: MyModel])
```It will generate and migrate `my_model_tags` table to the database which will be associated with `my_model` table.
Indexes
-------`ecto_migrate` has support of indexes:
```elixir
defmodule Weather do # is for later at now
use Ecto.Model
use Ecto.Migration.Auto.Indexindex(:city, unique: true)
index(:prcp)
schema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
end
end
```If you do not want to use DSL for defining indexes, macro index doing no more, as generate function:
```elixir
defmodule Weather do # is for later at now
use Ecto.Modelschema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
enddef __indexes__ do
[{[:city], [unique: true]},
{[:prpc], []}]
end
end
```Extra attribute options
-----------------------```elixir
defmodule Weather do # is for later at now
use Ecto.Model
use Ecto.Migration.Auto.Indexschema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
enddef __attribute_option__(:city), do: [size: 40]
def __attribute_option__(_), do: []
end
```Possibility to have more sources
--------------------------------If the same model used by different sources, it is possible to define callback for it
```elixir
defmodule Weather do # is for later at now
use Ecto.Model
use Ecto.Migration.Auto.Indexschema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
enddef __sources__, do: ["weather", "history_weather"]
end
```Upgrades in 0.3.x versions
--------------------------If you have installed version before 0.3.2, use 0.3.2 or 0.3.3 for upgrading the table, after that it is possible to
upgrade higher versions.