Module | Sequel::SchemaDumper |
In: |
lib/sequel/extensions/schema_dumper.rb
|
Convert the column schema information to a hash of column options, one of which must be :type. The other options added should modify that type (e.g. :size). If a database type is not recognized, return it as a String type.
# File lib/sequel/extensions/schema_dumper.rb, line 18 18: def column_schema_to_ruby_type(schema) 19: case schema[:db_type].downcase 20: when /\A(medium|small)?int(?:eger)?(?:\((\d+)\))?( unsigned)?\z/o 21: if !$1 && $2 && $2.to_i >= 10 && $3 22: # Unsigned integer type with 10 digits can potentially contain values which 23: # don't fit signed integer type, so use bigint type in target database. 24: {:type=>Bignum} 25: else 26: {:type=>Integer} 27: end 28: when /\Atinyint(?:\((\d+)\))?(?: unsigned)?\z/o 29: {:type =>schema[:type] == :boolean ? TrueClass : Integer} 30: when /\Abigint(?:\((?:\d+)\))?(?: unsigned)?\z/o 31: {:type=>Bignum} 32: when /\A(?:real|float|double(?: precision)?|double\(\d+,\d+\)(?: unsigned)?)\z/o 33: {:type=>Float} 34: when 'boolean', 'bit' 35: {:type=>TrueClass} 36: when /\A(?:(?:tiny|medium|long|n)?text|clob)\z/o 37: {:type=>String, :text=>true} 38: when 'date' 39: {:type=>Date} 40: when /\A(?:small)?datetime\z/o 41: {:type=>DateTime} 42: when /\Atimestamp(?:\((\d+)\))?(?: with(?:out)? time zone)?\z/o 43: {:type=>DateTime, :size=>($1.to_i if $1)} 44: when /\Atime(?: with(?:out)? time zone)?\z/o 45: {:type=>Time, :only_time=>true} 46: when /\An?char(?:acter)?(?:\((\d+)\))?\z/o 47: {:type=>String, :size=>($1.to_i if $1), :fixed=>true} 48: when /\A(?:n?varchar|character varying|bpchar|string)(?:\((\d+)\))?\z/o 49: {:type=>String, :size=>($1.to_i if $1)} 50: when /\A(?:small)?money\z/o 51: {:type=>BigDecimal, :size=>[19,2]} 52: when /\A(?:decimal|numeric|number)(?:\((\d+)(?:,\s*(\d+))?\))?\z/o 53: s = [($1.to_i if $1), ($2.to_i if $2)].compact 54: {:type=>BigDecimal, :size=>(s.empty? ? nil : s)} 55: when /\A(?:bytea|(?:tiny|medium|long)?blob|(?:var)?binary)(?:\((\d+)\))?\z/o 56: {:type=>File, :size=>($1.to_i if $1)} 57: when /\A(?:year|(?:int )?identity)\z/o 58: {:type=>Integer} 59: else 60: {:type=>String} 61: end 62: end
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 71 71: def dump_foreign_key_migration(options=OPTS) 72: ts = tables(options) 73: "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" 74: 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 90 90: def dump_indexes_migration(options=OPTS) 91: ts = tables(options) 92: "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" 93: 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 115 115: def dump_schema_migration(options=OPTS) 116: options = options.dup 117: if options[:indexes] == false && !options.has_key?(:foreign_keys) 118: # Unless foreign_keys option is specifically set, disable if indexes 119: # are disabled, as foreign keys that point to non-primary keys rely 120: # on unique indexes being created first 121: options[:foreign_keys] = false 122: end 123: 124: ts = sort_dumped_tables(tables(options), options) 125: skipped_fks = if sfk = options[:skipped_foreign_keys] 126: # Handle skipped foreign keys by adding them at the end via 127: # alter_table/add_foreign_key. Note that skipped foreign keys 128: # probably result in a broken down migration. 129: sfka = sfk.sort_by{|table, fks| table.to_s}.map{|table, fks| dump_add_fk_constraints(table, fks.values)} 130: sfka.join("\n\n").gsub(/^/o, ' ') unless sfka.empty? 131: end 132: 133: "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" 134: 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 145 145: def dump_table_schema(table, options=OPTS) 146: table = table.value.to_s if table.is_a?(SQL::Identifier) 147: gen = dump_table_generator(table, options) 148: commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n") 149: "create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/o, ' ')}\nend" 150: end