Skip to content

Commit

Permalink
Keep migrations consistent after Rails upgrades (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
thoughtbot-summer committed Dec 21, 2022
1 parent f24ef58 commit 2d20b28
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/pg_party/adapter_decorator.rb
Expand Up @@ -179,7 +179,7 @@ def table_partitioned?(table_name)

private

def create_partition(table_name, type, partition_key, **options)
def create_partition(table_name, type, partition_key, **options, &blk)
modified_options = options.except(:id, :primary_key, :template, :create_with_primary_key)
template = options.fetch(:template, PgParty.config.create_template_tables)
id = options.fetch(:id, :bigserial)
Expand All @@ -200,18 +200,18 @@ def create_partition(table_name, type, partition_key, **options)
end
modified_options[:options] = partition_by_clause(type, partition_key)

create_table(table_name, **modified_options) do |td|
migration_or_adapter(blk).create_table(table_name, **modified_options) do |td|
if !modified_options[:id] && id == :uuid
td.column(primary_key, id, null: false, default: uuid_function)
elsif !modified_options[:id] && id
td.column(primary_key, id, null: false)
end

yield(td) if block_given?
blk&.call(td)
end

# Rails 4 has a bug where uuid columns are always nullable
change_column_null(table_name, primary_key, false) if !modified_options[:id] && id == :uuid
migration_or_adapter(blk).change_column_null(table_name, primary_key, false) if !modified_options[:id] && id == :uuid

return unless template

Expand Down Expand Up @@ -455,5 +455,10 @@ def supports_partitions?
def postgres_major_version
__getobj__.send(:postgresql_version)/10000
end

def migration_or_adapter(blk)
blk_receiver = blk&.binding&.receiver
blk_receiver.is_a?(ActiveRecord::Migration) ? blk_receiver : self
end
end
end

0 comments on commit 2d20b28

Please sign in to comment.