xerions/ecto_migrate

Name: ecto_migrate

Owner: xerions

Description: Automatic migrations for ecto

Created: 2015-03-16 11:22:12.0

Updated: 2018-02-21 07:55:30.0

Pushed: 2016-09-19 12:06:11.0

Homepage: null

Size: 85

Language: Elixir

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Ecto Migrate Build Status

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):

-S mix

After, it should be possible:

lication.start(:ecto_it)
s EctoIt.Repo

rt Ecto.Query

odule Weather do # is for later at now
e Ecto.Model

hema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp,    :float, default: 0.0
d


.Migration.Auto.migrate(Repo, Weather)

ther{city: "Berlin", temp_lo: 20, temp_hi: 25} |> Repo.insert
.all(from w in Weather, where: w.city == "Berlin")

Lets redefine the same model in a shell and migrate it

odule Weather do # is for later at now
e Ecto.Model

hema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp,    :float, default: 0.0
field :wind,    :float, default: 0.0
d


.Migration.Auto.migrate(Repo, Weather)
.all(from w in Weather, where: w.city == "Berlin")

Lets use references

odule Post do
e Ecto.Model

hema "posts" do
field :title, :string
field :public, :boolean, default: true
field :visits, :integer
has_many :comments, Comment
d


odule Comment do
e Ecto.Model

hema "comments" do
field :text, :string
belongs_to :post, Post
d


.Migration.Auto.migrate(Repo, Post)
.Migration.Auto.migrate(Repo, Comment)

ecto_migrate also provides additional migrate/3 API. For using with custom source defined models. Example:

odule Taggable do
e Ecto.Model

hema "this is not a valid schema name and it will never be used" do
field :tag_id, :integer
d


odule MyModel do
e Ecto.Model
hema "my_model" do
field :a, :string
has_many :my_model_tags, {"my_model_tags", Taggable}, [foreign_key: :tag_id]
d

Now we can migrate my_model_tags table with:

.Migration.Auto.migrate(Repo, MyModel)
.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:

odule Weather do # is for later at now
e Ecto.Model
e Ecto.Migration.Auto.Index

dex(:city, unique: true)
dex(:prcp)
hema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp,    :float, default: 0.0
d

If you do not want to use DSL for defining indexes, macro index doing no more, as generate function:

odule Weather do # is for later at now
e Ecto.Model

hema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp,    :float, default: 0.0
d

f __indexes__ do
[{[:city], [unique: true]},
 {[:prpc], []}]
d

Extra attribute options
odule Weather do # is for later at now
e Ecto.Model
e Ecto.Migration.Auto.Index

hema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp,    :float, default: 0.0
d

f __attribute_option__(:city), do: [size: 40]
f __attribute_option__(_),     do: []

Possibility to have more sources

If the same model used by different sources, it is possible to define callback for it

odule Weather do # is for later at now
e Ecto.Model
e Ecto.Migration.Auto.Index

hema "weather" do
field :city
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp,    :float, default: 0.0
d

f __sources__, do: ["weather", "history_weather"]

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.