Class: Webhookdb::DBAdapter::PG
Constant Summary
collapse
- VERIFY_TIMEOUT =
2
- VERIFY_STATEMENT =
"SELECT 1"
- COLTYPE_MAP =
{
BIGINT => "bigint",
BIGINT_ARRAY => "bigint[]",
BOOLEAN => "boolean",
DATE => "date",
DECIMAL => "numeric",
DOUBLE => "double precision",
FLOAT => "float",
INTEGER => "integer",
INTEGER_ARRAY => "integer[]",
OBJECT => "jsonb",
TEXT => "text",
TEXT_ARRAY => "text[]",
TIMESTAMP => "timestamptz",
UUID => "uuid",
:serial => "serial",
:bigserial => "bigserial",
}.freeze
Constants included
from DefaultSql
DefaultSql::PG_RESERVED_KEYWORDS, DefaultSql::RESERVED_KEYWORDS
Constants included
from ColumnTypes
ColumnTypes::BIGINT, ColumnTypes::BIGINT_ARRAY, ColumnTypes::BOOLEAN, ColumnTypes::COLUMN_TYPES, ColumnTypes::DATE, ColumnTypes::DECIMAL, ColumnTypes::DOUBLE, ColumnTypes::FLOAT, ColumnTypes::INTEGER, ColumnTypes::INTEGER_ARRAY, ColumnTypes::OBJECT, ColumnTypes::TEXT, ColumnTypes::TEXT_ARRAY, ColumnTypes::TIMESTAMP, ColumnTypes::UUID
INVALID_IDENTIFIER_MESSAGE, INVALID_IDENTIFIER_PROMPT, VALID_IDENTIFIER
Instance Attribute Summary
#name, #table, #targets, #unique
Instance Method Summary
collapse
-
#add_column_sql(table, column, if_not_exists: false) ⇒ Object
-
#create_column_sql(column) ⇒ Object
-
#create_hash_partition_sql(table, partition_count, remainder) ⇒ Object
-
#create_index_sql(index, concurrently:) ⇒ Object
-
#create_index_sqls(index, concurrently:, partitions: []) ⇒ Object
-
#create_table_sql(table, columns, if_not_exists: false, partition: nil) ⇒ Object
-
#identifier_quote_char ⇒ Object
-
#merge_from_csv(connection, file, table, pk_col, copy_columns) ⇒ Object
-
#verify_connection(url, timeout: 2, statement: "SELECT 1") ⇒ Object
Methods included from DefaultSql
#assign_columns_sql, #create_schema_sql, #escape_identifier, #qualify_table
adapter, #connection, #create_schema_sql, #escape_identifier, supported_adapters_message, valid_identifier?, validate_identifier!
Instance Method Details
#add_column_sql(table, column, if_not_exists: false) ⇒ Object
115
116
117
118
119
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 115
def add_column_sql(table, column, if_not_exists: false)
c = self.create_column_sql(column)
ifne = if_not_exists ? " IF NOT EXISTS" : ""
return "ALTER TABLE #{self.qualify_table(table)} ADD COLUMN#{ifne} #{c}"
end
|
#create_column_sql(column) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 92
def create_column_sql(column)
modifiers = +""
coltype = COLTYPE_MAP.fetch(column.type)
if column.pk?
coltype = "bigserial" if column.type == BIGINT
coltype = "serial" if column.type == INTEGER
modifiers << " PRIMARY KEY"
elsif column.unique?
modifiers << " UNIQUE NOT NULL"
elsif !column.nullable?
modifiers << " NOT NULL"
end
colname = self.escape_identifier(column.name)
return "#{colname} #{coltype}#{modifiers}"
end
|
#create_hash_partition_sql(table, partition_count, remainder) ⇒ Object
108
109
110
111
112
113
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 108
def create_hash_partition_sql(table, partition_count, remainder)
tbl = self.qualify_table(table)
s = "CREATE TABLE #{tbl}_#{remainder} PARTITION OF #{tbl} " \
"FOR VALUES WITH (MODULUS #{partition_count}, REMAINDER #{remainder})"
return s
end
|
#create_index_sql(index, concurrently:) ⇒ Object
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 16
def create_index_sql(index, concurrently:)
tgts = index.targets.map { |c| self.escape_identifier(c.name) }.join(", ")
uniq = index.unique ? " UNIQUE" : ""
concurrent = concurrently ? " CONCURRENTLY" : ""
idxname = self.escape_identifier(index.name)
tblname = self.qualify_table(index.table)
where = ""
where = " " + Webhookdb::Customer.where(index.where).sql.delete_prefix('SELECT * FROM "customers" ') if index.where
return "CREATE#{uniq} INDEX#{concurrent} IF NOT EXISTS #{idxname} ON #{tblname} (#{tgts})#{where}"
end
|
#create_index_sqls(index, concurrently:, partitions: []) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 27
def create_index_sqls(index, concurrently:, partitions: [])
return super if partitions.empty?
result = []
result << self.create_index_sql(index, concurrently: false).gsub(" ON ", " ON ONLY ")
partitions.each do |partition|
partition_idx = index.change(table: partition.partition_table, name: "#{index.name}#{partition.suffix}")
result << self.create_index_sql(partition_idx, concurrently:)
result << "ALTER INDEX #{index.name} ATTACH PARTITION #{partition_idx.name}"
end
return result
end
|
#create_table_sql(table, columns, if_not_exists: false, partition: nil) ⇒ Object
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 39
def create_table_sql(table, columns, if_not_exists: false, partition: nil)
columns = columns.to_a
createtable = +"CREATE TABLE "
createtable << "IF NOT EXISTS " if if_not_exists
createtable << self.qualify_table(table)
partitioned_pks = []
partitioned_uniques = []
if partition
columns.each_with_index do |c, i|
if c.pk?
type = case c.type
when BIGINT
:bigserial
when INTEGER
:serial
else
c.type
end
columns[i] = c.change(pk: false, type:)
partitioned_pks << c
elsif c.unique?
columns[i] = c.change(unique: false)
partitioned_uniques << c
end
end
end
tbl_lines = columns.map { |c| self.create_column_sql(c) }
tbl_lines.concat(partitioned_pks.map do |c|
pkcols = [partition.column, c.name].uniq.join(", ")
"PRIMARY KEY (#{pkcols})"
end)
tbl_lines.concat(partitioned_uniques.map { |c| "UNIQUE (#{partition.column}, #{c.name})" })
lines = ["#{createtable} ("]
lines << (" " + tbl_lines.join(",\n "))
lines << ")"
if partition
m = case partition.by
when Webhookdb::DBAdapter::Partitioning::HASH
"HASH"
when Webhookdb::DBAdapter::Partitioning::RANGE
"RANGE"
else
raise ArgumentError, "unknown partition method: #{partition.by}"
end
lines << "PARTITION BY #{m} (#{partition.column})"
end
return lines.join("\n")
end
|
#identifier_quote_char ⇒ Object
14
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 14
def identifier_quote_char = '"'
|
#merge_from_csv(connection, file, table, pk_col, copy_columns) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 121
def merge_from_csv(connection, file, table, pk_col, copy_columns)
qtable = self.qualify_table(table)
temptable = "#{self.escape_identifier(table.name)}_staging_#{SecureRandom.hex(4)}"
connection.using do |db|
db << "CREATE TEMP TABLE #{temptable} (LIKE #{qtable})"
db.copy_into(temptable.to_sym, options: "DELIMITER ',', HEADER true, FORMAT csv", data: file)
pkname = self.escape_identifier(pk_col.name)
col_assigns = self.assign_columns_sql("src", nil, copy_columns)
upsert_sql = [
<<~UPDATE,
UPDATE #{qtable} AS tgt
SET #{col_assigns} FROM
(SELECT * FROM #{temptable} WHERE #{pkname} IN (SELECT #{pkname} FROM #{qtable})) src
WHERE tgt.#{pkname} = src.#{pkname};
UPDATE
"INSERT INTO #{qtable} SELECT * FROM #{temptable} WHERE #{pkname} NOT IN (SELECT #{pkname} FROM #{qtable});",
]
db << upsert_sql.join("\n")
end
end
|
#verify_connection(url, timeout: 2, statement: "SELECT 1") ⇒ Object
142
143
144
145
146
147
148
|
# File 'lib/webhookdb/db_adapter/pg.rb', line 142
def verify_connection(url, timeout: 2, statement: "SELECT 1")
conn = self.connection(url)
conn.using(connect_timeout: timeout) do |c|
c.execute("SET statement_timeout TO #{timeout * 1000}")
c.execute(statement)
end
end
|