|
| 1 | +defmodule Ecto.Integration.MigrationsTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias Ecto.Integration.PoolRepo |
| 5 | + import ExUnit.CaptureLog |
| 6 | + |
| 7 | + @moduletag :capture_log |
| 8 | + @base_migration 3_000_000 |
| 9 | + |
| 10 | + defmodule NormalMigration do |
| 11 | + use Ecto.Migration |
| 12 | + |
| 13 | + def change do |
| 14 | + create_if_not_exists table(:log_mode_table) |
| 15 | + |
| 16 | + alter table(:log_mode_table) do |
| 17 | + add :name, :text |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + defmodule RollbackMigration do |
| 23 | + use Ecto.Migration |
| 24 | + |
| 25 | + def change do |
| 26 | + create_if_not_exists table(:log_mode_table) |
| 27 | + |
| 28 | + alter table(:log_mode_table) do |
| 29 | + remove :not_exists, :text |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe "Migrator" do |
| 35 | + @create_table_sql ~s(CREATE TABLE IF NOT EXISTS "log_mode_table") |
| 36 | + @create_table_log "create table if not exists log_mode_table" |
| 37 | + @add_column_sql ~S(ALTER TABLE "log_mode_table" ADD COLUMN "name" TEXT) |
| 38 | + @alter_table_log "alter table log_mode_table" |
| 39 | + @drop_column_sql ~S(ALTER TABLE "log_mode_table" DROP COLUMN "name") |
| 40 | + @drop_table_sql ~s(DROP TABLE IF EXISTS "log_mode_table") |
| 41 | + @drop_table_log "drop table if exists log_mode_table" |
| 42 | + @version_insert ~s(INSERT INTO "schema_migrations") |
| 43 | + @version_delete ~s(DELETE FROM "schema_migrations") |
| 44 | + |
| 45 | + test "logs transaction commands" do |
| 46 | + num = @base_migration + System.unique_integer([:positive]) |
| 47 | + up_log = |
| 48 | + capture_log(fn -> |
| 49 | + Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info) |
| 50 | + end) |
| 51 | + |
| 52 | + assert up_log =~ "begin []" |
| 53 | + assert up_log =~ @create_table_sql |
| 54 | + assert up_log =~ @create_table_log |
| 55 | + assert up_log =~ @add_column_sql |
| 56 | + assert up_log =~ @alter_table_log |
| 57 | + assert up_log =~ @version_insert |
| 58 | + assert up_log =~ "commit []" |
| 59 | + |
| 60 | + # two columns in the table |
| 61 | + assert %{num_rows: 2} = PoolRepo.query!("PRAGMA table_info(log_mode_table)", []) |
| 62 | + |
| 63 | + down_log = |
| 64 | + capture_log(fn -> |
| 65 | + Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info) |
| 66 | + end) |
| 67 | + |
| 68 | + assert down_log =~ "begin []" |
| 69 | + assert down_log =~ @drop_column_sql |
| 70 | + assert down_log =~ @drop_table_sql |
| 71 | + assert down_log =~ @drop_table_log |
| 72 | + assert down_log =~ @version_delete |
| 73 | + assert down_log =~ "commit []" |
| 74 | + end |
| 75 | + |
| 76 | + test "does not log sql when log is default" do |
| 77 | + num = @base_migration + System.unique_integer([:positive]) |
| 78 | + up_log = |
| 79 | + capture_log(fn -> |
| 80 | + Ecto.Migrator.up(PoolRepo, num, NormalMigration, log: :info) |
| 81 | + end) |
| 82 | + |
| 83 | + refute up_log =~ "begin []" |
| 84 | + refute up_log =~ @create_table_sql |
| 85 | + assert up_log =~ @create_table_log |
| 86 | + refute up_log =~ @add_column_sql |
| 87 | + assert up_log =~ @alter_table_log |
| 88 | + refute up_log =~ @version_insert |
| 89 | + refute up_log =~ "commit []" |
| 90 | + |
| 91 | + down_log = |
| 92 | + capture_log(fn -> |
| 93 | + Ecto.Migrator.down(PoolRepo, num, NormalMigration, log: :info) |
| 94 | + end) |
| 95 | + |
| 96 | + refute down_log =~ "begin []" |
| 97 | + refute down_log =~ @drop_column_sql |
| 98 | + refute down_log =~ @drop_table_sql |
| 99 | + assert down_log =~ @drop_table_log |
| 100 | + refute down_log =~ @version_delete |
| 101 | + refute down_log =~ "commit []" |
| 102 | + end |
| 103 | + |
| 104 | + test "rolling back undoes previous migrations" do |
| 105 | + num = @base_migration + System.unique_integer([:positive]) |
| 106 | + up_log = |
| 107 | + capture_log(fn -> |
| 108 | + try do |
| 109 | + Ecto.Migrator.up(PoolRepo, num, RollbackMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info) |
| 110 | + rescue |
| 111 | + _ -> :ok |
| 112 | + end |
| 113 | + end) |
| 114 | + |
| 115 | + assert up_log =~ "begin []" |
| 116 | + assert up_log =~ @create_table_sql |
| 117 | + assert up_log =~ @create_table_log |
| 118 | + assert up_log =~ "rollback []" |
| 119 | + refute up_log =~ @version_insert |
| 120 | + refute up_log =~ "commit []" |
| 121 | + |
| 122 | + # table was not created due to rollback |
| 123 | + assert %{num_rows: 0} = PoolRepo.query!("PRAGMA table_info(log_mode_table)", []) |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments