Class: Refile::Postgres::Backend
- Inherits:
-
Object
- Object
- Refile::Postgres::Backend
- 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 =
3000- INIT_CONNECTION_ARG_ERROR_MSG =
"When initializing new Refile::Postgres::Backend first argument should be an instance of PG::Connection or a lambda/proc that returns it. When using ActiveRecord it is available as ActiveRecord::Base.connection.raw_connection"
Constants included from SmartTransaction
SmartTransaction::PQTRANS_INTRANS
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
- #clear!(confirm = nil) ⇒ Object
- #connection ⇒ Object
-
#initialize(connection_or_proc, max_size: nil, namespace: DEFAULT_NAMESPACE, registry_table: DEFAULT_REGISTRY_TABLE) ⇒ Backend
constructor
A new instance of Backend.
- #registry_table ⇒ Object
Methods included from SmartTransaction
#ensure_in_transaction, #smart_transaction
Constructor Details
#initialize(connection_or_proc, max_size: nil, namespace: DEFAULT_NAMESPACE, registry_table: DEFAULT_REGISTRY_TABLE) ⇒ Backend
Returns a new instance of Backend.
13 14 15 16 17 18 19 |
# File 'lib/refile/postgres/backend.rb', line 13 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_size ⇒ Object (readonly)
Returns the value of attribute max_size.
21 22 23 |
# File 'lib/refile/postgres/backend.rb', line 21 def max_size @max_size end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/refile/postgres/backend.rb', line 21 def namespace @namespace end |
Instance Method Details
#clear!(confirm = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/refile/postgres/backend.rb', line 115 def clear!(confirm = nil) raise Refile::Confirm unless confirm == :confirm registry_table ensure_in_transaction do connection.exec_params(%{ SELECT * FROM #{registry_table} INNER JOIN #{PG_LARGE_OBJECT_METADATA_TABLE} ON #{registry_table}.id = #{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_s.to_i) end end connection.exec_params("DELETE FROM #{registry_table} WHERE namespace = $1::varchar;", [namespace]) end end |
#connection ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/refile/postgres/backend.rb', line 38 def connection if has_active_connection? @connection else obtain_new_connection end end |
#registry_table ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/refile/postgres/backend.rb', line 23 def registry_table unless @registry_table_validated connection.exec %{ SELECT count(*) from pg_catalog.pg_tables WHERE tablename = '#{@registry_table}'; } do |result| unless result[0]["count"].to_i > 0 raise RegistryTableDoesNotExistError.new(%{Please create a table "#{@registry_table}" where backend could store list of attachments}) end end @registry_table_validated = true end @registry_table end |