Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ Repo.all(from w in Weather, where: w.city == "Berlin")

```

In order to auto-migrate a schema `MyProject.Schemas.MySchema` in your `MyProject` project, create a file:
```bash
$ cat > priv/repo/seeds.exs

Ecto.Migration.Auto.migrate(MyProject.Repo, MyProject.Schemas.MySchema)
^D

$ mix run priv/repo/seeds.exs
```
This should create a database table based on `MySchema`.

Lets redefine the same model in a shell and migrate it

```elixir
Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
Expand Down
6 changes: 3 additions & 3 deletions lib/ecto/migration/auto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Ecto.Migration.Auto do
index_changes = (from s in SystemTable.Index, where: ^tablename == s.tablename) |> repo.all |> Index.check(tableatom, module)

if migration_module = check_gen(tableatom, module, fields_changes, index_changes, opts) do
Ecto.Migrator.up(repo, random, migration_module)
Ecto.Migrator.up(repo, random(), migration_module)
Field.update_meta(repo, module, tablename, relateds) # May be in transaction?
Index.update_meta(repo, module, tablename, index_changes)
end
Expand Down Expand Up @@ -174,11 +174,11 @@ defmodule Ecto.Migration.Auto do
repo.get(model, "test")
catch
_, _ ->
Ecto.Migrator.up(repo, random, migration)
Ecto.Migrator.up(repo, random(), migration)
end
end

defp random, do: :crypto.rand_uniform(0, 1099511627775)
defp random, do: :rand.uniform(1099511627775)

defp migration_module(module, []), do: Module.concat(module, Migration)
defp migration_module(module, [for: mod]), do: Module.concat([module, mod, Migration])
Expand Down
2 changes: 1 addition & 1 deletion lib/ecto/migration/auto/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Ecto.Migration.Auto.Index do
end

defp merge_default({index, opts}) do
{index, Keyword.merge(default_index_opts, opts) |> List.keysort(0)}
{index, Keyword.merge(default_index_opts(), opts) |> List.keysort(0)}
end

defp transform_from_db(index) do
Expand Down
4 changes: 2 additions & 2 deletions lib/ecto/migration/system_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Ecto.Migration.SystemTable.Index.Migration do
end

defmodule Ecto.Migration.SystemTable.Index do
use Ecto.Model
use Ecto.Schema
@primary_key {:tablename, :string, []}
schema "ecto_auto_migration_index" do
field :index, :string
Expand All @@ -35,7 +35,7 @@ defmodule Ecto.Migration.SystemTable.Migration do
end

defmodule Ecto.Migration.SystemTable do
use Ecto.Model
use Ecto.Schema
@primary_key {:tablename, :string, []}
schema "ecto_auto_migration" do
field :metainfo, :string
Expand Down
34 changes: 19 additions & 15 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
defmodule EctoMigrate.Mixfile do
use Mix.Project
@version "0.6.3"
@version "0.6.4"
@github "https://github.com/xerions/ecto_migrate"

def project do
[app: :ecto_migrate,
version: @version,
[
app: :ecto_migrate,
version: @version,

description: description,
package: package,
description: description(),
package: package(),

# Docs
name: "Ecto Auto Migrate",
docs: [source_ref: "v#{@version}",
source_url: @github],
deps: deps]
# Docs
name: "Ecto Auto Migrate",
docs: [source_ref: "v#{@version}",
source_url: @github],
deps: deps()
]
end

defp description do
Expand All @@ -34,7 +36,7 @@ defmodule EctoMigrate.Mixfile do
#
# Type `mix help compile.app` for more information
def application do
[applications: [:logger]]
[extra_applications: [:logger, :ecto_sql]]
end

# Dependencies can be Hex packages:
Expand All @@ -47,9 +49,11 @@ defmodule EctoMigrate.Mixfile do
#
# Type `mix help deps` for more examples and options
defp deps do
[{:postgrex, ">= 0.0.0", optional: true},
{:mariaex, ">= 0.0.0", optional: true},
{:ecto, "~> 1.0.0"},
{:ecto_it, "~> 0.2.0", optional: true}]
[#{:postgrex, ">= 0.0.0", optional: true},
#{:mariaex, ">= 0.0.0", optional: true},
{:ecto, "~> 3.7"},
{:ecto_sql, "~> 3.7"}
#{:ecto_it, "~> 0.2.0", optional: true}
]
end
end
18 changes: 9 additions & 9 deletions test/ecto_migrate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule TestModel do
end

defmodule Ecto.Taggable do
use Ecto.Model
use Ecto.Schema
use Ecto.Migration.Auto.Index

index(:tag_id, using: "hash")
Expand All @@ -24,11 +24,11 @@ defmodule Ecto.Taggable do
end

defmodule MyModel do
use Ecto.Model
use Ecto.Schema
schema "my_model" do
field :a, :string
field :b, :integer
field :c, Ecto.DateTime
field :c, :naive_datetime
has_many :my_model_tags, {"my_model_tags", Ecto.Taggable}, [foreign_key: :tag_id]
end

Expand All @@ -39,15 +39,15 @@ end
defmodule EctoMigrateTest do
use ExUnit.Case
import Ecto.Query
alias EctoIt.Repo
alias Ecto.Repo

setup do
:ok = :application.start(:ecto_it)
on_exit fn -> :application.stop(:ecto_it) end
end

test "ecto_migrate with tags test" do
Ecto.Migration.Auto.migrate(EctoIt.Repo, TestModel)
Ecto.Migration.Auto.migrate(Ecto.Repo, TestModel)
query = from t in Ecto.Migration.SystemTable, select: t
[result] = Repo.all(query)
assert result.metainfo == "id:id,f:string,i:BIGINT,l:boolean"
Expand All @@ -58,15 +58,15 @@ defmodule EctoMigrateTest do

Repo.insert!(%MyModel{a: "foo"})
Repo.insert!(%MyModel{a: "bar"})
%MyModel{a: "foo"} |> Ecto.Model.put_source("my_model_2") |> Repo.insert!
%MyModel{a: "foo"} |> Ecto.Schema.put_source("my_model_2") |> Repo.insert!

model = %MyModel{}
new_tag = Ecto.Model.build(model, :my_model_tags)
new_tag = Ecto.Schema.build(model, :my_model_tags)
new_tag = %{new_tag | tag_id: 2, name: "test_tag", model: MyModel |> to_string}
EctoIt.Repo.insert!(new_tag)
Repo.insert!(new_tag)

query = from c in MyModel, where: c.id == 2, preload: [:my_model_tags]
[result] = EctoIt.Repo.all(query)
[result] = Repo.all(query)
[tags] = result.my_model_tags

assert tags.id == 1
Expand Down