diff --git a/README.md b/README.md index b69e478..1fe182f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config/config.exs b/config/config.exs index 136603b..3178df9 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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 diff --git a/lib/ecto/migration/auto.ex b/lib/ecto/migration/auto.ex index 2724c08..9272456 100644 --- a/lib/ecto/migration/auto.ex +++ b/lib/ecto/migration/auto.ex @@ -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 @@ -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]) diff --git a/lib/ecto/migration/auto/index.ex b/lib/ecto/migration/auto/index.ex index 238dfd3..c924fba 100644 --- a/lib/ecto/migration/auto/index.ex +++ b/lib/ecto/migration/auto/index.ex @@ -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 diff --git a/lib/ecto/migration/system_table.ex b/lib/ecto/migration/system_table.ex index a8ffb02..ea2065a 100644 --- a/lib/ecto/migration/system_table.ex +++ b/lib/ecto/migration/system_table.ex @@ -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 @@ -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 diff --git a/mix.exs b/mix.exs index f02f2a8..a1c8426 100644 --- a/mix.exs +++ b/mix.exs @@ -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 @@ -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: @@ -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 diff --git a/test/ecto_migrate_test.exs b/test/ecto_migrate_test.exs index afdff25..ca20435 100644 --- a/test/ecto_migrate_test.exs +++ b/test/ecto_migrate_test.exs @@ -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") @@ -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 @@ -39,7 +39,7 @@ end defmodule EctoMigrateTest do use ExUnit.Case import Ecto.Query - alias EctoIt.Repo + alias Ecto.Repo setup do :ok = :application.start(:ecto_it) @@ -47,7 +47,7 @@ defmodule EctoMigrateTest do 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" @@ -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