Skip to content

Commit

Permalink
Update SchemaDumper patch
Browse files Browse the repository at this point in the history
Fixes `options` hash is incorrectly nested.
  • Loading branch information
gussan committed Jan 7, 2017
1 parent f9d153b commit 31f9fb1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 77 deletions.
84 changes: 8 additions & 76 deletions lib/active_record/turntable/active_record_ext/schema_dumper.rb
Expand Up @@ -2,93 +2,25 @@
module ActiveRecord::Turntable
module ActiveRecordExt
module SchemaDumper
SEQUENCE_TABLE_REGEXP = /\A(.*)_id_seq\z/

private

# @note Override to dump database sequencer method
def table(table, stream)
columns = @connection.columns(table)
unless matchdata = table.match(SEQUENCE_TABLE_REGEXP)
return super
end

begin
tbl = StringIO.new

# first dump primary key column
if @connection.respond_to?(:primary_keys)
pk = @connection.primary_keys(table)
pk = pk.first unless pk.size > 1
else
pk = @connection.primary_key(table)
end

if table =~ /\A(.*)_id_seq\z/
tbl.print " create_sequence_for #{remove_prefix_and_suffix(Regexp.last_match(1)).inspect}"
else
tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}"
end

case pk
when String
tbl.print ", primary_key: #{pk.inspect}" unless pk == 'id'
pkcol = columns.detect { |c| c.name == pk }
pkcolspec = @connection.column_spec_for_primary_key(pkcol)
if pkcolspec.present?
pkcolspec.each do |key, value|
tbl.print ", #{key}: #{value}"
end
end
when Array
tbl.print ", primary_key: #{pk.inspect}"
else
tbl.print ", id: false"
end
tbl.print " create_sequence_for #{remove_prefix_and_suffix(matchdata[1]).inspect}"
tbl.print ", force: :cascade"

table_options = @connection.table_options(table)
tbl.print ", options: #{table_options.inspect}" unless table_options.blank?

if comment = @connection.table_comment(table).presence
tbl.print ", comment: #{comment.inspect}"
if table_options.present?
tbl.print ", #{format_options(table_options)}"
end

tbl.puts " do |t|"

# then dump all non-primary key columns
column_specs = columns.map do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
next if column.name == pk
@connection.column_spec(column)
end.compact

# find all migration keys used in this table
keys = @connection.migration_keys

# figure out the lengths for each column based on above keys
lengths = keys.map { |key|
column_specs.map { |spec|
spec[key] ? spec[key].length + 2 : 0
}.max
}

# the string we're going to sprintf our values against, with standardized column widths
format_string = lengths.map{ |len| "%-#{len}s" }

# find the max length for the 'type' column, which is special
type_length = column_specs.map{ |column| column[:type].length }.max

# add column type definition to our format string
format_string.unshift " t.%-#{type_length}s "

format_string *= ''

column_specs.each do |colspec|
values = keys.zip(lengths).map{ |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len }
values.unshift colspec[:type]
tbl.print((format_string % values).gsub(/,\s*$/, ''))
tbl.puts
end

indexes_in_create(table, tbl)

tbl.puts " end"
tbl.puts

tbl.rewind
Expand Down
@@ -0,0 +1,26 @@
require "spec_helper"

describe ActiveRecord::Turntable::ActiveRecordExt::SchemaDumper do
before(:all) do
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
ActiveRecord::SchemaDumper.prepend(ActiveRecord::Turntable::ActiveRecordExt::SchemaDumper)
end

before(:each) do
establish_connection_to(:test)
end

def dump_schema
stream = StringIO.new
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
stream.string
end

context "#dump" do
subject { dump_schema }
it { is_expected.to match(/create_table "users", force: :cascade, options: ".*?"\sdo\s\|t\|$/) }
it { is_expected.to match(/create_sequence_for "users", force: :cascade, options: /) }
it { is_expected.not_to match(/create_table "users_id_seq"/) }
it { is_expected.not_to match(/create_sequence_for "users_id_seq".*?do/) }
end
end
2 changes: 1 addition & 1 deletion spec/support/turntable_helper.rb
Expand Up @@ -19,7 +19,7 @@ def establish_connection_to(env = :test)

def truncate_shard
ActiveRecord::Base.descendants.each do |klass|
next if klass.abstract_class?
next if klass.abstract_class? || klass == ActiveRecord::SchemaMigration
klass.delete_all
end
end
Expand Down

0 comments on commit 31f9fb1

Please sign in to comment.