Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change migration prefix type to string #594

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
10 changes: 5 additions & 5 deletions lib/ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ defmodule Ecto.Migration do

@type t :: %__MODULE__{
table: String.t(),
prefix: atom,
prefix: String.t() | nil,
name: atom,
columns: [atom | String.t()],
unique: boolean,
Expand All @@ -420,7 +420,7 @@ defmodule Ecto.Migration do

@type t :: %__MODULE__{
name: String.t(),
prefix: atom | nil,
prefix: String.t() | nil,
comment: String.t() | nil,
primary_key: boolean | keyword(),
engine: atom,
Expand Down Expand Up @@ -453,15 +453,15 @@ defmodule Ecto.Migration do
"""
@type t :: %__MODULE__{
table: String.t(),
prefix: atom | nil,
prefix: String.t() | nil,
column: atom,
type: atom,
on_delete: atom,
on_update: atom,
validate: boolean,
with: list,
match: atom | nil,
options: [{:prefix, atom | nil}]
options: [{:prefix, String.t() | nil}]
}
end

Expand All @@ -482,7 +482,7 @@ defmodule Ecto.Migration do
@type t :: %__MODULE__{
name: atom,
table: String.t(),
prefix: atom | nil,
prefix: String.t() | nil,
check: String.t() | nil,
exclude: String.t() | nil,
comment: String.t() | nil,
Expand Down
22 changes: 11 additions & 11 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ defmodule Ecto.Adapters.MyXQLTest do

test "create table with prefix" do
create =
{:create, table(:posts, prefix: :foo),
{:create, table(:posts, prefix: "foo"),
[{:add, :category_0, %Reference{table: :categories}, []}]}

assert execute_ddl(create) == [
Expand All @@ -1623,7 +1623,7 @@ defmodule Ecto.Adapters.MyXQLTest do

test "create table with comment on columns and table" do
create =
{:create, table(:posts, comment: "comment", prefix: :foo),
{:create, table(:posts, comment: "comment", prefix: "foo"),
[
{:add, :category_0, %Reference{table: :categories}, [comment: "column comment"]},
{:add, :created_at, :datetime, []},
Expand Down Expand Up @@ -1662,7 +1662,7 @@ defmodule Ecto.Adapters.MyXQLTest do
[null: false]},
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []},
{:add, :category_5,
%Reference{table: :categories, options: [prefix: :foo], on_delete: :nilify_all}, []},
%Reference{table: :categories, options: [prefix: "foo"], on_delete: :nilify_all}, []},
{:add, :category_6,
%Reference{table: :categories, with: [here: :there], on_delete: :nilify_all}, []}
]}
Expand Down Expand Up @@ -1901,22 +1901,22 @@ defmodule Ecto.Adapters.MyXQLTest do
end

test "drop table with prefixes" do
drop = {:drop, table(:posts, prefix: :foo), :restrict}
drop = {:drop, table(:posts, prefix: "foo"), :restrict}
assert execute_ddl(drop) == [~s|DROP TABLE `foo`.`posts`|]
end

test "drop constraint" do
assert_raise ArgumentError, ~r/MySQL adapter does not support constraints/, fn ->
execute_ddl(
{:drop, constraint(:products, "price_must_be_positive", prefix: :foo), :restrict}
{:drop, constraint(:products, "price_must_be_positive", prefix: "foo"), :restrict}
)
end
end

test "drop_if_exists constraint" do
assert_raise ArgumentError, ~r/MySQL adapter does not support constraints/, fn ->
execute_ddl(
{:drop_if_exists, constraint(:products, "price_must_be_positive", prefix: :foo),
{:drop_if_exists, constraint(:products, "price_must_be_positive", prefix: "foo"),
:restrict}
)
end
Expand Down Expand Up @@ -2008,7 +2008,7 @@ defmodule Ecto.Adapters.MyXQLTest do

test "alter table with prefix" do
alter =
{:alter, table(:posts, prefix: :foo),
{:alter, table(:posts, prefix: "foo"),
[
{:add, :author_id, %Reference{table: :author}, []},
{:modify, :permalink_id, %Reference{table: :permalinks}, null: false}
Expand Down Expand Up @@ -2071,7 +2071,7 @@ defmodule Ecto.Adapters.MyXQLTest do
end

test "create index with prefix" do
create = {:create, index(:posts, [:category_id, :permalink], prefix: :foo)}
create = {:create, index(:posts, [:category_id, :permalink], prefix: "foo")}

assert execute_ddl(create) ==
[
Expand Down Expand Up @@ -2128,7 +2128,7 @@ defmodule Ecto.Adapters.MyXQLTest do
end

test "drop index with prefix" do
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: :foo), :restrict}
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: "foo"), :restrict}
assert execute_ddl(drop) == [~s|DROP INDEX `posts$main` ON `foo`.`posts`|]
end

Expand All @@ -2146,7 +2146,7 @@ defmodule Ecto.Adapters.MyXQLTest do
end

test "rename table with prefix" do
rename = {:rename, table(:posts, prefix: :foo), table(:new_posts, prefix: :foo)}
rename = {:rename, table(:posts, prefix: "foo"), table(:new_posts, prefix: "foo")}
assert execute_ddl(rename) == [~s|RENAME TABLE `foo`.`posts` TO `foo`.`new_posts`|]
end

Expand All @@ -2159,7 +2159,7 @@ defmodule Ecto.Adapters.MyXQLTest do
end

test "rename column in prefixed table" do
rename = {:rename, table(:posts, prefix: :foo), :given_name, :first_name}
rename = {:rename, table(:posts, prefix: "foo"), :given_name, :first_name}

assert execute_ddl(rename) == [
~s|ALTER TABLE `foo`.`posts` RENAME COLUMN `given_name` TO `first_name`|
Expand Down
24 changes: 12 additions & 12 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ defmodule Ecto.Adapters.PostgresTest do

test "create table with prefix" do
create =
{:create, table(:posts, prefix: :foo),
{:create, table(:posts, prefix: "foo"),
[{:add, :category_0, %Reference{table: :categories}, []}]}

assert execute_ddl(create) == [
Expand Down Expand Up @@ -2406,15 +2406,15 @@ defmodule Ecto.Adapters.PostgresTest do
end

test "drop table with prefix" do
drop = {:drop, table(:posts, prefix: :foo), :restrict}
drop = {:drop, table(:posts, prefix: "foo"), :restrict}
assert execute_ddl(drop) == [~s|DROP TABLE "foo"."posts"|]
end

test "drop table with cascade" do
drop = {:drop, table(:posts), :cascade}
assert execute_ddl(drop) == [~s|DROP TABLE "posts" CASCADE|]

drop = {:drop, table(:posts, prefix: :foo), :cascade}
drop = {:drop, table(:posts, prefix: "foo"), :cascade}
assert execute_ddl(drop) == [~s|DROP TABLE "foo"."posts" CASCADE|]
end

Expand Down Expand Up @@ -2518,7 +2518,7 @@ defmodule Ecto.Adapters.PostgresTest do

test "alter table with prefix" do
alter =
{:alter, table(:posts, prefix: :foo),
{:alter, table(:posts, prefix: "foo"),
[
{:add, :author_id, %Reference{table: :author}, []},
{:modify, :permalink_id, %Reference{table: :permalinks}, null: false}
Expand Down Expand Up @@ -2586,22 +2586,22 @@ defmodule Ecto.Adapters.PostgresTest do
end

test "create index with prefix" do
create = {:create, index(:posts, [:category_id, :permalink], prefix: :foo)}
create = {:create, index(:posts, [:category_id, :permalink], prefix: "foo")}

assert execute_ddl(create) ==
[
~s|CREATE INDEX "posts_category_id_permalink_index" ON "foo"."posts" ("category_id", "permalink")|
]

create = {:create, index(:posts, ["lower(permalink)"], name: "posts$main", prefix: :foo)}
create = {:create, index(:posts, ["lower(permalink)"], name: "posts$main", prefix: "foo")}

assert execute_ddl(create) ==
[~s|CREATE INDEX "posts$main" ON "foo"."posts" (lower(permalink))|]
end

test "create index with comment" do
create =
{:create, index(:posts, [:category_id, :permalink], prefix: :foo, comment: "comment")}
{:create, index(:posts, [:category_id, :permalink], prefix: "foo", comment: "comment")}

assert execute_ddl(create) == [
remove_newlines("""
Expand Down Expand Up @@ -2727,7 +2727,7 @@ defmodule Ecto.Adapters.PostgresTest do
end

test "drop index with prefix" do
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: :foo), :restrict}
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: "foo"), :restrict}
assert execute_ddl(drop) == [~s|DROP INDEX "foo"."posts$main"|]
end

