|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +class UselessStatementLoggerTest < MiniTest::Unit::TestCase |
| 6 | + def setup |
| 7 | + SafePgMigrations.instance_variable_set(:@config, nil) |
| 8 | + @connection = ActiveRecord::Base.connection |
| 9 | + @verbose_was = ActiveRecord::Migration.verbose |
| 10 | + @connection.create_table(:schema_migrations) { |t| t.string :version } |
| 11 | + ActiveRecord::SchemaMigration.create_table |
| 12 | + ActiveRecord::Migration.verbose = false |
| 13 | + @connection.execute("SET statement_timeout TO '70s'") |
| 14 | + @connection.execute("SET lock_timeout TO '70s'") |
| 15 | + end |
| 16 | + |
| 17 | + def teardown |
| 18 | + ActiveRecord::SchemaMigration.drop_table |
| 19 | + @connection.execute('SET statement_timeout TO 0') |
| 20 | + @connection.execute("SET lock_timeout TO '30s'") |
| 21 | + @connection.drop_table(:messages, if_exists: true) |
| 22 | + @connection.drop_table(:users, if_exists: true) |
| 23 | + ActiveRecord::Migration.verbose = @verbose_was |
| 24 | + end |
| 25 | + |
| 26 | + def test_ddl_transactions |
| 27 | + @migration = |
| 28 | + Class.new(ActiveRecord::Migration::Current) do |
| 29 | + disable_ddl_transaction! |
| 30 | + |
| 31 | + def change |
| 32 | + create_table(:users) { |t| t.string :email } |
| 33 | + end |
| 34 | + end.new |
| 35 | + |
| 36 | + write_calls = record_calls(SafePgMigrations, :say) { run_migration }.map(&:first) |
| 37 | + |
| 38 | + assert_includes( |
| 39 | + write_calls, |
| 40 | + '/!\ No need to explicitly use `disable_ddl_transaction`, safe-pg-migrations does it for you' |
| 41 | + ) |
| 42 | + end |
| 43 | + |
| 44 | + def test_no_warning_when_no_ddl_transaction |
| 45 | + @migration = |
| 46 | + Class.new(ActiveRecord::Migration::Current) do |
| 47 | + def change |
| 48 | + create_table(:users) { |t| t.string :email } |
| 49 | + end |
| 50 | + end.new |
| 51 | + |
| 52 | + write_calls = record_calls(SafePgMigrations, :say) { run_migration }.map(&:first) |
| 53 | + |
| 54 | + refute_includes write_calls, '/!\ No need to explicitly disable DDL transaction, safe-pg-migrations does it for you' |
| 55 | + end |
| 56 | + |
| 57 | + def test_add_index_concurrently |
| 58 | + @connection.create_table(:users) { |t| t.string :email } |
| 59 | + @migration = |
| 60 | + Class.new(ActiveRecord::Migration::Current) do |
| 61 | + def change |
| 62 | + add_index :users, :email, algorithm: :concurrently |
| 63 | + end |
| 64 | + end.new |
| 65 | + |
| 66 | + write_calls = record_calls(SafePgMigrations, :say) { run_migration }.map(&:first) |
| 67 | + |
| 68 | + assert_includes( |
| 69 | + write_calls, |
| 70 | + '/!\ No need to explicitly use `algorithm: :concurrently`, safe-pg-migrations does it for you' |
| 71 | + ) |
| 72 | + end |
| 73 | + |
| 74 | + def test_no_warning_when_no_index_concurrently |
| 75 | + @connection.create_table(:users) { |t| t.string :email } |
| 76 | + @migration = |
| 77 | + Class.new(ActiveRecord::Migration::Current) do |
| 78 | + def change |
| 79 | + add_index :users, :email |
| 80 | + end |
| 81 | + end.new |
| 82 | + |
| 83 | + write_calls = record_calls(SafePgMigrations, :say) { run_migration }.map(&:first) |
| 84 | + |
| 85 | + refute_includes( |
| 86 | + write_calls, |
| 87 | + '/!\ No need to explicitly use `algorithm: :concurrently`, safe-pg-migrations does it for you' |
| 88 | + ) |
| 89 | + end |
| 90 | + |
| 91 | + def test_add_foreign_key_validate_false |
| 92 | + @connection.create_table(:users) { |t| t.string :email } |
| 93 | + @connection.create_table(:messages) do |t| |
| 94 | + t.string :message |
| 95 | + t.bigint :user_id |
| 96 | + end |
| 97 | + |
| 98 | + @migration = |
| 99 | + Class.new(ActiveRecord::Migration::Current) do |
| 100 | + def change |
| 101 | + add_foreign_key :messages, :users, validate: false |
| 102 | + end |
| 103 | + end.new |
| 104 | + |
| 105 | + write_calls = record_calls(SafePgMigrations, :say) { run_migration }.map(&:first) |
| 106 | + |
| 107 | + assert_includes( |
| 108 | + write_calls, |
| 109 | + '/!\ No need to explicitly use `validate: :false`, safe-pg-migrations does it for you' |
| 110 | + ) |
| 111 | + end |
| 112 | + |
| 113 | + def test_add_foreign_key_no_validation |
| 114 | + @connection.create_table(:users) { |t| t.string :email } |
| 115 | + @connection.create_table(:messages) do |t| |
| 116 | + t.string :message |
| 117 | + t.bigint :user_id |
| 118 | + end |
| 119 | + |
| 120 | + @migration = |
| 121 | + Class.new(ActiveRecord::Migration::Current) do |
| 122 | + def change |
| 123 | + add_foreign_key :messages, :users |
| 124 | + end |
| 125 | + end.new |
| 126 | + |
| 127 | + write_calls = record_calls(SafePgMigrations, :say) { run_migration }.map(&:first) |
| 128 | + |
| 129 | + refute_includes( |
| 130 | + write_calls, |
| 131 | + '/!\ No need to explicitly use `validate: :false`, safe-pg-migrations does it for you' |
| 132 | + ) |
| 133 | + end |
| 134 | +end |
0 commit comments