Module: TenantLevelSecurity::SchemaDumper

Defined in:
lib/activerecord-tenant-level-security/schema_dumper.rb

Defined Under Namespace

Classes: Policy

Instance Method Summary collapse

Instance Method Details

#policies(stream) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/activerecord-tenant-level-security/schema_dumper.rb', line 10

def policies(stream)
  # Section Separator
  stream.puts if policies_in_database.any?

  policies_in_database.each do |policy|
    stream.puts(policy.to_schema)
  end
end

#policies_in_databaseObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activerecord-tenant-level-security/schema_dumper.rb', line 19

def policies_in_database
  query = <<~SQL
    SELECT
      tablename,
      qual
    FROM
      pg_policies
    WHERE
      policyname = 'tenant_policy'
    ORDER BY
      tablename;
  SQL
  results = ActiveRecord::Base.connection.execute(query)
  results.map do |result|
    table_name = result["tablename"]
    partition_key = convert_qual_to_partition_key(result["qual"])
    Policy.new(table_name: table_name, partition_key: partition_key)
  end
end

#tables(stream) ⇒ Object



5
6
7
8
# File 'lib/activerecord-tenant-level-security/schema_dumper.rb', line 5

def tables(stream)
  super
  policies(stream)
end