Module | Sequel::SchemaDumper |
In: |
lib/sequel/extensions/schema_dumper.rb
|
Dump foreign key constraints for all tables as a migration. This complements the :foreign_keys=>false option to dump_schema_migration. This only dumps the constraints (not the columns) using alter_table/add_foreign_key with an array of columns.
Note that the migration this produces does not have a down block, so you cannot reverse it.
# File lib/sequel/extensions/schema_dumper.rb, line 22 22: def dump_foreign_key_migration(options=OPTS) 23: ts = tables(options) 24: "Sequel.migration do\n change do\n\#{ts.sort_by{|t| t.to_s}.map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join(\"\\n\\n\").gsub(/^/o, ' ')}\n end\nend\n" 25: end
Dump indexes for all tables as a migration. This complements the :indexes=>false option to dump_schema_migration. Options:
:same_db : | Create a dump for the same database type, so don‘t ignore errors if the index statements fail. |
:index_names : | If set to false, don‘t record names of indexes. If set to :namespace, prepend the table name to the index name if the database does not use a global index namespace. |
# File lib/sequel/extensions/schema_dumper.rb, line 41 41: def dump_indexes_migration(options=OPTS) 42: ts = tables(options) 43: "Sequel.migration do\n change do\n\#{ts.sort_by{|t| t.to_s}.map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join(\"\\n\\n\").gsub(/^/o, ' ')}\n end\nend\n" 44: end
Return a string that contains a Sequel::Migration subclass that when run would recreate the database structure. Options:
:same_db : | Don‘t attempt to translate database types to ruby types. If this isn‘t set to true, all database types will be translated to ruby types, but there is no guarantee that the migration generated will yield the same type. Without this set, types that aren‘t recognized will be translated to a string-like type. |
:foreign_keys : | If set to false, don‘t dump foreign_keys (they can be |
added later via #dump_foreign_key_migration)
:indexes : | If set to false, don‘t dump indexes (they can be added later via dump_index_migration). |
:index_names : | If set to false, don‘t record names of indexes. If set to :namespace, prepend the table name to the index name. |
# File lib/sequel/extensions/schema_dumper.rb, line 66 66: def dump_schema_migration(options=OPTS) 67: options = options.dup 68: if options[:indexes] == false && !options.has_key?(:foreign_keys) 69: # Unless foreign_keys option is specifically set, disable if indexes 70: # are disabled, as foreign keys that point to non-primary keys rely 71: # on unique indexes being created first 72: options[:foreign_keys] = false 73: end 74: 75: ts = sort_dumped_tables(tables(options), options) 76: skipped_fks = if sfk = options[:skipped_foreign_keys] 77: # Handle skipped foreign keys by adding them at the end via 78: # alter_table/add_foreign_key. Note that skipped foreign keys 79: # probably result in a broken down migration. 80: sfka = sfk.sort_by{|table, fks| table.to_s}.map{|table, fks| dump_add_fk_constraints(table, fks.values)} 81: sfka.join("\n\n").gsub(/^/o, ' ') unless sfka.empty? 82: end 83: 84: "Sequel.migration do\n change do\n\#{ts.map{|t| dump_table_schema(t, options)}.join(\"\\n\\n\").gsub(/^/o, ' ')}\#{\"\\n \\n\" if skipped_fks}\#{skipped_fks}\n end\nend\n" 85: end
Return a string with a create table block that will recreate the given table‘s schema. Takes the same options as dump_schema_migration.
# File lib/sequel/extensions/schema_dumper.rb, line 96 96: def dump_table_schema(table, options=OPTS) 97: table = table.value.to_s if table.is_a?(SQL::Identifier) 98: gen = dump_table_generator(table, options) 99: commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n") 100: "create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/o, ' ')}\nend" 101: end