Methods
Public Instance
Public Instance methods
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 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 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.
# 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 the cached schema to the filename given in Marshal format.
# 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 the cached schema to the filename given unless the file already exists.
# File lib/sequel/extensions/schema_caching.rb, line 59 def dump_schema_cache?(file) dump_schema_cache(file) unless File.exist?(file) 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
-
:indexes - If set to false, don't dump indexes (they can be added later via dump_index_migration).
# 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
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 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
Replace the schema cache with the data from the given file, which should be in Marshal format.
# File lib/sequel/extensions/schema_caching.rb, line 65 def load_schema_cache(file) @schemas = Marshal.load(File.read(file)) nil end
Replace the schema cache with the data from the given file if the file exists.
# File lib/sequel/extensions/schema_caching.rb, line 72 def load_schema_cache?(file) load_schema_cache(file) if File.exist?(file) end
Return a dataset modified by the query block
# File lib/sequel/extensions/query.rb, line 8 def query(&block) dataset.query(&block) end