Skip to content

Commit fd8d852

Browse files
authored
Add comment support on mysql (#369)
1 parent 13a8335 commit fd8d852

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

lib/ecto/adapters/myxql/connection.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ if Code.ensure_loaded?(MyXQL) do
731731
if_do(command == :create_if_not_exists, "IF NOT EXISTS "),
732732
quote_table(table.prefix, table.name),
733733
table_structure,
734+
comment_expr(table.comment, true),
734735
engine_expr(table.engine), options_expr(table.options)]]
735736
end
736737

@@ -743,6 +744,10 @@ if Code.ensure_loaded?(MyXQL) do
743744
def execute_ddl({:alter, %Table{} = table, changes}) do
744745
[["ALTER TABLE ", quote_table(table.prefix, table.name), ?\s,
745746
column_changes(table, changes), pk_definitions(changes, ", ADD ")]]
747+
++
748+
if_do(table.comment,
749+
[["ALTER TABLE ", quote_table(table.prefix, table.name), comment_expr(table.comment)]]
750+
)
746751
end
747752

748753
def execute_ddl({:create, %Index{} = index}) do
@@ -887,10 +892,16 @@ if Code.ensure_loaded?(MyXQL) do
887892
default = Keyword.fetch(opts, :default)
888893
null = Keyword.get(opts, :null)
889894
after_column = Keyword.get(opts, :after)
895+
comment = Keyword.get(opts, :comment)
890896

891-
[default_expr(default), null_expr(null), after_expr(after_column)]
897+
[default_expr(default), null_expr(null), after_expr(after_column), comment_expr(comment)]
892898
end
893899

900+
defp comment_expr(comment, create_table? \\ false)
901+
defp comment_expr(comment, true) when is_binary(comment), do: " COMMENT = '#{escape_string(comment)}'"
902+
defp comment_expr(comment, false) when is_binary(comment), do: " COMMENT '#{escape_string(comment)}'"
903+
defp comment_expr(_, _), do: []
904+
894905
defp after_expr(nil), do: []
895906
defp after_expr(column) when is_atom(column) or is_binary(column), do: " AFTER `#{column}`"
896907
defp after_expr(_), do: []

test/ecto/adapters/myxql_test.exs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ defmodule Ecto.Adapters.MyXQLTest do
242242
|> select([:id, :parent_id])
243243

244244
cte_query = initial_query |> union_all(^iteration_query)
245-
245+
246246
breadcrumbs_query =
247247
"tree"
248248
|> recursive_ctes(true)
@@ -1101,6 +1101,20 @@ defmodule Ecto.Adapters.MyXQLTest do
11011101
""" |> remove_newlines]
11021102
end
11031103

1104+
test "create table with comment on columns and table" do
1105+
create = {:create, table(:posts, comment: "comment", prefix: :foo),
1106+
[
1107+
{:add, :category_0, %Reference{table: :categories}, [comment: "column comment"]},
1108+
{:add, :created_at, :datetime, []},
1109+
{:add, :updated_at, :datetime, [comment: "column comment 2"]}
1110+
]}
1111+
assert execute_ddl(create) == ["""
1112+
CREATE TABLE `foo`.`posts` (`category_0` BIGINT UNSIGNED COMMENT 'column comment',
1113+
CONSTRAINT `posts_category_0_fkey` FOREIGN KEY (`category_0`) REFERENCES `foo`.`categories`(`id`),
1114+
`created_at` datetime, `updated_at` datetime COMMENT 'column comment 2') COMMENT = 'comment' ENGINE = INNODB
1115+
""" |> remove_newlines]
1116+
end
1117+
11041118
test "create table with engine" do
11051119
create = {:create, table(:posts, engine: :myisam),
11061120
[{:add, :id, :serial, [primary_key: true]}]}
@@ -1335,6 +1349,24 @@ defmodule Ecto.Adapters.MyXQLTest do
13351349
""" |> remove_newlines]
13361350
end
13371351

1352+
test "alter table with comments on table and columns" do
1353+
alter = {:alter, table(:posts, comment: "table comment"),
1354+
[{:add, :title, :string, [default: "Untitled", size: 100, null: false, comment: "column comment"]},
1355+
{:modify, :price, :numeric, [precision: 8, scale: 2, null: true]},
1356+
{:modify, :permalink_id, %Reference{table: :permalinks}, [null: false, comment: "column comment 2"]},
1357+
{:remove, :summary}]}
1358+
1359+
assert execute_ddl(alter) == ["""
1360+
ALTER TABLE `posts`
1361+
ADD `title` varchar(100) DEFAULT 'Untitled' NOT NULL COMMENT 'column comment',
1362+
MODIFY `price` numeric(8,2) NULL,
1363+
MODIFY `permalink_id` BIGINT UNSIGNED NOT NULL COMMENT 'column comment 2',
1364+
ADD CONSTRAINT `posts_permalink_id_fkey` FOREIGN KEY (`permalink_id`) REFERENCES `permalinks`(`id`),
1365+
DROP `summary`
1366+
""" |> remove_newlines,
1367+
~s|ALTER TABLE `posts` COMMENT 'table comment'|]
1368+
end
1369+
13381370
test "alter table with prefix" do
13391371
alter = {:alter, table(:posts, prefix: :foo),
13401372
[{:add, :author_id, %Reference{table: :author}, []},

0 commit comments

Comments
 (0)