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:       case schema[:db_type].downcase
24:       when /\A(medium|small)?int(?:eger)?(?:\((\d+)\))?( unsigned)?\z/o
25:         if !$1 && $2 && $2.to_i >= 10 && $3
26:           # Unsigned integer type with 10 digits can potentially contain values which
27:           # don't fit signed integer type, so use bigint type in target database.
28:           {:type=>:Bignum}
29:         else
30:           {:type=>Integer}
31:         end
32:       when /\Atinyint(?:\((\d+)\))?(?: unsigned)?\z/o
33:         {:type =>schema[:type] == :boolean ? TrueClass : Integer}
34:       when /\Abigint(?:\((?:\d+)\))?(?: unsigned)?\z/o
35:         {:type=>:Bignum}
36:       when /\A(?:real|float|double(?: precision)?|double\(\d+,\d+\)(?: unsigned)?)\z/o
37:         {:type=>Float}
38:       when 'boolean', 'bit', 'bool'
39:         {:type=>TrueClass}
40:       when /\A(?:(?:tiny|medium|long|n)?text|clob)\z/o
41:         {:type=>String, :text=>true}
42:       when 'date'
43:         {:type=>Date}
44:       when /\A(?:small)?datetime\z/o
45:         {:type=>DateTime}
46:       when /\Atimestamp(?:\((\d+)\))?(?: with(?:out)? time zone)?\z/o
47:         {:type=>DateTime, :size=>($1.to_i if $1)}
48:       when /\Atime(?: with(?:out)? time zone)?\z/o
49:         {:type=>Time, :only_time=>true}
50:       when /\An?char(?:acter)?(?:\((\d+)\))?\z/o
51:         {:type=>String, :size=>($1.to_i if $1), :fixed=>true}
52:       when /\A(?:n?varchar|character varying|bpchar|string)(?:\((\d+)\))?\z/o
53:         {:type=>String, :size=>($1.to_i if $1)}
54:       when /\A(?:small)?money\z/o
55:         {:type=>BigDecimal, :size=>[19,2]}
56:       when /\A(?:decimal|numeric|number)(?:\((\d+)(?:,\s*(\d+))?\))?\z/o
57:         s = [($1.to_i if $1), ($2.to_i if $2)].compact
58:         {:type=>BigDecimal, :size=>(s.empty? ? nil : s)}
59:       when /\A(?:bytea|(?:tiny|medium|long)?blob|(?:var)?binary)(?:\((\d+)\))?\z/o
60:         {:type=>File, :size=>($1.to_i if $1)}
61:       when /\A(?:year|(?:int )?identity)\z/o
62:         {:type=>Integer}
63:       else
64:         {:type=>String}
65:       end
66:     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 75
75:     def dump_foreign_key_migration(options=OPTS)
76:       ts = tables(options)
77:       "Sequel.migration do\n  change do\n\#{ts.sort_by(&:to_s).map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join(\"\\n\\n\").gsub(/^/o, '    ')}\n  end\nend\n"
78:     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 94
94:     def dump_indexes_migration(options=OPTS)
95:       ts = tables(options)
96:       "Sequel.migration do\n  change do\n\#{ts.sort_by(&:to_s).map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join(\"\\n\\n\").gsub(/^/o, '    ')}\n  end\nend\n"
97:     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.

[Source]

     # File lib/sequel/extensions/schema_dumper.rb, line 119
119:     def dump_schema_migration(options=OPTS)
120:       options = options.dup
121:       if options[:indexes] == false && !options.has_key?(:foreign_keys)
122:         # Unless foreign_keys option is specifically set, disable if indexes
123:         # are disabled, as foreign keys that point to non-primary keys rely
124:         # on unique indexes being created first
125:         options[:foreign_keys] = false
126:       end
127: 
128:       ts = sort_dumped_tables(tables(options), options)
129:       skipped_fks = if sfk = options[:skipped_foreign_keys]
130:         # Handle skipped foreign keys by adding them at the end via
131:         # alter_table/add_foreign_key.  Note that skipped foreign keys
132:         # probably result in a broken down migration.
133:         sfka = sfk.sort_by{|table, fks| table.to_s}.map{|table, fks| dump_add_fk_constraints(table, fks.values)}
134:         sfka.join("\n\n").gsub(/^/o, '    ') unless sfka.empty?
135:       end
136: 
137:       "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"
138:     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 149
149:     def dump_table_schema(table, options=OPTS)
150:       gen = dump_table_generator(table, options)
151:       commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n")
152:       "create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/o, '  ')}\nend"
153:     end

[Validate]