Expand All @@ -2741,7 +2741,7 @@ defmodule Ecto.Adapters.PostgresTest do
drop = {:drop, index(:posts, [:id], name: "posts$main"), :cascade}
assert execute_ddl(drop) == [~s|DROP INDEX "posts$main" CASCADE|]

drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: :foo), :cascade}
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: "foo"), :cascade}
assert execute_ddl(drop) == [~s|DROP INDEX "foo"."posts$main" CASCADE|]
end

Expand All @@ -2755,7 +2755,7 @@ defmodule Ecto.Adapters.PostgresTest do

test "rename index with prefix" do
rename =
{:rename, index(:people, [:name], name: "persons_name_index", prefix: :foo),
{:rename, index(:people, [:name], name: "persons_name_index", prefix: "foo"),
"people_name_index"}

assert execute_ddl(rename) == [
Expand Down Expand Up @@ -2868,7 +2868,7 @@ defmodule Ecto.Adapters.PostgresTest do
end

test "rename table with prefix" do
rename = {:rename, table(:posts, prefix: :foo), table(:new_posts, prefix: :foo)}
rename = {:rename, table(:posts, prefix: "foo"), table(:new_posts, prefix: "foo")}
assert execute_ddl(rename) == [~s|ALTER TABLE "foo"."posts" RENAME TO "new_posts"|]
end

Expand All @@ -2878,7 +2878,7 @@ defmodule Ecto.Adapters.PostgresTest do
end

test "rename column in prefixed table" do
rename = {:rename, table(:posts, prefix: :foo), :given_name, :first_name}
rename = {:rename, table(:posts, prefix: "foo"), :given_name, :first_name}

assert execute_ddl(rename) == [
~s|ALTER TABLE "foo"."posts" RENAME "given_name" TO "first_name"|
Expand Down
22 changes: 11 additions & 11 deletions test/ecto/adapters/tds_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ defmodule Ecto.Adapters.TdsTest do

test "create table with prefix" do
create =
{:create, table(:posts, prefix: :foo),
{:create, table(:posts, prefix: "foo"),
[{:add, :category_0, %Reference{table: :categories}, []}]}

assert execute_ddl(create) == [
Expand All @@ -1458,7 +1458,7 @@ defmodule Ecto.Adapters.TdsTest do
[null: false]},
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []},
{:add, :category_5,
%Reference{table: :categories, options: [prefix: :foo], on_delete: :nilify_all}, []},
%Reference{table: :categories, options: [prefix: "foo"], on_delete: :nilify_all}, []},
{:add, :category_6,
%Reference{table: :categories, with: [here: :there], on_delete: :nilify_all}, []}
]}
Expand Down Expand Up @@ -1593,7 +1593,7 @@ defmodule Ecto.Adapters.TdsTest do
end

