Skip to content

Commit

Permalink
Merge 6819665 into 7974a67
Browse files Browse the repository at this point in the history
  • Loading branch information
urkle committed Dec 21, 2018
2 parents 7974a67 + 6819665 commit f4c9c96
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
42 changes: 31 additions & 11 deletions lib/schema_plus/core/active_record/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,36 @@ def table(table, _)
env.table.trailer = m[:trailer].split("\n").map(&:strip).reject{|s| s.blank?}
table_objects = m[:columns].strip.split("\n").map { |col|
cs = col.strip
result = nil
# first match expression index case
m = cs.match %r{
^
t\.(?<type>\S+) \s*
[:'"](?<name>[^"\s]+)[,"]? \s*
,? \s*
(?<options>.*)
$
}x
if !m.nil?
SchemaDump::Table::Column.new name: m[:name], type: m[:type], options: eval("{" + m[:options] + "}"), comments: []
else
^
t\.index \s*
"(?<index_cols>(?:[^"\\]|\\.)*?)" \s*
, \s*
name\: \s* [:'"](?<name>[^"\s]+)[,"]? \s*
,? \s*
(?<options>.*)
$
}x
unless m.nil?
index_cols = m[:index_cols].gsub('\\"', '"')
result = SchemaDump::Table::Index.new name: m[:name], columns: index_cols, options: eval("{" + m[:options] + "}")
end
if result.nil? # general matching of columns
m = cs.match %r{
^
t\.(?<type>\S+) \s*
[:'"](?<name>[^"\s]+)[,"]? \s*
,? \s*
(?<options>.*)
$
}x
unless m.nil?
result = SchemaDump::Table::Column.new name: m[:name], type: m[:type], options: eval("{" + m[:options] + "}"), comments: []
end
end
if result.nil? # index definitions with multiple columns
m = cs.match %r{
^
t\.index \s*
Expand All @@ -93,9 +112,10 @@ def table(table, _)
nil
else
index_cols = m[:index_cols].tr(%q{'":}, '').strip.split(/\s*,\s*/)
SchemaDump::Table::Index.new name: m[:name], columns: index_cols, options: eval("{#{m[:options]}}")
result = SchemaDump::Table::Index.new name: m[:name], columns: index_cols, options: eval("{#{m[:options]}}")
end
end
result
}.reject { |o| o.nil? }
env.table.columns = table_objects.select { |o| o.is_a? SchemaDump::Table::Column }
env.table.indexes = table_objects.select { |o| o.is_a? SchemaDump::Table::Index }
Expand Down
16 changes: 16 additions & 0 deletions spec/dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,29 @@ def after(env)

Then { expect(dump).to_not match(/create_table "inttable", id: :serial.*default:/m) }
end

context 'expression index handling' do
before(:each) do
migration.create_table "expressions" do |t|
t.string :field
t.string :column
t.index 'lower(field), id', name: 'index_expression_field'
t.index 'lower("column"), id', name: 'index_expression_column'
end
end

Then { expect(dump).to match(/index "lower.+field.+, id", :name=>"index_expression_field"/) }
Then { expect(dump).to match(/index "lower.+"column\\".+, id", :name=>"index_expression_column"/) }
end
end

context TestDumper::Middleware::Dumper::Table do
Then { expect(dump).to match(/t[.]integer.*:option=>"#{middleware}" \# comment: #{middleware}/) }
Then { expect(dump).to match(/statement: #{middleware}\s+end\s+(add_index.*)?\s+trailer: #{middleware}/) }
Then { expect(dump).to match(/could not dump table.*custom_table.*unknown type.*custom_type/mi) } if TestCustomType
Then { expect(dump).to match(/t[.]index.*:option=>"#{middleware}"/) }


end

private
Expand Down

0 comments on commit f4c9c96

Please sign in to comment.