Sha256: a0f3d3d0766f64520dbc9940cc547d781906402bb7b0b4a7f3e41ad8fc2407e3

Contents?: true

Size: 969 Bytes

Versions: 3

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

module TenantLevelSecurity
  module SchemaDumper
    def tables(stream)
      super
      policies(stream)
    end

    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

    def policies_in_database
      query = <<~SQL
        SELECT
          tablename
        FROM
          pg_policies
        WHERE
          policyname = 'tenant_policy'
        ORDER BY
          tablename;
      SQL
      results = ActiveRecord::Base.connection.execute(query)
      table_names = results.map { |x| x["tablename"] }

      table_names.map { |t| Policy.new(t) }
    end

    class Policy
      def initialize(table_name)
        @table_name = table_name
      end

      def to_schema
        %(  create_policy "#{table_name}")
      end

      private

      attr_reader :table_name
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-tenant-level-security-0.2.0 lib/activerecord-tenant-level-security/schema_dumper.rb
activerecord-tenant-level-security-0.1.0 lib/activerecord-tenant-level-security/schema_dumper.rb
activerecord-tenant-level-security-0.0.1 lib/activerecord-tenant-level-security/schema_dumper.rb