Skip to content

Commit

Permalink
Do not stub raw connection internals
Browse files Browse the repository at this point in the history
A flaky test appeared because of this stubbing.
  • Loading branch information
fatkodima committed Oct 20, 2024
1 parent 7b901f5 commit 681ecd8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/online_migrations/change_column_type_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def initialize_columns_type_change(table_name, columns_and_types, **options)
type_cast_functions[column_name] = type_cast_function if type_cast_function
tmp_column_name = conversions[column_name]

if raw_connection.server_version >= 11_00_00
if database_version >= 11_00_00
if primary_key(table_name) == column_name.to_s && old_col.type == :integer
# For PG < 11 and Primary Key conversions, setting a column as the PK
# converts even check constraints to NOT NULL column constraints
Expand Down Expand Up @@ -497,7 +497,7 @@ def __copy_exclusion_constraints(table_name, from_column, to_column)
def __set_not_null(table_name, column_name)
# For PG >= 12 we can "promote" CHECK constraint to NOT NULL constraint:
# https://github.com/postgres/postgres/commit/bbb96c3704c041d139181c6601e5bc770e045d26
if raw_connection.server_version >= 12_00_00
if database_version >= 12_00_00
execute(<<~SQL)
ALTER TABLE #{quote_table_name(table_name)}
ALTER #{quote_column_name(column_name)}
Expand Down
4 changes: 2 additions & 2 deletions lib/online_migrations/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def swap_column_names(table_name, column1, column2)
def add_column_with_default(table_name, column_name, type, **options)
default = options.fetch(:default)

if raw_connection.server_version >= 11_00_00 && !Utils.volatile_default?(self, type, default)
if database_version >= 11_00_00 && !Utils.volatile_default?(self, type, default)
add_column(table_name, column_name, type, **options)
else
__ensure_not_in_transaction!
Expand All @@ -465,7 +465,7 @@ def add_column_with_default(table_name, column_name, type, **options)
add_not_null_constraint(table_name, column_name, validate: false)
validate_not_null_constraint(table_name, column_name)

if raw_connection.server_version >= 12_00_00
if database_version >= 12_00_00
# In PostgreSQL 12+ it is safe to "promote" a CHECK constraint to `NOT NULL` for the column
change_column_null(table_name, column_name, false)
remove_not_null_constraint(table_name, column_name)
Expand Down
4 changes: 2 additions & 2 deletions test/support/minitest_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def with_partial_writes(value, &block)
end

def with_postgres(major_version, &block)
pg_connection = ActiveRecord::Base.connection.raw_connection
pg_connection.stub(:server_version, major_version * 1_00_00, &block)
connection = ActiveRecord::Base.connection
connection.stub(:database_version, major_version * 1_00_00, &block)
end

def ar_version
Expand Down

0 comments on commit 681ecd8

Please sign in to comment.