test "drop table with prefixes" do
drop = {:drop, table(:posts, prefix: :foo), :restrict}
drop = {:drop, table(:posts, prefix: "foo"), :restrict}
assert execute_ddl(drop) == ["DROP TABLE [foo].[posts]; "]
end

Expand Down Expand Up @@ -1766,7 +1766,7 @@ defmodule Ecto.Adapters.TdsTest do
end

test "rename table with prefix" do
rename = {:rename, table(:posts, prefix: :foo), table(:new_posts, prefix: :foo)}
rename = {:rename, table(:posts, prefix: "foo"), table(:new_posts, prefix: "foo")}
assert execute_ddl(rename) == [~s|EXEC sp_rename 'foo.posts', 'foo.new_posts'|]
end

Expand All @@ -1778,7 +1778,7 @@ defmodule Ecto.Adapters.TdsTest do
end

test "rename column in table with prefixes" do
rename = {:rename, table(:posts, prefix: :foo), :given_name, :first_name}
rename = {:rename, table(:posts, prefix: "foo"), :given_name, :first_name}

assert execute_ddl(rename) ==
["EXEC sp_rename 'foo.posts.given_name', 'first_name', 'COLUMN'"]
Expand All @@ -1803,7 +1803,7 @@ defmodule Ecto.Adapters.TdsTest do
end

