|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +class SafePgMigrationsTest < Minitest::Test |
| 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(:users, if_exists: true) |
| 22 | + ActiveRecord::Migration.verbose = @verbose_was |
| 23 | + end |
| 24 | + |
| 25 | + def test_create_table |
| 26 | + @migration = |
| 27 | + Class.new(ActiveRecord::Migration::Current) do |
| 28 | + def change |
| 29 | + create_table(:users) do |t| |
| 30 | + t.string :email |
| 31 | + t.references :user, foreign_key: true |
| 32 | + end |
| 33 | + end |
| 34 | + end.new |
| 35 | + |
| 36 | + calls = record_calls(@connection, :execute) { run_migration } |
| 37 | + assert_calls [ |
| 38 | + "SET statement_timeout TO '5s'", |
| 39 | + |
| 40 | + # Create the table with constraints. |
| 41 | + 'CREATE TABLE "users" ("id" bigserial primary key, "email" character varying, "user_id" bigint, ' \ |
| 42 | + 'CONSTRAINT "fk_rails_6d0b8b3c2f" FOREIGN KEY ("user_id") REFERENCES "users" ("id") )', |
| 43 | + |
| 44 | + # Create the index. |
| 45 | + 'SET statement_timeout TO 0', |
| 46 | + "SET lock_timeout TO '30s'", |
| 47 | + 'CREATE INDEX CONCURRENTLY "index_users_on_user_id" ON "users" ("user_id")', |
| 48 | + "SET lock_timeout TO '5s'", |
| 49 | + "SET statement_timeout TO '5s'", |
| 50 | + |
| 51 | + "SET statement_timeout TO '70s'", |
| 52 | + ], calls |
| 53 | + |
| 54 | + run_migration(:down) |
| 55 | + refute @connection.table_exists?(:users) |
| 56 | + end |
| 57 | + |
| 58 | + def test_create_table_idempotence |
| 59 | + # Simulates an interruption between the table creation and the index creation |
| 60 | + @connection.create_table(:users) do |t| |
| 61 | + t.string :name, index: true |
| 62 | + t.string :email |
| 63 | + end |
| 64 | + |
| 65 | + @migration = |
| 66 | + Class.new(ActiveRecord::Migration::Current) do |
| 67 | + def change |
| 68 | + create_table(:users) do |t| |
| 69 | + t.string :name, index: true |
| 70 | + t.string :email, index: true |
| 71 | + end |
| 72 | + end |
| 73 | + end.new |
| 74 | + |
| 75 | + calls = record_calls(@connection, :execute) { run_migration } |
| 76 | + indexes = ActiveRecord::Base.connection.indexes :users |
| 77 | + refute_empty indexes |
| 78 | + assert_equal 'index_users_on_email', indexes.first.name |
| 79 | + |
| 80 | + refute_includes flat_calls(calls), 'CREATE INDEX CONCURRENTLY "index_users_on_name" ON "users" ("name")' |
| 81 | + |
| 82 | + assert_calls [ |
| 83 | + "SET statement_timeout TO '5s'", |
| 84 | + 'SET statement_timeout TO 0', |
| 85 | + "SET lock_timeout TO '30s'", |
| 86 | + "SET lock_timeout TO '5s'", |
| 87 | + "SET statement_timeout TO '5s'", |
| 88 | + 'SET statement_timeout TO 0', |
| 89 | + "SET lock_timeout TO '30s'", |
| 90 | + 'CREATE INDEX CONCURRENTLY "index_users_on_email" ON "users" ("email")', |
| 91 | + "SET lock_timeout TO '5s'", |
| 92 | + "SET statement_timeout TO '5s'", |
| 93 | + "SET statement_timeout TO '70s'", |
| 94 | + ], calls |
| 95 | + end |
| 96 | +end |
0 commit comments