Method: ActiveRecord::PGExtensions::PessimisticMigrations#change_column_null
- Defined in:
- lib/active_record/pg_extensions/pessimistic_migrations.rb
#change_column_null(table, column, nullness, default = nil) ⇒ Object
adds a temporary check constraint to reduce locking when changing to NOT NULL, and we’re not in a transaction
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 |
# File 'lib/active_record/pg_extensions/pessimistic_migrations.rb', line 10 def change_column_null(table, column, nullness, default = nil) # no point in doing extra work to avoid locking if we're already in a transaction return super if nullness != false || open_transactions.positive? return if columns(table).find { |c| c.name == column.to_s }&.null == false # PG identifiers max out at 63 characters temp_constraint_name = "chk_rails_#{table}_#{column}_not_null"[0...63] scope = quoted_scope(temp_constraint_name) # check for temp constraint valid = select_value(" SELECT convalidated FROM pg_constraint INNER JOIN pg_namespace ON pg_namespace.oid=connamespace WHERE conname=\#{scope[:name]} AND nspname=\#{scope[:schema]}\n SQL\n if valid.nil?\n add_check_constraint(table,\n \"\#{quote_column_name(column)} IS NOT NULL\",\n name: temp_constraint_name,\n validate: false)\n end\n begin\n validate_constraint(table, temp_constraint_name)\n rescue PG::CheckViolation => e\n raise ActiveRecord::NotNullViolation.new(sql: e.sql, binds: e.binds)\n end\n\n transaction do\n super\n remove_check_constraint(table, name: temp_constraint_name)\n end\nend\n", "SCHEMA") |