Method: Sequel::SchemaDumper#dump_schema_migration

Defined in:
lib/sequel/extensions/schema_dumper.rb

#dump_schema_migration(options = OPTS) ⇒ Object

Return a string that contains a Sequel migration 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.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sequel/extensions/schema_dumper.rb', line 137

def dump_schema_migration(options=OPTS)
  options = options.dup
  if options[:indexes] == false && !options.has_key?(:foreign_keys)
    # Unless foreign_keys option is specifically set, disable if indexes
    # are disabled, as foreign keys that point to non-primary keys rely
    # on unique indexes being created first
    options[:foreign_keys] = false
  end

  ts = sort_dumped_tables(_dump_tables(options), options)
  skipped_fks = if sfk = options[:skipped_foreign_keys]
    # Handle skipped foreign keys by adding them at the end via
    # alter_table/add_foreign_key.  Note that skipped foreign keys
    # probably result in a broken down migration.
    sfka = sfk.sort.map{|table, fks| dump_add_fk_constraints(table, fks.values)}
    sfka.join("\n\n").gsub(/^/, '    ') unless sfka.empty?
  end

  <<END_MIG
Sequel.migration do
  change do
#{ts.map{|t| dump_table_schema(t, options)}.join("\n\n").gsub(/^/, '    ')}#{"\n    \n" if skipped_fks}#{skipped_fks}
  end
end
END_MIG
end