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
38 def column_schema_to_ruby_type(schema)
39   type = schema[:db_type].downcase
40   if database_type == :oracle
41     type = type.sub(/ not null\z/, '')
42   end
43   case type
44   when /\A(medium|small)?int(?:eger)?(?:\((\d+)\))?( unsigned)?\z/
45     if !$1 && $2 && $2.to_i >= 10 && $3
46       # Unsigned integer type with 10 digits can potentially contain values which
47       # don't fit signed integer type, so use bigint type in target database.
48       {:type=>:Bignum}
49     else
50       {:type=>Integer}
51     end
52   when /\Atinyint(?:\((\d+)\))?(?: unsigned)?\z/
53     {:type =>schema[:type] == :boolean ? TrueClass : Integer}
54   when /\Abigint(?:\((?:\d+)\))?(?: unsigned)?\z/
55     {:type=>:Bignum}
56   when /\A(?:real|float|double(?: precision)?|double\(\d+,\d+\))(?: unsigned)?\z/
57     {:type=>Float}
58   when 'boolean', 'bit', 'bool'
59     {:type=>TrueClass}
60   when /\A(?:(?:tiny|medium|long|n)?text|clob)\z/
61     {:type=>String, :text=>true}
62   when 'date'
63     {:type=>Date}
64   when /\A(?:small)?datetime\z/
65     {:type=>DateTime}
66   when /\Atimestamp(?:\((\d+)\))?(?: with(?:out)? time zone)?\z/
67     {:type=>DateTime, :size=>($1.to_i if $1)}
68   when /\Atime(?: with(?:out)? time zone)?\z/
69     {:type=>Time, :only_time=>true}
70   when /\An?char(?:acter)?(?:\((\d+)\))?\z/
71     {:type=>String, :size=>($1.to_i if $1), :fixed=>true}
72   when /\A(?:n?varchar2?|character varying|bpchar|string)(?:\((\d+)\))?\z/
73     {:type=>String, :size=>($1.to_i if $1)}
74   when /\A(?:small)?money\z/
75     {:type=>BigDecimal, :size=>[19,2]}
76   when /\A(?:decimal|numeric|number)(?:\((\d+)(?:,\s*(\d+))?\))?(?: unsigned)?\z/
77     s = [($1.to_i if $1), ($2.to_i if $2)].compact
78     {:type=>BigDecimal, :size=>(s.empty? ? nil : s)}
79   when /\A(?:bytea|(?:tiny|medium|long)?blob|(?:var)?binary)(?:\((\d+)\))?\z/
80     {:type=>File, :size=>($1.to_i if $1)}
81   when /\A(?:year|(?:int )?identity)\z/
82     {:type=>Integer}
83   else
84     {:type=>String}
85   end
86 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
 95     def dump_foreign_key_migration(options=OPTS)
 96       ts = _dump_tables(options)
 97       <<END_MIG
 98 Sequel.migration do
 99   change do
100 #{ts.map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join("\n\n").gsub(/^/, '    ')}
101   end
102 end
103 END_MIG
104     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
113     def dump_indexes_migration(options=OPTS)
114       ts = _dump_tables(options)
115       <<END_MIG
116 Sequel.migration do
117   change do
118 #{ts.map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join("\n\n").gsub(/^/, '    ')}
119   end
120 end
121 END_MIG
122     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
137     def dump_schema_migration(options=OPTS)
138       options = options.dup
139       if options[:indexes] == false && !options.has_key?(:foreign_keys)
140         # Unless foreign_keys option is specifically set, disable if indexes
141         # are disabled, as foreign keys that point to non-primary keys rely
142         # on unique indexes being created first
143         options[:foreign_keys] = false
144       end
145 
146       ts = sort_dumped_tables(_dump_tables(options), options)
147       skipped_fks = if sfk = options[:skipped_foreign_keys]
148         # Handle skipped foreign keys by adding them at the end via
149         # alter_table/add_foreign_key.  Note that skipped foreign keys
150         # probably result in a broken down migration.
151         sfka = sfk.sort.map{|table, fks| dump_add_fk_constraints(table, fks.values)}
152         sfka.join("\n\n").gsub(/^/, '    ') unless sfka.empty?
153       end
154 
155       <<END_MIG
156 Sequel.migration do
157   change do
158 #{ts.map{|t| dump_table_schema(t, options)}.join("\n\n").gsub(/^/, '    ')}#{"\n    \n" if skipped_fks}#{skipped_fks}
159   end
160 end
161 END_MIG
162     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
166 def dump_table_schema(table, options=OPTS)
167   gen = dump_table_generator(table, options)
168   commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n")
169   "create_table(#{table.inspect}#{", #{IGNORE_INDEX_ERRORS_KEY}true" if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/, '  ')}\nend"
170 end