Module: Sequel::SchemaDumper

Defined in:
lib/sequel/extensions/schema_dumper.rb

Instance Method Summary collapse

Instance Method Details

#column_schema_to_ruby_type(schema) ⇒ Object

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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sequel/extensions/schema_dumper.rb', line 18

def column_schema_to_ruby_type(schema)
  case schema[:db_type].downcase
  when /\A(medium|small)?int(?:eger)?(?:\((\d+)\))?( unsigned)?\z/o
    if !$1 && $2 && $2.to_i >= 10 && $3
      # Unsigned integer type with 10 digits can potentially contain values which
      # don't fit signed integer type, so use bigint type in target database.
      {:type=>Bignum}
    else
      {:type=>Integer}
    end
  when /\Atinyint(?:\((\d+)\))?(?: unsigned)?\z/o
    {:type =>schema[:type] == :boolean ? TrueClass : Integer}
  when /\Abigint(?:\((?:\d+)\))?(?: unsigned)?\z/o
    {:type=>Bignum}
  when /\A(?:real|float|double(?: precision)?|double\(\d+,\d+\)(?: unsigned)?)\z/o
    {:type=>Float}
  when 'boolean', 'bit'
    {:type=>TrueClass}
  when /\A(?:(?:tiny|medium|long|n)?text|clob)\z/o
    {:type=>String, :text=>true}
  when 'date'
    {:type=>Date}
  when /\A(?:small)?datetime\z/o
    {:type=>DateTime}
  when /\Atimestamp(?:\((\d+)\))?(?: with(?:out)? time zone)?\z/o
    {:type=>DateTime, :size=>($1.to_i if $1)}
  when /\Atime(?: with(?:out)? time zone)?\z/o
    {:type=>Time, :only_time=>true}
  when /\An?char(?:acter)?(?:\((\d+)\))?\z/o
    {:type=>String, :size=>($1.to_i if $1), :fixed=>true}
  when /\A(?:n?varchar|character varying|bpchar|string)(?:\((\d+)\))?\z/o
    {:type=>String, :size=>($1.to_i if $1)}
  when /\A(?:small)?money\z/o
    {:type=>BigDecimal, :size=>[19,2]}
  when /\A(?:decimal|numeric|number)(?:\((\d+)(?:,\s*(\d+))?\))?\z/o
    s = [($1.to_i if $1), ($2.to_i if $2)].compact
    {:type=>BigDecimal, :size=>(s.empty? ? nil : s)}
  when /\A(?:bytea|(?:tiny|medium|long)?blob|(?:var)?binary)(?:\((\d+)\))?\z/o
    {:type=>File, :size=>($1.to_i if $1)}
  when /\A(?:year|(?:int )?identity)\z/o
    {:type=>Integer}
  else
    {:type=>String}
  end
end

#dump_foreign_key_migration(options = OPTS) ⇒ Object

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.



71
72
73
74
75
76
77
78
79
80
# File 'lib/sequel/extensions/schema_dumper.rb', line 71

def dump_foreign_key_migration(options=OPTS)
  ts = tables(options)
  <<END_MIG
Sequel.migration do
  change do
#{ts.sort_by(&:to_s).map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, '    ')}
  end
end
END_MIG
end

#dump_indexes_migration(options = OPTS) ⇒ Object

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.



89
90
91
92
93
94
95
96
97
98
# File 'lib/sequel/extensions/schema_dumper.rb', line 89

def dump_indexes_migration(options=OPTS)
  ts = tables(options)
  <<END_MIG
Sequel.migration do
  change do
#{ts.sort_by(&:to_s).map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, '    ')}
  end
end
END_MIG
end

#dump_schema_migration(options = OPTS) ⇒ Object

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sequel/extensions/schema_dumper.rb', line 113

def dump_schema_migration(options=OPTS)
  options = options.dup
  if options[:indexes] == false && !options.has_key?(:foreign_keys)
    # Unless foreign_keys option is specifically set, disable if indexes
    # are disabled, as foreign keys that point to non-primary keys rely
    # on unique indexes being created first
    options[:foreign_keys] = false
  end

  ts = sort_dumped_tables(tables(options), options)
  skipped_fks = if sfk = options[:skipped_foreign_keys]
    # Handle skipped foreign keys by adding them at the end via
    # alter_table/add_foreign_key.  Note that skipped foreign keys
    # probably result in a broken down migration.
    sfka = sfk.sort_by{|table, fks| table.to_s}.map{|table, fks| dump_add_fk_constraints(table, fks.values)}
    sfka.join("\n\n").gsub(/^/o, '    ') unless sfka.empty?
  end

  <<END_MIG
Sequel.migration do
  change do
#{ts.map{|t| dump_table_schema(t, options)}.join("\n\n").gsub(/^/o, '    ')}#{"\n    \n" if skipped_fks}#{skipped_fks}
  end
end
END_MIG
end

#dump_table_schema(table, options = OPTS) ⇒ Object

Return a string with a create table block that will recreate the given table’s schema. Takes the same options as dump_schema_migration.



142
143
144
145
146
147
# File 'lib/sequel/extensions/schema_dumper.rb', line 142

def dump_table_schema(table, options=OPTS)
  table = table.value.to_s if table.is_a?(SQL::Identifier)
  gen = dump_table_generator(table, options)
  commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n")
  "create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/o, '  ')}\nend"
end