Skip to content

Commit cc30950

Browse files
committed
fix: solve internal schema issue
1 parent 98579da commit cc30950

2 files changed

Lines changed: 37 additions & 27 deletions

File tree

db/migrate/20260125000001_enable_rls_on_remaining_tables.rb

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def up
1818
enable_rls_on_table(:support_faqs)
1919
enable_rls_on_table(:organizations)
2020

21-
# Enable RLS on Rails internal tables (block all API access)
22-
enable_rls_on_table(:ar_internal_metadata)
23-
enable_rls_on_table(:schema_migrations)
21+
# NOTE: schema_migrations and ar_internal_metadata intentionally excluded.
22+
# Adding FORCE RLS with deny-all to Rails internal tables breaks db:migrate
23+
# on every deploy. These tables are not exposed via any API and need no RLS.
2424

2525
# ===========================================================================
2626
# SUPPORT TICKETS - Organization scoped
@@ -297,24 +297,6 @@ def up
297297
USING (false);
298298
SQL
299299

300-
# ===========================================================================
301-
# RAILS INTERNAL TABLES - Block all API access
302-
# These should never be accessible via PostgREST/API
303-
# ===========================================================================
304-
305-
# ar_internal_metadata - Rails internal
306-
execute <<-SQL
307-
CREATE POLICY ar_internal_metadata_deny_all ON ar_internal_metadata
308-
FOR ALL
309-
USING (false);
310-
SQL
311-
312-
# schema_migrations - Rails internal
313-
execute <<-SQL
314-
CREATE POLICY schema_migrations_deny_all ON schema_migrations
315-
FOR ALL
316-
USING (false);
317-
SQL
318300
end
319301

320302
def down
@@ -372,10 +354,6 @@ def down
372354
drop_policy(:organizations, 'organizations_update_policy')
373355
drop_policy(:organizations, 'organizations_delete_policy')
374356

375-
# Drop policies for Rails internal tables
376-
drop_policy(:ar_internal_metadata, 'ar_internal_metadata_deny_all')
377-
drop_policy(:schema_migrations, 'schema_migrations_deny_all')
378-
379357
# Disable RLS
380358
disable_rls_on_table(:support_tickets)
381359
disable_rls_on_table(:support_ticket_messages)
@@ -386,8 +364,6 @@ def down
386364
disable_rls_on_table(:opponent_teams)
387365
disable_rls_on_table(:support_faqs)
388366
disable_rls_on_table(:organizations)
389-
disable_rls_on_table(:ar_internal_metadata)
390-
disable_rls_on_table(:schema_migrations)
391367
end
392368

393369
private
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
# Rails internal tables (schema_migrations, ar_internal_metadata) should never
4+
# have RLS. With FORCE ROW LEVEL SECURITY and a deny-all policy, db:migrate
5+
# fails on every deploy because the prostaff user cannot INSERT into
6+
# schema_migrations to record completed migrations. This migration undoes the
7+
# RLS applied by EnableRlsOnRemainingTables (20260125000001).
8+
class RemoveRlsFromRailsInternalTables < ActiveRecord::Migration[7.2]
9+
def up
10+
execute "DROP POLICY IF EXISTS schema_migrations_deny_all ON schema_migrations;"
11+
execute "ALTER TABLE schema_migrations NO FORCE ROW LEVEL SECURITY;"
12+
execute "ALTER TABLE schema_migrations DISABLE ROW LEVEL SECURITY;"
13+
14+
execute "DROP POLICY IF EXISTS ar_internal_metadata_deny_all ON ar_internal_metadata;"
15+
execute "ALTER TABLE ar_internal_metadata NO FORCE ROW LEVEL SECURITY;"
16+
execute "ALTER TABLE ar_internal_metadata DISABLE ROW LEVEL SECURITY;"
17+
end
18+
19+
def down
20+
execute "ALTER TABLE schema_migrations ENABLE ROW LEVEL SECURITY;"
21+
execute "ALTER TABLE schema_migrations FORCE ROW LEVEL SECURITY;"
22+
execute <<-SQL
23+
CREATE POLICY schema_migrations_deny_all ON schema_migrations
24+
FOR ALL USING (false);
25+
SQL
26+
27+
execute "ALTER TABLE ar_internal_metadata ENABLE ROW LEVEL SECURITY;"
28+
execute "ALTER TABLE ar_internal_metadata FORCE ROW LEVEL SECURITY;"
29+
execute <<-SQL
30+
CREATE POLICY ar_internal_metadata_deny_all ON ar_internal_metadata
31+
FOR ALL USING (false);
32+
SQL
33+
end
34+
end

0 commit comments

Comments
 (0)