@@ -2,6 +2,7 @@ defmodule Ecto.Integration.MigrationsTest do
22 use ExUnit.Case , async: true
33
44 alias Ecto.Integration.PoolRepo
5+ import ExUnit.CaptureLog
56
67 @ moduletag :capture_log
78 @ base_migration 3_000_000
@@ -15,13 +16,112 @@ defmodule Ecto.Integration.MigrationsTest do
1516 end
1617 end
1718
19+ defmodule NormalMigration do
20+ use Ecto.Migration
21+
22+ def change do
23+ create_if_not_exists table ( :log_mode_table )
24+ end
25+ end
26+
1827 test "logs Postgres notice messages" do
1928 log =
20- ExUnit.CaptureLog . capture_log ( fn ->
29+ capture_log ( fn ->
2130 num = @ base_migration + System . unique_integer ( [ :positive ] )
2231 Ecto.Migrator . up ( PoolRepo , num , DuplicateTableMigration , log: false )
2332 end )
2433
2534 assert log =~ ~s( relation "duplicate_table" already exists, skipping)
2635 end
36+
37+ describe "Migrator" do
38+ @ get_lock_command ~s( LOCK TABLE "schema_migrations" IN SHARE UPDATE EXCLUSIVE MODE)
39+ @ create_table_sql ~s( CREATE TABLE IF NOT EXISTS "log_mode_table")
40+ @ create_table_log "create table if not exists log_mode_table"
41+ @ drop_table_sql ~s( DROP TABLE IF EXISTS "log_mode_table")
42+ @ drop_table_log "drop table if exists log_mode_table"
43+ @ version_insert ~s( INSERT INTO "schema_migrations")
44+ @ version_delete ~s( DELETE FROM "schema_migrations")
45+
46+ test "logs locking and transaction commands when log_all: true" do
47+ num = @ base_migration + System . unique_integer ( [ :positive ] )
48+ up_log =
49+ capture_log ( fn ->
50+ Ecto.Migrator . up ( PoolRepo , num , NormalMigration , log_all: true , log_sql: :info , log: :info )
51+ end )
52+
53+ assert Regex . scan ( ~r/ (begin \[ \] )/ , up_log ) |> length ( ) == 2
54+ assert up_log =~ @ get_lock_command
55+ assert up_log =~ @ create_table_sql
56+ assert up_log =~ @ create_table_log
57+ assert up_log =~ @ version_insert
58+ assert Regex . scan ( ~r/ (commit \[ \] )/ , up_log ) |> length ( ) == 2
59+
60+ down_log =
61+ capture_log ( fn ->
62+ Ecto.Migrator . down ( PoolRepo , num , NormalMigration , log_all: true , log_sql: :info , log: :info )
63+ end )
64+
65+ assert down_log =~ "begin []"
66+ assert down_log =~ @ get_lock_command
67+ assert down_log =~ @ drop_table_sql
68+ assert down_log =~ @ drop_table_log
69+ assert down_log =~ @ version_delete
70+ assert down_log =~ "commit []"
71+ end
72+
73+ test "does not log locking and transaction commands when log_sql is true" do
74+ num = @ base_migration + System . unique_integer ( [ :positive ] )
75+ up_log =
76+ capture_log ( fn ->
77+ Ecto.Migrator . up ( PoolRepo , num , NormalMigration , log_sql: :info , log: :info )
78+ end )
79+
80+ refute up_log =~ "begin []"
81+ refute up_log =~ @ get_lock_command
82+ assert up_log =~ @ create_table_sql
83+ assert up_log =~ @ create_table_log
84+ refute up_log =~ @ version_insert
85+ refute up_log =~ "commit []"
86+
87+ down_log =
88+ capture_log ( fn ->
89+ Ecto.Migrator . down ( PoolRepo , num , NormalMigration , log_sql: :info , log: :info )
90+ end )
91+
92+ refute down_log =~ "begin []"
93+ refute down_log =~ @ get_lock_command
94+ assert down_log =~ @ drop_table_sql
95+ assert down_log =~ @ drop_table_log
96+ refute down_log =~ @ version_delete
97+ refute down_log =~ "commit []"
98+ end
99+
100+ test ~s( does not log sql when log is default) do
101+ num = @ base_migration + System . unique_integer ( [ :positive ] )
102+ up_log =
103+ capture_log ( fn ->
104+ Ecto.Migrator . up ( PoolRepo , num , NormalMigration , log: :info )
105+ end )
106+
107+ refute up_log =~ "begin []"
108+ refute up_log =~ @ get_lock_command
109+ refute up_log =~ @ create_table_sql
110+ assert up_log =~ @ create_table_log
111+ refute up_log =~ @ version_insert
112+ refute up_log =~ "commit []"
113+
114+ down_log =
115+ capture_log ( fn ->
116+ Ecto.Migrator . down ( PoolRepo , num , NormalMigration , log: :info )
117+ end )
118+
119+ refute down_log =~ "begin []"
120+ refute down_log =~ @ get_lock_command
121+ refute down_log =~ @ drop_table_sql
122+ assert down_log =~ @ drop_table_log
123+ refute down_log =~ @ version_delete
124+ refute down_log =~ "commit []"
125+ end
126+ end
27127end
0 commit comments