Class: StrongMigrations::Adapters::PostgreSQLAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- StrongMigrations::Adapters::PostgreSQLAdapter
- Defined in:
- lib/strong_migrations/adapters/postgresql_adapter.rb
Instance Method Summary collapse
- #add_column_default_safe? ⇒ Boolean
- #analyze_table(table) ⇒ Object
- #auto_incrementing_types ⇒ Object
- #change_type_safe?(table, column, type, options, existing_column, existing_type) ⇒ Boolean
- #check_lock_timeout(limit) ⇒ Object
- #constraints(table, column) ⇒ Object
-
#default_volatile?(default) ⇒ Boolean
default to true if unsure.
-
#index_corruption? ⇒ Boolean
only check in non-developer environments (where actual server version is used).
- #max_constraint_name_length ⇒ Object
- #min_version ⇒ Object
- #name ⇒ Object
- #server_version ⇒ Object
- #set_lock_timeout(timeout) ⇒ Object
- #set_statement_timeout(timeout) ⇒ Object
- #set_transaction_timeout(timeout) ⇒ Object
- #writes_blocked? ⇒ Boolean
Methods inherited from AbstractAdapter
Constructor Details
This class inherits a constructor from StrongMigrations::Adapters::AbstractAdapter
Instance Method Details
#add_column_default_safe? ⇒ Boolean
49 50 51 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 49 def add_column_default_safe? true end |
#analyze_table(table) ⇒ Object
45 46 47 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 45 def analyze_table(table) connection.execute "ANALYZE #{connection.quote_table_name(table.to_s)}" end |
#auto_incrementing_types ⇒ Object
162 163 164 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 162 def auto_incrementing_types ["primary_key", "serial", "bigserial"] end |
#change_type_safe?(table, column, type, options, existing_column, existing_type) ⇒ Boolean
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 53 def change_type_safe?(table, column, type, , existing_column, existing_type) safe = false case type.to_s when "string" # safe to increase limit or remove it # not safe to decrease limit or add a limit case existing_type when "character varying" safe = ![:limit] || (existing_column.limit && [:limit] >= existing_column.limit) when "text" safe = ![:limit] when "citext" safe = ![:limit] && !indexed?(table, column) end when "text" # safe to change varchar to text (and text to text) safe = ["character varying", "text"].include?(existing_type) || (existing_type == "citext" && !indexed?(table, column)) when "citext" safe = ["character varying", "text"].include?(existing_type) && !indexed?(table, column) when "varbit" # increasing length limit or removing the limit is safe # but there doesn't seem to be a way to set/modify it # https://wiki.postgresql.org/wiki/What%27s_new_in_PostgreSQL_9.2#Reduce_ALTER_TABLE_rewrites when "numeric", "decimal" # numeric and decimal are equivalent and can be used interchangeably safe = ["numeric", "decimal"].include?(existing_type) && ( ( # unconstrained ![:precision] && ![:scale] ) || ( # increased precision, same scale [:precision] && existing_column.precision && [:precision] >= existing_column.precision && [:scale] == existing_column.scale ) ) when "datetime", "timestamp", "timestamptz" # precision for datetime # limit for timestamp, timestamptz precision = (type.to_s == "datetime" ? [:precision] : [:limit]) || 6 existing_precision = existing_column.limit || existing_column.precision || 6 type_map = { "timestamp" => "timestamp without time zone", "timestamptz" => "timestamp with time zone" } maybe_safe = type_map.value?(existing_type) && precision >= existing_precision if maybe_safe new_type = type.to_s == "datetime" ? datetime_type : type.to_s # resolve with fallback new_type = type_map[new_type] || new_type safe = new_type == existing_type || time_zone == "UTC" end when "time" precision = [:precision] || [:limit] || 6 existing_precision = existing_column.precision || existing_column.limit || 6 safe = existing_type == "time without time zone" && precision >= existing_precision when "timetz" # increasing precision is safe # but there doesn't seem to be a way to set/modify it when "interval" # https://wiki.postgresql.org/wiki/What%27s_new_in_PostgreSQL_9.2#Reduce_ALTER_TABLE_rewrites # Active Record uses precision before limit precision = [:precision] || [:limit] || 6 existing_precision = existing_column.precision || existing_column.limit || 6 safe = existing_type == "interval" && precision >= existing_precision when "inet" safe = existing_type == "cidr" end safe end |
#check_lock_timeout(limit) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 35 def check_lock_timeout(limit) lock_timeout = connection.select_all("SHOW lock_timeout").first["lock_timeout"] lock_timeout_sec = timeout_to_sec(lock_timeout) if lock_timeout_sec == 0 warn "[strong_migrations] DANGER: No lock timeout set" elsif lock_timeout_sec > limit warn "[strong_migrations] DANGER: Lock timeout is longer than #{limit} seconds: #{lock_timeout}" end end |
#constraints(table, column) ⇒ Object
170 171 172 173 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 170 def constraints(table, column) # TODO improve column check connection.check_constraints(table).select { |c| /\b#{Regexp.escape(column.to_s)}\b/.match?(c.expression) } end |
#default_volatile?(default) ⇒ Boolean
default to true if unsure
156 157 158 159 160 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 156 def default_volatile?(default) name = default.to_s.delete_suffix("()") rows = select_all("SELECT provolatile FROM pg_proc WHERE proname = #{connection.quote(name)}").to_a rows.empty? || rows.any? { |r| r["provolatile"] == "v" } end |
#index_corruption? ⇒ Boolean
only check in non-developer environments (where actual server version is used)
149 150 151 152 153 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 149 def index_corruption? server_version >= Gem::Version.new("14.0") && server_version < Gem::Version.new("14.4") && !StrongMigrations.developer_env? end |
#max_constraint_name_length ⇒ Object
166 167 168 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 166 def max_constraint_name_length 63 end |
#min_version ⇒ Object
8 9 10 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 8 def min_version "12" end |
#name ⇒ Object
4 5 6 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 4 def name "PostgreSQL" end |
#server_version ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 12 def server_version @version ||= begin target_version(StrongMigrations.target_postgresql_version) do version = select_all("SHOW server_version_num").first["server_version_num"].to_i # major and minor version "#{version / 10000}.#{(version % 10000)}" end end end |
#set_lock_timeout(timeout) ⇒ Object
31 32 33 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 31 def set_lock_timeout(timeout) set_timeout("lock_timeout", timeout) end |
#set_statement_timeout(timeout) ⇒ Object
22 23 24 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 22 def set_statement_timeout(timeout) set_timeout("statement_timeout", timeout) end |
#set_transaction_timeout(timeout) ⇒ Object
26 27 28 29 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 26 def set_transaction_timeout(timeout) # TODO make sure true version supports it as well? set_timeout("transaction_timeout", timeout) if server_version >= Gem::Version.new("17") end |
#writes_blocked? ⇒ Boolean
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/strong_migrations/adapters/postgresql_adapter.rb', line 135 def writes_blocked? query = <<~SQL SELECT relation::regclass::text FROM pg_locks WHERE mode IN ('ShareRowExclusiveLock', 'AccessExclusiveLock') AND pid = pg_backend_pid() SQL select_all(query.squish).any? end |