Class: PgHaMigrations::Table
Instance Attribute Summary
Attributes inherited from Relation
#mode, #name, #schema
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Relation
#==, #conflicts_with?, connection, #eql?, #fully_qualified_name, #hash, #initialize, #present?
Class Method Details
.from_table_name(table, mode = nil) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/pg_ha_migrations/relation.rb', line 55
def self.from_table_name(table, mode=nil)
pg_name = ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.(table.to_s)
schema_conditional = if pg_name.schema
"#{connection.quote(pg_name.schema)}"
else
"ANY (current_schemas(false))"
end
schema = connection.select_value(<<~SQL)
SELECT schemaname
FROM pg_tables
WHERE tablename = #{connection.quote(pg_name.identifier)} AND schemaname = #{schema_conditional}
ORDER BY array_position(current_schemas(false), schemaname)
LIMIT 1
SQL
raise UndefinedTableError, "Table #{pg_name.quoted} does not exist#{" in search path" unless pg_name.schema}" unless schema.present?
new(pg_name.identifier, schema, mode)
end
|
Instance Method Details
#check_constraints ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/pg_ha_migrations/relation.rb', line 116
def check_constraints
connection.structs_from_sql(PgHaMigrations::CheckConstraint, <<~SQL)
SELECT conname AS name, pg_get_constraintdef(pg_constraint.oid) AS definition, convalidated AS validated
FROM pg_constraint, pg_class, pg_namespace
WHERE pg_class.oid = pg_constraint.conrelid
AND pg_class.relnamespace = pg_namespace.oid
AND pg_class.relname = #{connection.quote(name)}
AND pg_namespace.nspname = #{connection.quote(schema)}
AND pg_constraint.contype = 'c' -- 'c' stands for check constraints
SQL
end
|
#has_rows? ⇒ Boolean
128
129
130
|
# File 'lib/pg_ha_migrations/relation.rb', line 128
def has_rows?
connection.select_value("SELECT EXISTS (SELECT 1 FROM #{fully_qualified_name} LIMIT 1)")
end
|
#natively_partitioned? ⇒ Boolean
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/pg_ha_migrations/relation.rb', line 77
def natively_partitioned?
return @natively_partitioned if defined?(@natively_partitioned)
@natively_partitioned = !!connection.select_value(<<~SQL)
SELECT true
FROM pg_partitioned_table, pg_class, pg_namespace
WHERE pg_class.oid = pg_partitioned_table.partrelid
AND pg_class.relnamespace = pg_namespace.oid
AND pg_class.relname = #{connection.quote(name)}
AND pg_namespace.nspname = #{connection.quote(schema)}
SQL
end
|
#partitions(include_sub_partitions: false, include_self: false) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/pg_ha_migrations/relation.rb', line 90
def partitions(include_sub_partitions: false, include_self: false)
tables = connection.structs_from_sql(self.class, <<~SQL)
SELECT child.relname AS name, child_ns.nspname AS schema, NULLIF('#{mode}', '') AS mode
FROM pg_inherits
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
JOIN pg_class child ON pg_inherits.inhrelid = child.oid
JOIN pg_namespace parent_ns ON parent.relnamespace = parent_ns.oid
JOIN pg_namespace child_ns ON child.relnamespace = child_ns.oid
WHERE parent.relname = #{connection.quote(name)}
AND parent_ns.nspname = #{connection.quote(schema)}
ORDER BY child.oid -- Ensure consistent ordering for tests
SQL
if include_sub_partitions
sub_partitions = tables.each_with_object([]) do |table, arr|
arr.concat(table.partitions(include_sub_partitions: true))
end
tables.concat(sub_partitions)
end
tables.prepend(self) if include_self
tables
end
|
#total_bytes ⇒ Object
132
133
134
135
136
137
138
139
|
# File 'lib/pg_ha_migrations/relation.rb', line 132
def total_bytes
connection.select_value(<<~SQL)
SELECT pg_total_relation_size(pg_class.oid)
FROM pg_class, pg_namespace
WHERE pg_class.relname = #{connection.quote(name)}
AND pg_namespace.nspname = #{connection.quote(schema)}
SQL
end
|