test "create index with prefix" do
create = {:create, index(:posts, [:category_id, :permalink], prefix: :foo)}
create = {:create, index(:posts, [:category_id, :permalink], prefix: "foo")}

assert execute_ddl(create) ==
[
Expand All @@ -1812,7 +1812,7 @@ defmodule Ecto.Adapters.TdsTest do
end

test "create index with prefix if not exists" do
create = {:create_if_not_exists, index(:posts, [:category_id, :permalink], prefix: :foo)}
create = {:create_if_not_exists, index(:posts, [:category_id, :permalink], prefix: "foo")}

assert execute_ddl(create) ==
[
Expand All @@ -1839,7 +1839,7 @@ defmodule Ecto.Adapters.TdsTest do
assert execute_ddl(create) ==
[~s|CREATE UNIQUE INDEX [posts_permalink_index] ON [posts] ([permalink]);|]

create = {:create, index(:posts, [:permalink], unique: true, prefix: :foo)}
create = {:create, index(:posts, [:permalink], unique: true, prefix: "foo")}

assert execute_ddl(create) ==
[~s|CREATE UNIQUE INDEX [posts_permalink_index] ON [foo].[posts] ([permalink]);|]
Expand All @@ -1866,7 +1866,7 @@ defmodule Ecto.Adapters.TdsTest do

test "drop index with prefix" do
drop =
{:drop, index(:posts, [:id], name: "posts_category_id_permalink_index", prefix: :foo),
{:drop, index(:posts, [:id], name: "posts_category_id_permalink_index", prefix: "foo"),
:restrict}

assert execute_ddl(drop) ==
Expand All @@ -1876,7 +1876,7 @@ defmodule Ecto.Adapters.TdsTest do
test "drop index with prefix if exists" do
drop =
{:drop_if_exists,
index(:posts, [:id], name: "posts_category_id_permalink_index", prefix: :foo), :restrict}
index(:posts, [:id], name: "posts_category_id_permalink_index", prefix: "foo"), :restrict}

assert execute_ddl(drop) ==
[
Expand All @@ -1892,7 +1892,7 @@ defmodule Ecto.Adapters.TdsTest do

drop_cascade =
{:drop_if_exists,
index(:posts, [:id], name: "posts_category_id_permalink_index", prefix: :foo), :cascade}
index(:posts, [:id], name: "posts_category_id_permalink_index", prefix: "foo"), :cascade}

assert_raise ArgumentError, ~r"MSSQL does not support `CASCADE` in DROP INDEX commands", fn ->
execute_ddl(drop_cascade)
Expand Down
12 changes: 6 additions & 6 deletions test/ecto/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,20 @@ defmodule Ecto.MigrationTest do
assert references(:posts, type: :uuid, column: :other) ==
%Reference{table: "posts", column: :other, type: :uuid, prefix: nil}

assert references(:posts, type: :uuid, column: :other, prefix: :blog) ==
assert references(:posts, type: :uuid, column: :other, prefix: "blog") ==
%Reference{
table: "posts",
column: :other,
type: :uuid,
prefix: :blog,
options: [prefix: :blog]
prefix: "blog",
options: [prefix: "blog"]
}
end

@tag repo_config: [migration_foreign_key: [type: :uuid, column: :other, prefix: :blog]]
@tag repo_config: [migration_foreign_key: [type: :uuid, column: :other, prefix: "blog"]]
test "create a reference with using the foreign key repo config" do
assert references(:posts) ==
%Reference{table: "posts", column: :other, type: :uuid, prefix: :blog}
%Reference{table: "posts", column: :other, type: :uuid, prefix: "blog"}
end

@tag repo_config: [migration_primary_key: [type: :binary_id]]
Expand Down Expand Up @@ -711,7 +711,7 @@ defmodule Ecto.MigrationTest do
assert table.prefix == :foo
end

@tag prefix: :bar
@tag prefix: "bar"
test "raise error when prefixes don't match" do
assert_raise Ecto.MigrationError,
"the :prefix option `foo` does not match the migrator prefix `bar`",
Expand Down
Loading
Loading