Module: PgVersions

Defined in:
lib/pg_versions/pg_versions.rb,
lib/pg_versions.rb,
lib/pg_versions/rails.rb,
lib/pg_versions/version.rb

Overview

TODO: prepared statements?

Defined Under Namespace

Classes: Connection, ConnectionThread, Engine, InvalidParameters, Notification

Constant Summary collapse

VERSION =
"2.1"

Class Method Summary collapse

Class Method Details

.bump(*channels, connection: nil) ⇒ Object

TODO: ensure this is called only once per transaction, or that all bumps occur in the same order in all transactions, to avoid deadlocks



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pg_versions/pg_versions.rb', line 83

def self.bump(*channels, connection: nil)
  #TODO: pg_connection.exec returned nil once during testing.
  PgVersions.with_connection(connection) { |pg_connection|
    channels = [channels].flatten.sort
    return {} if channels.size == 0
    quoted_channels = channels.map.with_index { |channel, i| "(#{i},'#{pg_connection.escape_string(channel)}')" }.join(", ")
    # table-wide share lock is there to mutually exclude table cleaner
    # clock_timestamp() - this has to be a timestamp after table lock got acquired
    pg_connection.exec("
      LOCK TABLE pg_versions IN ACCESS SHARE MODE;
      WITH 
        to_bump(i, channel) AS (VALUES #{quoted_channels})
        , current_instant(ts) AS (VALUES (clock_timestamp()))
        , updated AS (
            INSERT INTO pg_versions(channel, instant, counter)
            SELECT to_bump.channel, (SELECT ts FROM current_instant), 0 FROM to_bump
            ON CONFLICT (channel) DO UPDATE SET
              instant = GREATEST(pg_versions.instant, EXCLUDED.instant),
              counter = CASE WHEN pg_versions.instant < EXCLUDED.instant THEN 0 ELSE pg_versions.counter + 1 END
            RETURNING channel, instant, pg_versions.counter
            )
      SELECT DISTINCT
        i
        , #{timestamp_to_integers('updated.instant')} || ',' || updated.counter::text AS version
        , pg_notify(updated.channel::text, #{timestamp_to_integers('updated.instant')} || ',' || updated.counter::text)::text
      FROM
        to_bump
        JOIN updated ON to_bump.channel = updated.channel;
    ") { |result| 
      result.map { |row| [channels[Integer(row["i"])], string_to_version(row["version"])] }.to_h
    }
  }
end

.create_table(connection = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/pg_versions/pg_versions.rb', line 66

def self.create_table(connection=nil)
  PgVersions.with_connection(connection) { |pg_connection|
    open(File.dirname(__FILE__)+"/../../create-table.sql") { |sql_file|
      pg_connection.exec sql_file.read
    }
  }
end

.drop_table(connection = nil) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/pg_versions/pg_versions.rb', line 74

def self.drop_table(connection=nil)
  PgVersions.with_connection(connection) { |pg_connection|
    open(File.dirname(__FILE__)+"/../../drop-table.sql") { |sql_file|
      pg_connection.exec sql_file.read
    }
  }
end

.read(*channels, connection: nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pg_versions/pg_versions.rb', line 118

def self.read(*channels, connection: nil)
  PgVersions.with_connection(connection) { |pg_connection|
    channels = [channels].flatten.sort
    return {} if channels.size == 0
    versions = {}
    quoted_channels = channels.map.with_index { |channel, i| "(#{i},'#{pg_connection.escape_string(channel)}')" }.join(", ")
    not_found_channels = pg_connection.exec("
      LOCK TABLE pg_versions IN ACCESS SHARE MODE;
      WITH
        channels(i, channel) AS (VALUES #{quoted_channels})
      SELECT
        i
        , #{timestamp_to_integers('instant')} || ',' || counter AS version
      FROM
        channels
        JOIN pg_versions ON pg_versions.channel = channels.channel
      ORDER BY
        i DESC;
    ") { |result|
      result.each { |row|
        versions[channels.delete_at(Integer(row["i"]))] = string_to_version(row["version"])
      }
    }
    #TODO: bump in the same query instead of calling bump
    versions.merge!(self.bump(channels, connection: pg_connection))  if channels.size > 0
    versions
  }
end

.string_to_version(version_str) ⇒ Object



61
62
63
# File 'lib/pg_versions/pg_versions.rb', line 61

def self.string_to_version(version_str) 
  version_str.split(",").map { |str| Integer(str) }
end

.timestamp_to_integers(input) ⇒ Object



37
38
39
# File 'lib/pg_versions/pg_versions.rb', line 37

def self.timestamp_to_integers(input)
  "to_char(%s, 'YYYYMMDD')::integer || ',' || to_char(%s, 'HH24MISS')::integer || ',' || to_char(%s, 'US')::integer"%[input, input, input]
end

.with_connection(pg_connection, reset: false, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pg_versions/pg_versions.rb', line 42

def self.with_connection(pg_connection, reset: false, &block)
  if pg_connection.kind_of? PG::Connection
    if reset
      pg_connection.sync_reset
      pg_connection.exec("select;")
    end
    block.call(pg_connection)
  elsif pg_connection.respond_to? :call
    pg_connection.call(reset, &block)
  elsif pg_connection.nil? and defined? ActiveRecord
    ActiveRecord::Base.connection_pool.with_connection { |ar_connection|
      block.call(ar_connection.instance_variable_get(:@connection))
    }
  else
    raise InvalidParameters, "Missing connection. Either pass pg connection object or import ActiveRecord."
  end
end