module Sequel::SchemaDumper

  1. lib/sequel/extensions/schema_dumper.rb
Parent: Sequel

Public Instance methods

dump_foreign_key_migration (options=OPTS)

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.

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 22
    def dump_foreign_key_migration(options=OPTS)
      ts = tables(options)
      <<END_MIG
Sequel.migration do
  change do
#{ts.sort_by{|t| t.to_s}.map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, '    ')}
  end
end
END_MIG
    end
dump_indexes_migration (options=OPTS)

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.

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 40
    def dump_indexes_migration(options=OPTS)
      ts = tables(options)
      <<END_MIG
Sequel.migration do
  change do
#{ts.sort_by{|t| t.to_s}.map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, '    ')}
  end
end
END_MIG
    end
dump_schema_migration (options=OPTS)

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.

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 64
    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(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_by{|table, fks| table.to_s}.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(/^/o, '    ')}#{"\n    \n" if skipped_fks}#{skipped_fks}
  end
end
END_MIG
    end
dump_table_schema (table, options=OPTS)

Return a string with a create table block that will recreate the given table’s schema. Takes the same options as dump_schema_migration.

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 93
def dump_table_schema(table, options=OPTS)
  table = table.value.to_s if table.is_a?(SQL::Identifier)
  gen = dump_table_generator(table, options)
  commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n")
  "create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/o, '  ')}\nend"
end