Packages

Ecto auto migration library. It allows to generate and run migrations for initial and update migrations.

Current section

16 Versions

Jump to

Compare versions

6 files changed
+51 additions
-16 deletions
  @@ -1,3 +1,8 @@
1 + ## v0.3.3
2 +
3 + * Enhancements
4 + * allow defining more sources for the same model
5 +
1 6 ## v0.3.2
2 7
3 8 * Enhancements
  @@ -172,3 +172,24 @@ defmodule Weather do # is for later at now
172 172 def __attribute_option__(_), do: []
173 173 end
174 174 ```
175 +
176 + Possibility to have more sources
177 + --------------------------------
178 +
179 + If the same model used by different sources, it is possible to define callback for it
180 +
181 + ```elixir
182 + defmodule Weather do # is for later at now
183 + use Ecto.Model
184 + use Ecto.Migration.Auto.Index
185 +
186 + schema "weather" do
187 + field :city
188 + field :temp_lo, :integer
189 + field :temp_hi, :integer
190 + field :prcp, :float, default: 0.0
191 + end
192 +
193 + def __sources__, do: ["weather", "history_weather"]
194 + end
195 + ```
  @@ -29,4 +29,4 @@
29 29 [{<<"app">>,<<"postgrex">>},
30 30 {<<"optional">>,true},
31 31 {<<"requirement">>,<<">= 0.0.0">>}]}]}.
32 - {<<"version">>,<<"0.3.2">>}.
32 + {<<"version">>,<<"0.3.3">>}.
  @@ -57,25 +57,34 @@ defmodule Ecto.Migration.Auto do
57 57 """
58 58 def migrate(repo, module, opts \\ []) do
59 59 ensure_exists(repo)
60 + for tablename <- sources(module) do
61 + {assoc_field, tablename} = get_tablename(module, tablename, opts)
62 + tableatom = tablename |> String.to_atom
63 + for_opts = {assoc_field, opts[:for]}
60 64
61 - {assoc_field, tablename} = get_tablename(module, opts)
62 - tableatom = tablename |> String.to_atom
63 - for_opts = {assoc_field, opts[:for]}
65 + {fields_changes, assocs} = repo.get(SystemTable, tablename) |> Field.check(tableatom, module, for_opts)
66 + index_changes = (from s in SystemTable.Index, where: ^tablename == s.tablename) |> repo.all |> Index.check(tableatom, module)
64 67
65 - {fields_changes, assocs} = repo.get(SystemTable, tablename) |> Field.check(tableatom, module, for_opts)
66 - index_changes = (from s in SystemTable.Index, where: ^tablename == s.tablename) |> repo.all |> Index.check(tableatom, module)
67 -
68 - if migration_module = check_gen(tableatom, module, fields_changes, index_changes, opts) do
69 - Ecto.Migrator.up(repo, random, migration_module)
70 - Field.update_meta(repo, module, tablename, assocs) # May be in transaction?
71 - Index.update_meta(repo, module, tablename, index_changes)
68 + if migration_module = check_gen(tableatom, module, fields_changes, index_changes, opts) do
69 + Ecto.Migrator.up(repo, random, migration_module)
70 + Field.update_meta(repo, module, tablename, assocs) # May be in transaction?
71 + Index.update_meta(repo, module, tablename, index_changes)
72 + end
72 73 end
73 74 end
74 75
75 - defp get_tablename(module, []) do
76 - {nil, module.__schema__(:source)}
76 + def sources(module) do
77 + tablename = module.__schema__(:source)
78 + case function_exported?(module, :__sources__, 0) do
79 + true -> module.__sources__()
80 + false -> [tablename]
81 + end
77 82 end
78 - defp get_tablename(module, [for: mod]) do
83 +
84 + defp get_tablename(module, tablename, []) do
85 + {nil, tablename}
86 + end
87 + defp get_tablename(module, _, [for: mod]) do
79 88 %Ecto.Association.Has{assoc_key: assoc_key, queryable: {tablename, _}} = find_assoc_field(module, mod)
80 89 {assoc_key, tablename}
81 90 end
  @@ -106,7 +106,7 @@ defmodule Ecto.Migration.Auto.Field do
106 106 defp better_db_type(type), do: type
107 107
108 108 defp get_attribute_opts(module, name) do
109 - case :erlang.function_exported(module, :__attribute_option__, 1) do
109 + case function_exported?(module, :__attribute_option__, 1) do
110 110 true -> module.__attribute_option__(name)
111 111 _ -> []
112 112 end
Loading more files…