activerecord-materialize-adapter 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/lib/active_record/connection_adapters/materialize/column.rb +30 -0
- data/lib/active_record/connection_adapters/materialize/database_statements.rb +199 -0
- data/lib/active_record/connection_adapters/materialize/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/materialize/oid/array.rb +91 -0
- data/lib/active_record/connection_adapters/materialize/oid/bit.rb +53 -0
- data/lib/active_record/connection_adapters/materialize/oid/bit_varying.rb +15 -0
- data/lib/active_record/connection_adapters/materialize/oid/bytea.rb +17 -0
- data/lib/active_record/connection_adapters/materialize/oid/cidr.rb +50 -0
- data/lib/active_record/connection_adapters/materialize/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/materialize/oid/date_time.rb +23 -0
- data/lib/active_record/connection_adapters/materialize/oid/decimal.rb +15 -0
- data/lib/active_record/connection_adapters/materialize/oid/enum.rb +20 -0
- data/lib/active_record/connection_adapters/materialize/oid/hstore.rb +70 -0
- data/lib/active_record/connection_adapters/materialize/oid/inet.rb +15 -0
- data/lib/active_record/connection_adapters/materialize/oid/jsonb.rb +15 -0
- data/lib/active_record/connection_adapters/materialize/oid/legacy_point.rb +44 -0
- data/lib/active_record/connection_adapters/materialize/oid/money.rb +41 -0
- data/lib/active_record/connection_adapters/materialize/oid/oid.rb +15 -0
- data/lib/active_record/connection_adapters/materialize/oid/point.rb +64 -0
- data/lib/active_record/connection_adapters/materialize/oid/range.rb +96 -0
- data/lib/active_record/connection_adapters/materialize/oid/specialized_string.rb +18 -0
- data/lib/active_record/connection_adapters/materialize/oid/type_map_initializer.rb +112 -0
- data/lib/active_record/connection_adapters/materialize/oid/uuid.rb +25 -0
- data/lib/active_record/connection_adapters/materialize/oid/vector.rb +28 -0
- data/lib/active_record/connection_adapters/materialize/oid/xml.rb +30 -0
- data/lib/active_record/connection_adapters/materialize/oid.rb +35 -0
- data/lib/active_record/connection_adapters/materialize/quoting.rb +205 -0
- data/lib/active_record/connection_adapters/materialize/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/materialize/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/materialize/schema_definitions.rb +222 -0
- data/lib/active_record/connection_adapters/materialize/schema_dumper.rb +49 -0
- data/lib/active_record/connection_adapters/materialize/schema_statements.rb +742 -0
- data/lib/active_record/connection_adapters/materialize/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/materialize/utils.rb +80 -0
- data/lib/active_record/connection_adapters/materialize/version.rb +9 -0
- data/lib/active_record/connection_adapters/materialize_adapter.rb +952 -0
- data/lib/active_record/tasks/materialize_database_tasks.rb +130 -0
- data/lib/activerecord-materialize-adapter.rb +3 -0
- data/lib/materialize/errors/database_error.rb +10 -0
- data/lib/materialize/errors/incomplete_input.rb +10 -0
- metadata +170 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module Materialize
|
6
|
+
module OID # :nodoc:
|
7
|
+
class LegacyPoint < Type::Value # :nodoc:
|
8
|
+
include ActiveModel::Type::Helpers::Mutable
|
9
|
+
|
10
|
+
def type
|
11
|
+
:point
|
12
|
+
end
|
13
|
+
|
14
|
+
def cast(value)
|
15
|
+
case value
|
16
|
+
when ::String
|
17
|
+
if value[0] == "(" && value[-1] == ")"
|
18
|
+
value = value[1...-1]
|
19
|
+
end
|
20
|
+
cast(value.split(","))
|
21
|
+
when ::Array
|
22
|
+
value.map { |v| Float(v) }
|
23
|
+
else
|
24
|
+
value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def serialize(value)
|
29
|
+
if value.is_a?(::Array)
|
30
|
+
"(#{number_for_point(value[0])},#{number_for_point(value[1])})"
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def number_for_point(number)
|
38
|
+
number.to_s.gsub(/\.0$/, "")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module Materialize
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Money < Type::Decimal # :nodoc:
|
8
|
+
def type
|
9
|
+
:money
|
10
|
+
end
|
11
|
+
|
12
|
+
def scale
|
13
|
+
2
|
14
|
+
end
|
15
|
+
|
16
|
+
def cast_value(value)
|
17
|
+
return value unless ::String === value
|
18
|
+
|
19
|
+
# Because money output is formatted according to the locale, there are two
|
20
|
+
# cases to consider (note the decimal separators):
|
21
|
+
# (1) $12,345,678.12
|
22
|
+
# (2) $12.345.678,12
|
23
|
+
# Negative values are represented as follows:
|
24
|
+
# (3) -$2.55
|
25
|
+
# (4) ($2.55)
|
26
|
+
|
27
|
+
value = value.sub(/^\((.+)\)$/, '-\1') # (4)
|
28
|
+
case value
|
29
|
+
when /^-?\D*+[\d,]+\.\d{2}$/ # (1)
|
30
|
+
value.gsub!(/[^-\d.]/, "")
|
31
|
+
when /^-?\D*+[\d.]+,\d{2}$/ # (2)
|
32
|
+
value.gsub!(/[^-\d,]/, "").sub!(/,/, ".")
|
33
|
+
end
|
34
|
+
|
35
|
+
super(value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
Point = Struct.new(:x, :y)
|
5
|
+
|
6
|
+
module ConnectionAdapters
|
7
|
+
module Materialize
|
8
|
+
module OID # :nodoc:
|
9
|
+
class Point < Type::Value # :nodoc:
|
10
|
+
include ActiveModel::Type::Helpers::Mutable
|
11
|
+
|
12
|
+
def type
|
13
|
+
:point
|
14
|
+
end
|
15
|
+
|
16
|
+
def cast(value)
|
17
|
+
case value
|
18
|
+
when ::String
|
19
|
+
return if value.blank?
|
20
|
+
|
21
|
+
if value[0] == "(" && value[-1] == ")"
|
22
|
+
value = value[1...-1]
|
23
|
+
end
|
24
|
+
x, y = value.split(",")
|
25
|
+
build_point(x, y)
|
26
|
+
when ::Array
|
27
|
+
build_point(*value)
|
28
|
+
else
|
29
|
+
value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def serialize(value)
|
34
|
+
case value
|
35
|
+
when ActiveRecord::Point
|
36
|
+
"(#{number_for_point(value.x)},#{number_for_point(value.y)})"
|
37
|
+
when ::Array
|
38
|
+
serialize(build_point(*value))
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def type_cast_for_schema(value)
|
45
|
+
if ActiveRecord::Point === value
|
46
|
+
[value.x, value.y]
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def number_for_point(number)
|
54
|
+
number.to_s.gsub(/\.0$/, "")
|
55
|
+
end
|
56
|
+
|
57
|
+
def build_point(x, y)
|
58
|
+
ActiveRecord::Point.new(Float(x), Float(y))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module Materialize
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Range < Type::Value # :nodoc:
|
8
|
+
attr_reader :subtype, :type
|
9
|
+
delegate :user_input_in_time_zone, to: :subtype
|
10
|
+
|
11
|
+
def initialize(subtype, type = :range)
|
12
|
+
@subtype = subtype
|
13
|
+
@type = type
|
14
|
+
end
|
15
|
+
|
16
|
+
def type_cast_for_schema(value)
|
17
|
+
value.inspect.gsub("Infinity", "::Float::INFINITY")
|
18
|
+
end
|
19
|
+
|
20
|
+
def cast_value(value)
|
21
|
+
return if value == "empty"
|
22
|
+
return value unless value.is_a?(::String)
|
23
|
+
|
24
|
+
extracted = extract_bounds(value)
|
25
|
+
from = type_cast_single extracted[:from]
|
26
|
+
to = type_cast_single extracted[:to]
|
27
|
+
|
28
|
+
if !infinity?(from) && extracted[:exclude_start]
|
29
|
+
raise ArgumentError, "The Ruby Range object does not support excluding the beginning of a Range. (unsupported value: '#{value}')"
|
30
|
+
end
|
31
|
+
::Range.new(from, to, extracted[:exclude_end])
|
32
|
+
end
|
33
|
+
|
34
|
+
def serialize(value)
|
35
|
+
if value.is_a?(::Range)
|
36
|
+
from = type_cast_single_for_database(value.begin)
|
37
|
+
to = type_cast_single_for_database(value.end)
|
38
|
+
::Range.new(from, to, value.exclude_end?)
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def ==(other)
|
45
|
+
other.is_a?(Range) &&
|
46
|
+
other.subtype == subtype &&
|
47
|
+
other.type == type
|
48
|
+
end
|
49
|
+
|
50
|
+
def map(value) # :nodoc:
|
51
|
+
new_begin = yield(value.begin)
|
52
|
+
new_end = yield(value.end)
|
53
|
+
::Range.new(new_begin, new_end, value.exclude_end?)
|
54
|
+
end
|
55
|
+
|
56
|
+
def force_equality?(value)
|
57
|
+
value.is_a?(::Range)
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def type_cast_single(value)
|
62
|
+
infinity?(value) ? value : @subtype.deserialize(value)
|
63
|
+
end
|
64
|
+
|
65
|
+
def type_cast_single_for_database(value)
|
66
|
+
infinity?(value) ? value : @subtype.serialize(@subtype.cast(value))
|
67
|
+
end
|
68
|
+
|
69
|
+
def extract_bounds(value)
|
70
|
+
from, to = value[1..-2].split(",")
|
71
|
+
{
|
72
|
+
from: (value[1] == "," || from == "-infinity") ? infinity(negative: true) : from,
|
73
|
+
to: (value[-2] == "," || to == "infinity") ? infinity : to,
|
74
|
+
exclude_start: (value[0] == "("),
|
75
|
+
exclude_end: (value[-1] == ")")
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
def infinity(negative: false)
|
80
|
+
if subtype.respond_to?(:infinity)
|
81
|
+
subtype.infinity(negative: negative)
|
82
|
+
elsif negative
|
83
|
+
-::Float::INFINITY
|
84
|
+
else
|
85
|
+
::Float::INFINITY
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def infinity?(value)
|
90
|
+
value.respond_to?(:infinite?) && value.infinite?
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module Materialize
|
6
|
+
module OID # :nodoc:
|
7
|
+
class SpecializedString < Type::String # :nodoc:
|
8
|
+
attr_reader :type
|
9
|
+
|
10
|
+
def initialize(type, **options)
|
11
|
+
@type = type
|
12
|
+
super(**options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/array/extract"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module ConnectionAdapters
|
7
|
+
module Materialize
|
8
|
+
module OID # :nodoc:
|
9
|
+
# This class uses the data from Materialize pg_type table to build
|
10
|
+
# the OID -> Type mapping.
|
11
|
+
# - OID is an integer representing the type.
|
12
|
+
# - Type is an OID::Type object.
|
13
|
+
# This class has side effects on the +store+ passed during initialization.
|
14
|
+
class TypeMapInitializer # :nodoc:
|
15
|
+
def initialize(store)
|
16
|
+
@store = store
|
17
|
+
end
|
18
|
+
|
19
|
+
def run(records)
|
20
|
+
nodes = records.reject { |row| @store.key? row["oid"].to_i }
|
21
|
+
mapped = nodes.extract! { |row| @store.key? row["typname"] }
|
22
|
+
ranges = nodes.extract! { |row| row["typtype"] == "r" }
|
23
|
+
enums = nodes.extract! { |row| row["typtype"] == "e" }
|
24
|
+
domains = nodes.extract! { |row| row["typtype"] == "d" }
|
25
|
+
arrays = nodes.extract! { |row| row["typinput"] == "array_in" }
|
26
|
+
composites = nodes.extract! { |row| row["typelem"].to_i != 0 }
|
27
|
+
|
28
|
+
mapped.each { |row| register_mapped_type(row) }
|
29
|
+
enums.each { |row| register_enum_type(row) }
|
30
|
+
domains.each { |row| register_domain_type(row) }
|
31
|
+
arrays.each { |row| register_array_type(row) }
|
32
|
+
ranges.each { |row| register_range_type(row) }
|
33
|
+
composites.each { |row| register_composite_type(row) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def query_conditions_for_initial_load
|
37
|
+
known_type_names = @store.keys.map { |n| "'#{n}'" }
|
38
|
+
known_type_types = %w('r' 'e' 'd')
|
39
|
+
<<~SQL % [known_type_names.join(", "), known_type_types.join(", ")]
|
40
|
+
WHERE
|
41
|
+
t.typname IN (%s)
|
42
|
+
OR t.typtype IN (%s)
|
43
|
+
OR t.typelem != 0
|
44
|
+
SQL
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def register_mapped_type(row)
|
49
|
+
alias_type row["oid"], row["typname"]
|
50
|
+
end
|
51
|
+
|
52
|
+
def register_enum_type(row)
|
53
|
+
register row["oid"], OID::Enum.new
|
54
|
+
end
|
55
|
+
|
56
|
+
def register_array_type(row)
|
57
|
+
register_with_subtype(row["oid"], row["typelem"].to_i) do |subtype|
|
58
|
+
OID::Array.new(subtype, row["typdelim"])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def register_range_type(row)
|
63
|
+
register_with_subtype(row["oid"], row["rngsubtype"].to_i) do |subtype|
|
64
|
+
OID::Range.new(subtype, row["typname"].to_sym)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def register_domain_type(row)
|
69
|
+
if base_type = @store.lookup(row["typbasetype"].to_i)
|
70
|
+
register row["oid"], base_type
|
71
|
+
else
|
72
|
+
warn "unknown base type (OID: #{row["typbasetype"]}) for domain #{row["typname"]}."
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def register_composite_type(row)
|
77
|
+
if subtype = @store.lookup(row["typelem"].to_i)
|
78
|
+
register row["oid"], OID::Vector.new(row["typdelim"], subtype)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def register(oid, oid_type = nil, &block)
|
83
|
+
oid = assert_valid_registration(oid, oid_type || block)
|
84
|
+
if block_given?
|
85
|
+
@store.register_type(oid, &block)
|
86
|
+
else
|
87
|
+
@store.register_type(oid, oid_type)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def alias_type(oid, target)
|
92
|
+
oid = assert_valid_registration(oid, target)
|
93
|
+
@store.alias_type(oid, target)
|
94
|
+
end
|
95
|
+
|
96
|
+
def register_with_subtype(oid, target_oid)
|
97
|
+
if @store.key?(target_oid)
|
98
|
+
register(oid) do |_, *args|
|
99
|
+
yield @store.lookup(target_oid, *args)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def assert_valid_registration(oid, oid_type)
|
105
|
+
raise ArgumentError, "can't register nil type for OID #{oid}" if oid_type.nil?
|
106
|
+
oid.to_i
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module Materialize
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Uuid < Type::Value # :nodoc:
|
8
|
+
ACCEPTABLE_UUID = %r{\A(\{)?([a-fA-F0-9]{4}-?){8}(?(1)\}|)\z}
|
9
|
+
|
10
|
+
alias_method :serialize, :deserialize
|
11
|
+
|
12
|
+
def type
|
13
|
+
:uuid
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def cast_value(value)
|
18
|
+
casted = value.to_s
|
19
|
+
casted if casted.match?(ACCEPTABLE_UUID)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module Materialize
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Vector < Type::Value # :nodoc:
|
8
|
+
attr_reader :delim, :subtype
|
9
|
+
|
10
|
+
# +delim+ corresponds to the `typdelim` column in the pg_types
|
11
|
+
# table. +subtype+ is derived from the `typelem` column in the
|
12
|
+
# pg_types table.
|
13
|
+
def initialize(delim, subtype)
|
14
|
+
@delim = delim
|
15
|
+
@subtype = subtype
|
16
|
+
end
|
17
|
+
|
18
|
+
# FIXME: this should probably split on +delim+ and use +subtype+
|
19
|
+
# to cast the values. Unfortunately, the current Rails behavior
|
20
|
+
# is to just return the string.
|
21
|
+
def cast(value)
|
22
|
+
value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module Materialize
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Xml < Type::String # :nodoc:
|
8
|
+
def type
|
9
|
+
:xml
|
10
|
+
end
|
11
|
+
|
12
|
+
def serialize(value)
|
13
|
+
return unless value
|
14
|
+
Data.new(super)
|
15
|
+
end
|
16
|
+
|
17
|
+
class Data # :nodoc:
|
18
|
+
def initialize(value)
|
19
|
+
@value = value
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
@value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/type"
|
4
|
+
require "active_record/connection_adapters/materialize/oid/array"
|
5
|
+
require "active_record/connection_adapters/materialize/oid/bit"
|
6
|
+
require "active_record/connection_adapters/materialize/oid/bit_varying"
|
7
|
+
require "active_record/connection_adapters/materialize/oid/bytea"
|
8
|
+
require "active_record/connection_adapters/materialize/oid/cidr"
|
9
|
+
require "active_record/connection_adapters/materialize/oid/date"
|
10
|
+
require "active_record/connection_adapters/materialize/oid/date_time"
|
11
|
+
require "active_record/connection_adapters/materialize/oid/decimal"
|
12
|
+
require "active_record/connection_adapters/materialize/oid/enum"
|
13
|
+
require "active_record/connection_adapters/materialize/oid/hstore"
|
14
|
+
require "active_record/connection_adapters/materialize/oid/inet"
|
15
|
+
require "active_record/connection_adapters/materialize/oid/jsonb"
|
16
|
+
require "active_record/connection_adapters/materialize/oid/money"
|
17
|
+
require "active_record/connection_adapters/materialize/oid/oid"
|
18
|
+
require "active_record/connection_adapters/materialize/oid/point"
|
19
|
+
require "active_record/connection_adapters/materialize/oid/legacy_point"
|
20
|
+
require "active_record/connection_adapters/materialize/oid/range"
|
21
|
+
require "active_record/connection_adapters/materialize/oid/specialized_string"
|
22
|
+
require "active_record/connection_adapters/materialize/oid/uuid"
|
23
|
+
require "active_record/connection_adapters/materialize/oid/vector"
|
24
|
+
require "active_record/connection_adapters/materialize/oid/xml"
|
25
|
+
|
26
|
+
require "active_record/connection_adapters/materialize/oid/type_map_initializer"
|
27
|
+
|
28
|
+
module ActiveRecord
|
29
|
+
module ConnectionAdapters
|
30
|
+
module Materialize
|
31
|
+
module OID # :nodoc:
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|