Class: Refile::Postgres::Backend

Inherits:
Object
  • Object
show all
Extended by:
BackendMacros
Includes:
SmartTransaction
Defined in:
lib/refile/postgres/backend.rb,
lib/refile/postgres/backend/reader.rb

Defined Under Namespace

Classes: Reader

Constant Summary collapse

RegistryTableDoesNotExistError =
Class.new(StandardError)
DEFAULT_REGISTRY_TABLE =
"refile_attachments"
DEFAULT_NAMESPACE =
"default"
PG_LARGE_OBJECT_METADATA_TABLE =
"pg_largeobject_metadata"
READ_CHUNK_SIZE =
16384

Constants included from SmartTransaction

SmartTransaction::INIT_CONNECTION_ARG_ERROR_MSG, SmartTransaction::PQTRANS_INTRANS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SmartTransaction

#ensure_in_transaction, #smart_transaction, #with_connection

Constructor Details

#initialize(connection_or_proc, max_size: nil, namespace: DEFAULT_NAMESPACE, registry_table: DEFAULT_REGISTRY_TABLE) ⇒ Backend

Returns a new instance of Backend.



12
13
14
15
16
17
18
# File 'lib/refile/postgres/backend.rb', line 12

def initialize(connection_or_proc, max_size: nil, namespace: DEFAULT_NAMESPACE, registry_table: DEFAULT_REGISTRY_TABLE)
  @connection_or_proc = connection_or_proc
  @namespace = namespace.to_s
  @registry_table = registry_table
  @registry_table_validated = false
  @max_size = max_size
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



20
21
22
# File 'lib/refile/postgres/backend.rb', line 20

def max_size
  @max_size
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



20
21
22
# File 'lib/refile/postgres/backend.rb', line 20

def namespace
  @namespace
end

Instance Method Details

#clear!(confirm = nil) ⇒ Object

Raises:

  • (Refile::Confirm)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/refile/postgres/backend.rb', line 117

def clear!(confirm = nil)
  raise Refile::Confirm unless confirm == :confirm
  registry_table
  with_connection do |connection|
    ensure_in_transaction(connection) do
      connection.exec_params(%{
        SELECT #{registry_table}.oid FROM #{registry_table}
        INNER JOIN #{PG_LARGE_OBJECT_METADATA_TABLE} ON #{registry_table}.oid = #{PG_LARGE_OBJECT_METADATA_TABLE}.oid
        WHERE #{registry_table}.namespace = $1::varchar;
      }, [namespace]) do |result|
        result.each_row do |row|
          connection.lo_unlink(row[0].to_i)
        end
      end
      connection.exec_params("DELETE FROM #{registry_table} WHERE namespace = $1::varchar;", [namespace])
    end
  end
end

#registry_tableObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/refile/postgres/backend.rb', line 22

def registry_table
  unless @registry_table_validated
    with_connection do |connection|
      connection.exec_params("SELECT * FROM pg_catalog.pg_tables WHERE tablename = $1::varchar;", [@registry_table]) do |result|
        if result.count == 0
          raise RegistryTableDoesNotExistError.new(%{Please create a table "#{@registry_table}" where backend could store list of attachments})
        end
      end
    end
    @registry_table_validated = true
  end
  @registry_table
end