diff options
author | Jasper Maes <jaspermaes.jm@gmail.com> | 2018-08-29 21:29:18 +0200 |
---|---|---|
committer | Jasper Maes <jaspermaes.jm@gmail.com> | 2018-08-29 21:56:18 +0200 |
commit | fc59df67e278b5a874c6ce258038d277acc100d1 (patch) | |
tree | 384b87e16500e2328c07ca6101e96ee3305fbc23 /config/initializers | |
parent | 380c7169cbcc67b4046bc63b62fd1c452c76ad12 (diff) | |
download | gitlab-ce-fc59df67e278b5a874c6ce258038d277acc100d1.tar.gz |
Rails 5: include opclasses in rails 5 schema dump
Diffstat (limited to 'config/initializers')
-rw-r--r-- | config/initializers/postgresql_opclasses_support.rb | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/config/initializers/postgresql_opclasses_support.rb b/config/initializers/postgresql_opclasses_support.rb index 7b8afc78817..12a0770a455 100644 --- a/config/initializers/postgresql_opclasses_support.rb +++ b/config/initializers/postgresql_opclasses_support.rb @@ -144,7 +144,10 @@ module ActiveRecord [column, opclass] if opclass end.compact] - IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where, nil, using, opclasses) + index_attrs = [table_name, index_name, unique, column_names, [], orders, where, nil, using, opclasses] + index_attrs.insert(-2, nil) if Gitlab.rails5? # include index comment for Rails 5 + + IndexDefinition.new(*index_attrs) end end.compact end @@ -172,29 +175,38 @@ module ActiveRecord def indexes(table, stream) if (indexes = @connection.indexes(table)).any? add_index_statements = indexes.map do |index| - statement_parts = [ - "add_index #{remove_prefix_and_suffix(index.table).inspect}", - index.columns.inspect, - "name: #{index.name.inspect}", - ] - statement_parts << 'unique: true' if index.unique - - index_lengths = (index.lengths || []).compact - statement_parts << "length: #{Hash[index.columns.zip(index.lengths)].inspect}" if index_lengths.any? - - index_orders = index.orders || {} - statement_parts << "order: #{index.orders.inspect}" if index_orders.any? - statement_parts << "where: #{index.where.inspect}" if index.where - statement_parts << "using: #{index.using.inspect}" if index.using - statement_parts << "type: #{index.type.inspect}" if index.type - statement_parts << "opclasses: #{index.opclasses}" if index.opclasses.present? - - " #{statement_parts.join(', ')}" + table_name = remove_prefix_and_suffix(index.table).inspect + " add_index #{([table_name]+index_parts(index)).join(', ')}" end stream.puts add_index_statements.sort.join("\n") stream.puts end end + + def indexes_in_create(table, stream) + if (indexes = @connection.indexes(table)).any? + index_statements = indexes.map do |index| + " t.index #{index_parts(index).join(', ')}" + end + stream.puts index_statements.sort.join("\n") + end + end + + def index_parts(index) + index_parts = [ + index.columns.inspect, + "name: #{index.name.inspect}", + ] + index_parts << "unique: true" if index.unique + index_parts << "length: { #{format_options(index.lengths)} }" if index.lengths.present? + index_parts << "order: { #{format_options(index.orders)} }" if index.orders.present? + index_parts << "where: #{index.where.inspect}" if index.where + index_parts << "using: #{index.using.inspect}" if index.using + index_parts << "type: #{index.type.inspect}" if index.type + index_parts << "opclasses: #{index.opclasses.inspect}" if index.opclasses.present? + index_parts << "comment: #{index.comment.inspect}" if Gitlab.rails5? && index.comment + index_parts + end end end |