Skip to content

Commit

Permalink
Fix SchemaCache for rails HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Feb 21, 2024
1 parent ee4005e commit f2fe3c9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
12 changes: 5 additions & 7 deletions lib/online_migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "online_migrations/change_column_type_helpers"
require "online_migrations/background_migrations/migration_helpers"
require "online_migrations/schema_statements"
require "online_migrations/schema_cache"
require "online_migrations/migration"
require "online_migrations/migrator"
require "online_migrations/schema_dumper"
Expand All @@ -29,11 +30,6 @@ class UnsafeMigration < Error; end
autoload :CommandChecker
autoload :BackgroundMigration

autoload_at "online_migrations/schema_cache" do
autoload :SchemaCache
autoload :SchemaCache7
end

autoload_at "online_migrations/lock_retrier" do
autoload :LockRetrier
autoload :ConstantLockRetrier
Expand Down Expand Up @@ -102,8 +98,10 @@ def load
ActiveRecord::Tasks::DatabaseTasks.singleton_class.prepend(OnlineMigrations::DatabaseTasks)
ActiveRecord::Migration::CommandRecorder.include(OnlineMigrations::CommandRecorder)

if OnlineMigrations::Utils.ar_version >= 7.1
ActiveRecord::ConnectionAdapters::SchemaCache.prepend(OnlineMigrations::SchemaCache7)
if OnlineMigrations::Utils.ar_version >= 7.2
ActiveRecord::ConnectionAdapters::SchemaCache.prepend(OnlineMigrations::SchemaCache72)
elsif OnlineMigrations::Utils.ar_version >= 7.1
ActiveRecord::ConnectionAdapters::SchemaCache.prepend(OnlineMigrations::SchemaCache71)
else
ActiveRecord::ConnectionAdapters::SchemaCache.prepend(OnlineMigrations::SchemaCache)
end
Expand Down
80 changes: 79 additions & 1 deletion lib/online_migrations/schema_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def duplicate_column(old_column_name, new_column_name, columns)
end

# @private
module SchemaCache7
module SchemaCache71
# Active Record >= 7.1 changed signature of the methods,
# see https://github.com/rails/rails/pull/48716.
def primary_keys(connection, table_name)
Expand Down Expand Up @@ -154,4 +154,82 @@ def duplicate_column(old_column_name, new_column_name, columns)
columns << new_column.freeze
end
end

# @private
module SchemaCache72
# Active Record >= 7.2 changed signature of the methods,
# see https://github.com/rails/rails/pull/48716.
def primary_keys(pool, table_name)
if (renamed_table = renamed_table?(pool, table_name))
super(pool, renamed_table)
elsif renamed_column?(pool, table_name)
super(pool, column_rename_table(table_name))
else
super
end
end

def columns(pool, table_name)
if (renamed_table = renamed_table?(pool, table_name))
super(pool, renamed_table)
elsif renamed_column?(pool, table_name)
columns = super(pool, column_rename_table(table_name))
OnlineMigrations.config.column_renames[table_name].each do |old_column_name, new_column_name|
duplicate_column(old_column_name, new_column_name, columns)
end
columns
else
super.reject { |column| column.name.end_with?("_for_type_change") }
end
end

def indexes(pool, table_name)
if (renamed_table = renamed_table?(pool, table_name))
super(pool, renamed_table)
elsif renamed_column?(pool, table_name)
super(pool, column_rename_table(table_name))
else
super
end
end

def clear_data_source_cache!(pool, name)
if (renamed_table = renamed_table?(pool, name))
super(pool, renamed_table)
end

if renamed_column?(pool, name)
super(pool, column_rename_table(name))
end

super
end

private
def renamed_table?(pool, table_name)
table_renames = OnlineMigrations.config.table_renames
if table_renames.key?(table_name)
views = pool.with_connection(&:views)
table_renames[table_name] if views.include?(table_name)
end
end

def renamed_column?(pool, table_name)
column_renames = OnlineMigrations.config.column_renames
column_renames.key?(table_name) && pool.with_connection(&:views).include?(table_name)
end

def column_rename_table(table_name)
"#{table_name}_column_rename"
end

def duplicate_column(old_column_name, new_column_name, columns)
old_column = columns.find { |column| column.name == old_column_name }
new_column = old_column.dup
# Active Record defines only reader for :name
new_column.instance_variable_set(:@name, new_column_name)
# Correspond to the Active Record freezing of each column
columns << new_column.freeze
end
end
end

0 comments on commit f2fe3c9

Please sign in to comment.