Module Sequel::SchemaDumper
In: lib/sequel/extensions/schema_dumper.rb

Methods

Public Instance methods

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.

[Source]

    # File lib/sequel/extensions/schema_dumper.rb, line 22
22:     def column_schema_to_ruby_type(schema)
23:       type = schema[:db_type].downcase
24:       if database_type == :oracle
25:         type = type.sub(/ not null\z/, '')
26:       end
27:       case type
28:       when /\A(medium|small)?int(?:eger)?(?:\((\d+)\))?( unsigned)?\z/
29:         if !$1 && $2 && $2.to_i >= 10 && $3
30:           # Unsigned integer type with 10 digits can potentially contain values which
31:           # don't fit signed integer type, so use bigint type in target database.
32:           {:type=>:Bignum}
33:         else
34:           {:type=>Integer}
35:         end
36:       when /\Atinyint(?:\((\d+)\))?(?: unsigned)?\z/
37:         {:type =>schema[:type] == :boolean ? TrueClass : Integer}
38:       when /\Abigint(?:\((?:\d+)\))?(?: unsigned)?\z/
39:         {:type=>:Bignum}
40:       when /\A(?:real|float|double(?: precision)?|double\(\d+,\d+\)(?: unsigned)?)\z/
41:         {:type=>Float}
42:       when 'boolean', 'bit', 'bool'
43:         {:type=>TrueClass}
44:       when /\A(?:(?:tiny|medium|long|n)?text|clob)\z/
45:         {:type=>String, :text=>true}
46:       when 'date'
47:         {:type=>Date}
48:       when /\A(?:small)?datetime\z/
49:         {:type=>DateTime}
50:       when /\Atimestamp(?:\((\d+)\))?(?: with(?:out)? time zone)?\z/
51:         {:type=>DateTime, :size=>($1.to_i if $1)}
52:       when /\Atime(?: with(?:out)? time zone)?\z/
53:         {:type=>Time, :only_time=>true}
54:       when /\An?char(?:acter)?(?:\((\d+)\))?\z/
55:         {:type=>String, :size=>($1.to_i if $1), :fixed=>true}
56:       when /\A(?:n?varchar2?|character varying|bpchar|string)(?:\((\d+)\))?\z/
57:         {:type=>String, :size=>($1.to_i if $1)}
58:       when /\A(?:small)?money\z/
59:         {:type=>BigDecimal, :size=>[19,2]}
60:       when /\A(?:decimal|numeric|number)(?:\((\d+)(?:,\s*(\d+))?\))?\z/
61:         s = [($1.to_i if $1), ($2.to_i if $2)].compact
62:         {:type=>BigDecimal, :size=>(s.empty? ? nil : s)}
63:       when /\A(?:bytea|(?:tiny|medium|long)?blob|(?:var)?binary)(?:\((\d+)\))?\z/
64:         {:type=>File, :size=>($1.to_i if $1)}
65:       when /\A(?:year|(?:int )?identity)\z/
66:         {:type=>Integer}
67:       else
68:         {:type=>String}
69:       end
70:     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.

[Source]

    # File lib/sequel/extensions/schema_dumper.rb, line 79
79:     def dump_foreign_key_migration(options=OPTS)
80:       ts = tables(options)
81:       "Sequel.migration do\n  change do\n\#{ts.sort.map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join(\"\\n\\n\").gsub(/^/, '    ')}\n  end\nend\n"
82:     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.

[Source]

     # File lib/sequel/extensions/schema_dumper.rb, line 98
 98:     def dump_indexes_migration(options=OPTS)
 99:       ts = tables(options)
100:       "Sequel.migration do\n  change do\n\#{ts.sort.map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join(\"\\n\\n\").gsub(/^/, '    ')}\n  end\nend\n"
101:     end

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.

[Source]

     # File lib/sequel/extensions/schema_dumper.rb, line 123
123:     def dump_schema_migration(options=OPTS)
124:       options = options.dup
125:       if options[:indexes] == false && !options.has_key?(:foreign_keys)
126:         # Unless foreign_keys option is specifically set, disable if indexes
127:         # are disabled, as foreign keys that point to non-primary keys rely
128:         # on unique indexes being created first
129:         options[:foreign_keys] = false
130:       end
131: 
132:       ts = sort_dumped_tables(tables(options), options)
133:       skipped_fks = if sfk = options[:skipped_foreign_keys]
134:         # Handle skipped foreign keys by adding them at the end via
135:         # alter_table/add_foreign_key.  Note that skipped foreign keys
136:         # probably result in a broken down migration.
137:         sfka = sfk.sort.map{|table, fks| dump_add_fk_constraints(table, fks.values)}
138:         sfka.join("\n\n").gsub(/^/, '    ') unless sfka.empty?
139:       end
140: 
141:       "Sequel.migration do\n  change do\n\#{ts.map{|t| dump_table_schema(t, options)}.join(\"\\n\\n\").gsub(/^/, '    ')}\#{\"\\n    \\n\" if skipped_fks}\#{skipped_fks}\n  end\nend\n"
142:     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.

[Source]

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

[Validate]