Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## Unreleased - xxxx-xx-xx
- Fix `db_belongs_to` raising `ActiveRecord::InvalidForeignKey` on PostgreSQL when the table the key is on has row-level security. PostgreSQL omits the `Key (...)=(...)` values from the violation there, `Key is not present in table "companies"`, so the columns are now read from the constraint the message names
- Fix `db_belongs_to` reporting a composite key's tenant association instead of the parent's. When every column's row exists, the key refuses the combination (a parent of another tenant); the error went to the first matching association, the tenant's in a key that leads with it, and now goes to the last, the parent's
- Fix `db_belongs_to` reporting a foreign key violated by another table as the record's own. A child saved inside the record's save (a callback, an autosaved association) whose key shares a column with one of the record's `db_belongs_to` associations, such as a composite key's tenant column, had its failure added as that association's `must exist` error, so `save` returned `false` instead of raising. The violation is now handled only when its table is the record's: PostgreSQL and MySQL name it in the message, SQLite in the failed statement; when it cannot be read, as before

## [2.1.2] - 2026-10-27
- Fix `db_belongs_to` raising `ActiveRecord::InvalidForeignKey` for a violated composite foreign key on PostgreSQL. Its error names every column of the key, `Key (tenant_id, company_id)`; each is now a candidate, so the association's `must exist` error is added instead
Expand Down
5 changes: 5 additions & 0 deletions lib/database_validations/lib/adapters/mysql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def foreign_key_error_column(error)
column = error.message[/FOREIGN KEY \(`([^`]+)`\)/, 1]
column ? [column] : []
end

# `a foreign key constraint fails (`db`.`tenant_users`, CONSTRAINT ...`
def foreign_key_error_table(error)
error.message[/fails \(`[^`]+`\.`([^`]+)`/, 1]
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/database_validations/lib/adapters/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def foreign_key_error_column(error)
columns = error.message[/Key \(([^)]+)\)/, 1]
columns ? columns.split(', ') : []
end

# `insert or update on table "tenant_users" violates foreign key constraint ...`
def foreign_key_error_table(error)
error.message[/on table "([^"]+)"/, 1]
end

# PostgreSQL leaves the columns out, `DETAIL: Key is not present in table "companies".`, when the table the
# key is on has row-level security for the current role, because the values could belong to a row the role
# cannot see. The constraint is still named, and its columns are the table's to answer.
def foreign_key_error_constraint(error)
error.message[/violates foreign key constraint "([^"]+)"/, 1]
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/database_validations/lib/adapters/sqlite_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ def unique_error_columns(error)
error.message.scan(/\w+\.([^,:]+)/).flatten
end

# SQLite's message names no table; the failed statement does.
def foreign_key_error_table(error)
return unless error.respond_to?(:sql) && error.sql

error.sql[/\A\s*(?:INSERT\s+INTO|UPDATE)\s+"?([^"\s(]+)"?/i, 1]
end

def foreign_key_error_column(error)
return [] unless error.respond_to?(:sql) && error.sql

Expand Down
58 changes: 40 additions & 18 deletions lib/database_validations/lib/rescuer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,41 @@ def handled?(instance, error, validate)
when ActiveRecord::RecordNotUnique
process(validate, instance, error, for_unique_index: :unique_index_name, for_db_uniqueness: :unique_error_columns)
when ActiveRecord::InvalidForeignKey
process(validate, instance, error, for_db_presence: :foreign_key_error_column)
return false unless own_table?(instance, error)

keys = foreign_key_columns(instance, error).map { |column| KeyGenerator.for_db_presence(column) }
process_keys(validate, instance, keys)
else false
end
end

# A foreign key of another table, a child saved along with the instance, is not the instance's to report: a
# composite key shares columns with the instance's own associations (`tenant_id`), so matching by column alone
# would pin the child's failure on one of them. A table the adapter cannot read is not ruled out.
def own_table?(instance, error)
table = Adapters.factory(instance.class).foreign_key_error_table(error)
table.nil? || table == instance.class.table_name.to_s.split('.').last
end

# The columns the error names, or, where the message leaves them out, the columns of the constraint it names.
def foreign_key_columns(instance, error)
adapter = Adapters.factory(instance.class)
columns = adapter.foreign_key_error_column(error)
return columns if columns.any? || !adapter.respond_to?(:foreign_key_error_constraint)

key = foreign_key_named(instance, adapter.foreign_key_error_constraint(error))
key ? Array(key.column).map(&:to_s) : []
end

def foreign_key_named(instance, name)
name && instance.class.connection.foreign_keys(instance.class.table_name).find { |key| key.name == name }
end

def process(validate, instance, error, key_types)
keys = resolve_keys(instance, error, key_types)
process_keys(validate, instance, resolve_keys(instance, error, key_types))
end

def process_keys(validate, instance, keys)
attribute_validator = find_matching_validator(instance, keys)
return false unless attribute_validator

Expand All @@ -25,37 +53,31 @@ def process(validate, instance, error, key_types)
def resolve_keys(instance, error, key_types)
adapter = Adapters.factory(instance.class)

key_types.flat_map do |key_generator, error_processor|
result = adapter.public_send(error_processor, error)

# FK adapters return an array of candidate columns, each generating a
# separate key. Uniqueness adapters return columns that form a single
# composite key, passed together to the key generator.
if key_generator == :for_db_presence
Array(result).map { |column| KeyGenerator.public_send(key_generator, column) }
else
[KeyGenerator.public_send(key_generator, result)]
end
# Uniqueness adapters return the columns of a single composite key, passed together to the key generator. A
# foreign key's candidate columns each make a key of their own (`foreign_key_columns`).
key_types.map do |key_generator, error_processor|
KeyGenerator.public_send(key_generator, adapter.public_send(error_processor, error))
end
end

def find_matching_validator(instance, keys)
first_match = nil
last_match = nil

keys.each do |key|
attribute_validator = instance._db_validators[key]
next unless attribute_validator

first_match ||= attribute_validator
last_match = attribute_validator

next if (keys.size > 1) && !foreign_key_invalid?(instance, attribute_validator)

return attribute_validator
end

# TOCTOU fallback: if disambiguate queries all passed (concurrent insert),
# use the first matching validator rather than leaving the error unhandled.
first_match
# Every candidate's row exists: the key refuses the combination, as a composite key that leads with a tenant
# column does for a parent of another tenant, or a concurrent change removed the row (TOCTOU). The last
# matching column is the parent's in such a key, not the tenant's, so its association gets the error.
last_match
end

def foreign_key_invalid?(instance, attribute_validator)
Expand Down
28 changes: 28 additions & 0 deletions spec/adapters/foreign_key_error_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ def build_error(message:, sql: nil)
)
expect(described_class.foreign_key_error_column(error)).to eq(%w[tenant_id company_id])
end

it 'extracts the table the violated key is on' do
error = build_error(
message: 'PG::ForeignKeyViolation: ERROR: insert or update on table "tenant_users" violates ' \
'foreign key constraint "fk_tenant_company" DETAIL: Key (tenant_id, company_id)=(2, 1) is not ' \
'present in table "companies".'
)
expect(described_class.foreign_key_error_table(error)).to eq('tenant_users')
end
end

describe DatabaseValidations::Adapters::MysqlAdapter do
Expand All @@ -33,9 +42,28 @@ def build_error(message:, sql: nil)
)
expect(described_class.foreign_key_error_column(error)).to eq(['company_id'])
end

it 'extracts the table the violated key is on' do
error = build_error(
message: 'Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails ' \
'(`test`.`db_belongs_users`, CONSTRAINT `fk_rails_abc123` FOREIGN KEY (`company_id`) ' \
'REFERENCES `companies` (`id`))'
)
expect(described_class.foreign_key_error_table(error)).to eq('db_belongs_users')
end
end

describe DatabaseValidations::Adapters::SqliteAdapter do
it 'extracts the table from the INSERT or UPDATE statement' do
insert = build_error(message: 'FOREIGN KEY constraint failed',
sql: 'INSERT INTO "db_belongs_users" ("company_id") VALUES (?)')
update = build_error(message: 'FOREIGN KEY constraint failed',
sql: 'UPDATE "db_belongs_users" SET "company_id" = ? WHERE "id" = ?')
expect(described_class.foreign_key_error_table(insert)).to eq('db_belongs_users')
expect(described_class.foreign_key_error_table(update)).to eq('db_belongs_users')
expect(described_class.foreign_key_error_table(build_error(message: 'FOREIGN KEY constraint failed'))).to be_nil
end

it 'extracts column from single-column INSERT SQL' do
error = build_error(
message: 'SQLite3::ConstraintException: FOREIGN KEY constraint failed',
Expand Down
63 changes: 63 additions & 0 deletions spec/validations/db_belongs_to_row_level_security_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
RSpec.describe '.db_belongs_to with PostgreSQL row-level security' do
# End to end for a foreign key violation that carries no `Key (...)=(...)` in its `DETAIL` line: see
# `Rescuer.foreign_key_columns`.
let(:role) { 'database_validations_rls_probe' }
let(:connection) { ActiveRecord::Base.connection }
let(:klass) { define_class(ActiveRecord::Base, :rls_members) { db_belongs_to :rls_parent } }

before do
define_database(postgresql_configuration)
stub_const('RlsParent', Class.new(ActiveRecord::Base) { self.table_name = 'rls_parents' })

can_create_roles = connection.select_value(
'SELECT rolsuper OR rolcreaterole FROM pg_roles WHERE rolname = current_user'
)
skip 'needs a role that can create roles' unless can_create_roles

connection.execute("DROP ROLE IF EXISTS #{role}")
connection.execute("CREATE ROLE #{role}")

ActiveRecord::Schema.define(version: 1) do
create_table :rls_parents
create_table :rls_members do |t|
t.belongs_to :rls_parent, foreign_key: true
end
end

# Row-level security on the table the key is on is what hides the key's values.
%w[rls_parents rls_members].each do |table|
connection.execute("GRANT SELECT, INSERT, UPDATE, DELETE ON #{table} TO #{role}")
connection.execute("GRANT USAGE, SELECT ON SEQUENCE #{table}_id_seq TO #{role}")
connection.execute("ALTER TABLE #{table} ENABLE ROW LEVEL SECURITY")
connection.execute("CREATE POLICY allow_all ON #{table} USING (true) WITH CHECK (true)")
end
connection.execute("SET ROLE #{role}")
end

after do
connection.execute('RESET ROLE')
connection.execute("DROP OWNED BY #{role}")
connection.execute("DROP ROLE #{role}")
end

it 'is run under a role that row-level security applies to (the premise of the other examples)' do
error = begin
connection.execute('INSERT INTO rls_members (rls_parent_id) VALUES (-1)')
rescue ActiveRecord::InvalidForeignKey => e
e
end

expect(error.message).to include('violates foreign key constraint')
expect(error.message).not_to match(/Key \(/)
end

it 'adds the error to the record instead of raising' do
record = klass.create(rls_parent_id: -1)

expect(record.errors.details).to eq(rls_parent: [{ error: :blank }])
end

it 'raises RecordInvalid from the bang methods' do
expect { klass.create!(rls_parent_id: -1) }.to raise_error(ActiveRecord::RecordInvalid)
end
end
50 changes: 50 additions & 0 deletions spec/validations/db_belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class DbBelongsUser < ActiveRecord::Base; end
class Department < ActiveRecord::Base; end
class MultiFkUser < ActiveRecord::Base; end
class TenantUser < ActiveRecord::Base; end
class TenantAccount < ActiveRecord::Base; end
class Tenant < ActiveRecord::Base; end
# rubocop:enable RSpec/LeakyConstantDeclaration
# rubocop:enable Lint/ConstantDefinitionInBlock

Expand Down Expand Up @@ -224,6 +226,54 @@ def define_tables
expect(record.save).to be false
expect(record.errors[:company]).to be_present
end

describe 'with the tenant declared too' do
before do
ActiveRecord::Schema.define(version: 5) do
create_table :tenants
add_foreign_key :tenant_users, :tenants, column: :tenant_id
end
Tenant.reset_column_information
Tenant.create!(id: 1)
Tenant.create!(id: 2)
end

it "adds the parent's error, not the tenant's, for a parent of another tenant" do
record_klass = define_class(TenantUser, :tenant_users) do
db_belongs_to :tenant
db_belongs_to :company
end
company = company_klass.create!(tenant_id: 1)
record = record_klass.new(company_id: company.id, tenant_id: 2)

expect(record.save).to be false
expect(record.errors[:company]).to be_present
expect(record.errors[:tenant]).to be_empty
end
end

describe "a child's key failing inside the record's save" do
# The child's key shares `company_id` with the record's own association, so matching by column alone would
# report the child's failure as the record's.
before do
ActiveRecord::Schema.define(version: 4) do
create_table(:tenant_accounts) { |t| t.belongs_to :company, foreign_key: true }
end
TenantUser.reset_column_information
end

it "raises it rather than reporting it as the record's association" do
account_klass = define_class(TenantAccount, :tenant_accounts) do
db_belongs_to :company

after_create { TenantUser.create!(company_id: company_id, tenant_id: 2) }
end
company = company_klass.create!(tenant_id: 1)

expect { account_klass.create(company_id: company.id) }
.to raise_error(ActiveRecord::InvalidForeignKey, /tenant_users/)
end
end
end
end

Expand Down
Loading