-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
schema_dumper.rb
74 lines (62 loc) · 2.25 KB
/
schema_dumper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
class SchemaDumper < ConnectionAdapters::SchemaDumper # :nodoc:
private
def extensions(stream)
extensions = @connection.extensions
if extensions.any?
stream.puts " # These are extensions that must be enabled in order to support this database"
extensions.sort.each do |extension|
stream.puts " enable_extension #{extension.inspect}"
end
stream.puts
end
end
def types(stream)
types = @connection.enum_types
if types.any?
stream.puts " # Custom types defined in this database."
stream.puts " # Note that some types may not work with other database engines. Be careful if changing database."
types.sort.each do |name, values|
stream.puts " create_enum #{name.inspect}, #{values.split(",").inspect}"
end
stream.puts
end
end
def prepare_column_options(column)
spec = super
spec[:array] = "true" if column.array?
if @connection.supports_virtual_columns? && column.virtual?
spec[:as] = extract_expression_for_virtual_column(column)
spec[:stored] = true
spec = { type: schema_type(column).inspect }.merge!(spec)
end
spec[:enum_type] = "\"#{column.sql_type}\"" if column.enum?
spec
end
def default_primary_key?(column)
schema_type(column) == :bigserial
end
def explicit_primary_key_default?(column)
column.type == :uuid || (column.type == :integer && !column.serial?)
end
def schema_type(column)
return super unless column.serial?
if column.bigint?
:bigserial
else
:serial
end
end
def schema_expression(column)
super unless column.serial?
end
def extract_expression_for_virtual_column(column)
column.default_function.inspect
end
end
end
end
end