class Sequel::Database

  1. lib/sequel/extensions/query.rb
  2. lib/sequel/extensions/schema_dumper.rb
  3. lib/sequel/extensions/schema_caching.rb
  4. show all
Parent: Sequel

Public Instance methods

dump_foreign_key_migration (options={})

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 16
def dump_foreign_key_migration(options={})
  ts = tables(options)
  Sequel.migration do  up do#{ts.sort_by{|t| t.to_s}.map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, '    ')}  endend
end
dump_indexes_migration (options={})

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.

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 31
def dump_indexes_migration(options={})
  ts = tables(options)
  Sequel.migration do  up 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    down do#{ts.sort_by{|t| t.to_s}.reverse.map{|t| dump_table_indexes(t, :drop_index, options)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, '    ')}  endend
end
dump_schema_cache (file)

Dump the cached schema to the filename given in Marshal format.

[show source]
# File lib/sequel/extensions/schema_caching.rb, line 52
def dump_schema_cache(file)
  File.open(file, 'wb'){|f| f.write(Marshal.dump(@schemas))}
  nil
end
dump_schema_cache? (file)

Dump the cached schema to the filename given unless the file already exists.

[show source]
# File lib/sequel/extensions/schema_caching.rb, line 59
def dump_schema_cache?(file)
  dump_schema_cache(file) unless File.exist?(file)
end
dump_schema_migration (options={})

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

  • :indexes - If set to false, don't dump indexes (they can be added later via dump_index_migration).

[show source]
# File lib/sequel/extensions/schema_dumper.rb, line 56
def dump_schema_migration(options={})
  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

  Sequel.migration do  up do#{ts.map{|t| dump_table_schema(t, options)}.join("\n\n").gsub(/^/o, '    ')}#{"\n    \n" if skipped_fks}#{skipped_fks}  end    down do    drop_table(#{ts.reverse.inspect[1...-1]})  endend
end
dump_table_schema (table, options={})

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 89
def dump_table_schema(table, options={})
  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
load_schema_cache (file)

Replace the schema cache with the data from the given file, which should be in Marshal format.

[show source]
# File lib/sequel/extensions/schema_caching.rb, line 65
def load_schema_cache(file)
  @schemas = Marshal.load(File.read(file))
  nil
end
load_schema_cache? (file)

Replace the schema cache with the data from the given file if the file exists.

[show source]
# File lib/sequel/extensions/schema_caching.rb, line 72
def load_schema_cache?(file)
  load_schema_cache(file) if File.exist?(file)
end
query (&block)

Return a dataset modified by the query block

[show source]
# File lib/sequel/extensions/query.rb, line 8
def query(&block)
  dataset.query(&block)
end