Class: Foundries::Snapshot::Adapters::PostgresAdapter
- Inherits:
-
Object
- Object
- Foundries::Snapshot::Adapters::PostgresAdapter
- Defined in:
- lib/foundries/snapshot/adapters/postgres_adapter.rb
Instance Method Summary collapse
- #capture(table_name, io) ⇒ Object
- #count(table_name) ⇒ Object
- #disable_referential_integrity ⇒ Object
- #empty?(table_name) ⇒ Boolean
-
#initialize(connection) ⇒ PostgresAdapter
constructor
A new instance of PostgresAdapter.
- #reset_sequence(table_name) ⇒ Object
- #restore(table_name, io) ⇒ Object
- #table_names ⇒ Object
Constructor Details
#initialize(connection) ⇒ PostgresAdapter
Returns a new instance of PostgresAdapter.
7 8 9 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 7 def initialize(connection) @connection = connection end |
Instance Method Details
#capture(table_name, io) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 23 def capture(table_name, io) raw = @connection.raw_connection raw.copy_data("COPY #{quoted(table_name)} TO STDOUT") do while (row = raw.get_copy_data) io.write(row) end end end |
#count(table_name) ⇒ Object
19 20 21 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 19 def count(table_name) @connection.select_value("SELECT COUNT(*) FROM #{quoted(table_name)}") end |
#disable_referential_integrity ⇒ Object
41 42 43 44 45 46 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 41 def disable_referential_integrity @connection.execute("SET session_replication_role = replica") yield ensure @connection.execute("SET session_replication_role = DEFAULT") end |
#empty?(table_name) ⇒ Boolean
15 16 17 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 15 def empty?(table_name) @connection.select_value("SELECT NOT EXISTS (SELECT 1 FROM #{quoted(table_name)})") end |
#reset_sequence(table_name) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 48 def reset_sequence(table_name) pk = @connection.primary_key(table_name) return unless pk # Check for sequence first — UUID PKs have no sequence seq = @connection.select_value( "SELECT pg_get_serial_sequence(#{@connection.quote(table_name)}, #{@connection.quote(pk)})" ) return unless seq max_id = @connection.select_value( "SELECT COALESCE(MAX(#{@connection.quote_column_name(pk)}), 0) FROM #{quoted(table_name)}" ) @connection.execute("SELECT setval(#{@connection.quote(seq)}, #{max_id})") end |
#restore(table_name, io) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 32 def restore(table_name, io) raw = @connection.raw_connection raw.copy_data("COPY #{quoted(table_name)} FROM STDIN") do io.each_line do |line| raw.put_copy_data(line) end end end |
#table_names ⇒ Object
11 12 13 |
# File 'lib/foundries/snapshot/adapters/postgres_adapter.rb', line 11 def table_names @connection.tables - %w[schema_migrations ar_internal_metadata] end |