module Sequel::SchemaDumper

  1. lib/sequel/extensions/schema_dumper.rb

Public Instance methods

column_schema_to_ruby_type(schema)

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.

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

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
90     def dump_foreign_key_migration(options=OPTS)
91       ts = tables(options)
92       <<END_MIG
93 Sequel.migration do
94   change do
95 #{ts.sort.map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join("\n\n").gsub(/^/, '    ')}
96   end
97 end
98 END_MIG
99     end
dump_indexes_migration(options=OPTS)

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.

[show source]
    # File lib/sequel/extensions/schema_dumper.rb
108     def dump_indexes_migration(options=OPTS)
109       ts = tables(options)
110       <<END_MIG
111 Sequel.migration do
112   change do
113 #{ts.sort.map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join("\n\n").gsub(/^/, '    ')}
114   end
115 end
116 END_MIG
117     end
dump_schema_migration(options=OPTS)

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.

[show source]
    # File lib/sequel/extensions/schema_dumper.rb
132     def dump_schema_migration(options=OPTS)
133       options = options.dup
134       if options[:indexes] == false && !options.has_key?(:foreign_keys)
135         # Unless foreign_keys option is specifically set, disable if indexes
136         # are disabled, as foreign keys that point to non-primary keys rely
137         # on unique indexes being created first
138         options[:foreign_keys] = false
139       end
140 
141       ts = sort_dumped_tables(tables(options), options)
142       skipped_fks = if sfk = options[:skipped_foreign_keys]
143         # Handle skipped foreign keys by adding them at the end via
144         # alter_table/add_foreign_key.  Note that skipped foreign keys
145         # probably result in a broken down migration.
146         sfka = sfk.sort.map{|table, fks| dump_add_fk_constraints(table, fks.values)}
147         sfka.join("\n\n").gsub(/^/, '    ') unless sfka.empty?
148       end
149 
150       <<END_MIG
151 Sequel.migration do
152   change do
153 #{ts.map{|t| dump_table_schema(t, options)}.join("\n\n").gsub(/^/, '    ')}#{"\n    \n" if skipped_fks}#{skipped_fks}
154   end
155 end
156 END_MIG
157     end
dump_table_schema(table, options=OPTS)

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
161 def dump_table_schema(table, options=OPTS)
162   gen = dump_table_generator(table, options)
163   commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n")
164   "create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/, '  ')}\nend"
165 end