Skip to content

Commit 70d52cb

Browse files
committed
add migrated_api? to check is a table migrated or not
1 parent a325c57 commit 70d52cb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/ecto/migration/auto.ex

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ defmodule Ecto.Migration.Auto do
8181
end
8282
end
8383

84+
def migrated?(repo, model) do
85+
tablename = model.__schema__(:source)
86+
try do
87+
query = from t in Ecto.Migration.SystemTable, select: t, where: t.tablename == ^tablename
88+
case repo.all(query) do
89+
[] -> false
90+
[%Ecto.Migration.SystemTable{tablename: tablename}] -> true
91+
end
92+
catch _, _ ->
93+
false
94+
end
95+
end
96+
8497
defp get_tablename(_module, tablename, []) do
8598
{nil, tablename}
8699
end

test/ecto_migrate_test.exs

+2
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,7 @@ defmodule EctoMigrateTest do
7272
assert tags.id == 1
7373
assert tags.model == "Elixir.MyModel"
7474
assert tags.name == "test_tag"
75+
76+
assert true == Ecto.Migration.Auto.migrated?(Repo, TestModel)
7577
end
7678
end

0 commit comments

Comments
 (0)