koyo-postgres-replication 0.1.1.pre → 0.1.4.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52f66b5b4c592753e8cb81e6291a035289cce3f58bc610e4d78de5a1c0bb6fa9
4
- data.tar.gz: bbb086947453e1937f052ed86e6dd68efd4797b5a2d2ecaaccb77de92185832a
3
+ metadata.gz: d7e309ca1f76d89a85e0d84b66fd54b629e7060ffd089a6a4383099230ccad9f
4
+ data.tar.gz: d387c7c45a91cf7500c83188688024fd8c08c97c4298fcc015d1d7b12adc425b
5
5
  SHA512:
6
- metadata.gz: cd86fc705158b786bb847522d25bcccd881c55758a5f0857d8696fa6cee60972c1ca8ff0bf2bced4ad6444c5891a32acc19ed586d738eaea0b8c3cd34dac2218
7
- data.tar.gz: 7c48ed00f79b044873fafbef2e9ced4828caa50d0804d2720d220c49052fe3db67a22230312db1de91cc4ddaad2d21f5cdda04dfaf6efcf13bcbca176184c737
6
+ metadata.gz: 69dae55f084fc5d4206164d47e312d5bcbf530397190563126097fc3e0250760537ae1930b68228e18f7490e929e374af67663bb3ee8b6da99460dc718951bc6
7
+ data.tar.gz: 4c53b1c6a422c78f70885ac5db3a4951b8323b8ba9707771915cd4cbe933274d2697c467c2b75586ca9c77e2e5fd299cf8bdfdc5b011a39dc6efa2ab6288bb82
data/.rubocop.yml CHANGED
@@ -17,6 +17,7 @@ Layout/LineLength:
17
17
  Max: 90
18
18
 
19
19
  AllCops:
20
+ TargetRubyVersion: 3.2.2
20
21
  DisplayCopNames: true
21
22
  Exclude:
22
23
  - 'spec/dummy/config/**/*'
data/.yardoc/checksums CHANGED
@@ -3,12 +3,12 @@ lib/koyo/repl/log.rb 811d21aa6f8843589b66da79a96a4e8b755fcc2d
3
3
  lib/koyo/repl/mod.rb 92b54b56643b06fdc74adc65dc04460f631d4036
4
4
  lib/koyo/repl/data.rb cb847585c0f439b9f645b5ebd7fa3b96e08b94ff
5
5
  lib/koyo/repl/install.rb 9b19b7bb74d084882a1f726693bac2d693015daa
6
- lib/koyo/repl/railtie.rb acb36cbbe1b386ece7567990945adecb6b6e5b80
7
- lib/koyo/repl/version.rb 94239a7a9719294e13a38b878da1d328b44fb3f5
8
- lib/koyo/repl/data_row.rb 321caf0b080621f2d319fa152948cbe864c77294
9
- lib/koyo/repl/database.rb 39a304909ccf68d69d36c84e3036debd9f29f6ec
10
- lib/koyo/repl/diagnostics.rb 03b91c5ea3dbe1e337f4427d8f3261ebedb45a70
11
- lib/koyo/repl/configuration.rb 17858828602d696a584a9e3c239c0a5d94faa738
12
- lib/koyo/repl/postgres_server.rb 2569fa642e55f6032e6ea73e9ab8762018d3cd6c
6
+ lib/koyo/repl/railtie.rb 9c560c38f4663edda57aa39c9bb4bd9411daabbd
7
+ lib/koyo/repl/version.rb d0d535b2aac27e3caa5eaeef69e117772e334f44
8
+ lib/koyo/repl/data_row.rb 46bd0b440721f3b0ddcded3fef1817ca5d77cb35
9
+ lib/koyo/repl/database.rb 719f2723b83a927e0524f6dea79393caaeba0514
10
+ lib/koyo/repl/diagnostics.rb 93226c47642c5e20f615425aff8a0c0ba988b07b
11
+ lib/koyo/repl/configuration.rb 8a32f819abf035340a3997d989ccafe0751d209e
12
+ lib/koyo/repl/postgres_server.rb 3d564dff9e7820962da02f8a383a607fb72110dc
13
13
  lib/koyo_postgres_replication.rb e3f4d905e3045b59f1f7c799d84cd9fde70eebd8
14
14
  lib/koyo/repl/event_handler_service.rb 99b67bdcf3e0e92a04cd01bf8d05a4569b45d218
data/.yardoc/object_types CHANGED
Binary file
Binary file
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --protected
2
+ --no-private
3
+ --embed-mixin ClassMethods
4
+ -
5
+ README.md
6
+ CHANGELOG.md
7
+ MIT-LICENSE
data/Gemfile CHANGED
@@ -6,6 +6,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
6
6
  # Specify your gem's dependencies in koyo_repl.gemspec
7
7
  gemspec
8
8
 
9
+ gem 'composite_primary_keys', '~> 14.0'
9
10
  gem 'pg', '~> 1.1'
10
11
  gem 'rails', '~> 7.0'
11
12
  gem 'rake', '~> 13.0'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- koyo-postgres-replication (0.1.1.pre)
4
+ koyo-postgres-replication (0.1.4.pre)
5
5
  pg (~> 1.1)
6
6
  rack (~> 2.0, >= 2.0.0)
7
7
  rails (~> 7.0)
@@ -78,6 +78,8 @@ GEM
78
78
  ast (2.4.2)
79
79
  base64 (0.1.1)
80
80
  builder (3.2.4)
81
+ composite_primary_keys (14.0.6)
82
+ activerecord (~> 7.0.2)
81
83
  concurrent-ruby (1.2.2)
82
84
  crass (1.0.6)
83
85
  date (3.3.3)
@@ -215,6 +217,7 @@ PLATFORMS
215
217
  arm64-darwin-22
216
218
 
217
219
  DEPENDENCIES
220
+ composite_primary_keys (~> 14.0)
218
221
  debug
219
222
  factory_bot_rails
220
223
  ffaker
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Koyo::Postgres::Replication
2
2
 
3
3
  例 rei - Japanese for example
4
+
4
5
  効用 koyo - Japanese for utility
5
6
 
6
7
  ## Replcation slots
@@ -36,9 +37,7 @@ wiki](https://github.com/wiseleyb/koyo-postgres-replication/wiki/Configuring-Pos
36
37
  Add to Gemfile:
37
38
 
38
39
  ```
39
- gem 'koyo-postgres-replication',
40
- git: 'https://github.com/wiseleyb/koyo-postgres-replication',
41
- require: 'koyo'
40
+ gem 'koyo-postgres-replication', require: 'koyo'
42
41
  ```
43
42
 
44
43
  Then:
@@ -172,6 +171,9 @@ for sql examples on how to interact with replication slots.
172
171
 
173
172
  ## Yard Doc
174
173
 
174
+ Yard docs are up on
175
+ [RubyDoc.info](https://rubydoc.info/github/wiseleyb/koyo-postgres-replication/main)
176
+
175
177
  Cheat sheets:
176
178
  * https://gist.github.com/chetan/1827484
177
179
  * https://kapeli.com/cheat_sheets/Yard.docset/Contents/Resources/Documents/index
data/changelog.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # Koyo Postgres Replication Changelog
2
2
 
3
+ ## 0.1.4.pre
4
+
5
+ - Add link to
6
+ [doc](https://rubydoc.info/github/wiseleyb/koyo-postgres-replication/main) in
7
+ README
8
+ - Fix issues around composite keys (multiple primary keys) [Issues
9
+ 4](https://github.com/wiseleyb/koyo-postgres-replication/issues/4)
10
+
11
+ ## 0.1.3.pre
12
+
13
+ - republishing yanked gem
14
+
15
+ ## 0.1.2.pre
16
+
17
+ - bump TargetRubyVersion for Rubocop to 3.2.2
18
+ - fix [Issue
19
+ 2](https://github.com/wiseleyb/koyo-postgres-replication/issues/2) - gemspec
20
+ config around Yard doc
21
+ - Enter retry loop instead of crashing when db connection goes away
22
+
23
+ ## 0.1.1.pre
24
+
25
+ - remove deprecated has_rdoc from gem spec
26
+ - fix [Issue 1](https://github.com/wiseleyb/koyo-postgres-replication/issues/1)
27
+ - remove recursive run, replace with simple loop
28
+
3
29
  ## 0.1.0.pre
4
30
 
5
31
  - rails 7 plugin
@@ -12,7 +38,3 @@
12
38
  - utility class for working with replication slots
13
39
  - functional - but not tested in real life yet
14
40
 
15
- ## 0.1.1.pre
16
-
17
- - remove deprecated has_rdoc from gem spec
18
- - fix [Issue 1](https://github.com/wiseleyb/koyo-postgres-replication/issues/1) - remove recursive run, replace with simple loop
@@ -17,10 +17,13 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.required_ruby_version = '>= 2.7.0'
19
19
 
20
- # spec.metadata["allowed_push_host"] = ''
21
- spec.metadata['changelog_uri'] = "#{url}/changelog.md"
22
- spec.metadata['homepage_uri'] = spec.homepage
23
- spec.metadata['source_code_uri'] = burl
20
+ spec.metadata = {
21
+ 'bug_tracker_uri' => "#{url}/issues",
22
+ 'changelog_uri' => "#{url}/blob/main/changelog.md",
23
+ 'documentation' => 'https://www.rubydoc.info/github/wiseleyb/koyo-postgres-replication/main',
24
+ 'homepage_uri' => spec.homepage,
25
+ 'source_code_uri' => burl
26
+ }
24
27
 
25
28
  spec.files = Dir.chdir(__dir__) do
26
29
  `git ls-files -z`.split("\x0").reject do |f|
@@ -11,8 +11,10 @@ module Koyo
11
11
  :kind, # insert/update/delete
12
12
  :schema, # always public for this - not needed
13
13
  :table, # table being changed
14
- :id, # table.id
15
- :id_type, # integer/uuid
14
+ :id, # table.id, null for composite keys
15
+ :id_type, # integer/uuid, null for composite keys
16
+ :ids, # for composite keys - array of ids
17
+ :id_types, # for composite keys - array of types
16
18
  :columns, # all columns from table - array
17
19
  :column_types, # all types of columns - array
18
20
  :values # all values from table - array
@@ -34,12 +36,12 @@ module Koyo
34
36
  # WARN: this breaks for multiple primary keys
35
37
  def check_set_primary_keys
36
38
  if @row['oldkeys']
37
- if @row['oldkeys']['keynames'].size > 1
38
- raise "This doesn't support multiple keys right now"
39
+ @ids = Array.new(@row['oldkeys']['keyvalues'])
40
+ @id_types = Array.new(@row['oldkeys']['keytypes'])
41
+ if @row['oldkeys']['keynames'].size == 1
42
+ @id = @ids.first
43
+ @id_type = @id_types.first
39
44
  end
40
-
41
- @id = @row['oldkeys']['keyvalues'].first
42
- @id_type = @row['oldkeys']['keytypes'].first
43
45
  else
44
46
  @id = val(:id)
45
47
  @id_type = type(:id)
@@ -49,13 +51,15 @@ module Koyo
49
51
  # Gets a value for a name from columnsvalues
50
52
  # @param name column name
51
53
  def val(name)
52
- values[columns.index(name.to_s)]
54
+ idx = columns.index(name.to_s)
55
+ idx ? values[idx] : nil
53
56
  end
54
57
 
55
58
  # Get a val type from columntypes
56
59
  # @param name column name
57
60
  def type(name)
58
- column_types[columns.index(name.to_s)]
61
+ idx = columns.index(name.to_s)
62
+ idx ? column_types[idx] : nil
59
63
  end
60
64
  end
61
65
  end
@@ -19,6 +19,7 @@ module Koyo
19
19
  conn_name = Koyo::Repl.config.database_name
20
20
 
21
21
  unless conn_name
22
+ ActiveRecord::Base.establish_connection
22
23
  @conn = ActiveRecord::Base.connection
23
24
  return @conn
24
25
  end
@@ -35,6 +36,14 @@ module Koyo
35
36
  @conn
36
37
  end
37
38
 
39
+ # Attempts to re-establish db connection
40
+ # This would be used when something happens like the postgres
41
+ # server restarts
42
+ def re_establish_conn
43
+ @conn = nil
44
+ conn
45
+ end
46
+
38
47
  # Reads from the replication slot.
39
48
  # Reading from this marks the rows read (so you won't see them again)
40
49
  # For testing you can use `peek_slot` if you want to - which will keep
@@ -51,24 +51,69 @@ module Koyo
51
51
  tick_tock
52
52
  # Possibly fatal errors
53
53
  rescue ActiveRecord::StatementInvalid => e
54
- if e.cause.exception.is_a?(PG::ConnectionBad)
55
- Koyo::Repl::EventHandlerService.koyo_error(e)
56
- msg = "SHUTTING DOWN. Fatal Error in ReplPostgresServer: #{e.message}"
57
- log_repl_fatal(msg, err: e)
58
- break
59
- else
60
- log_recoverable_error(e)
61
- end
62
- # recoverable error
54
+ Koyo::Repl::EventHandlerService.koyo_error(e)
55
+ @retry = 0
56
+ break unless attempt_reconnect(e)
63
57
  rescue StandardError => e
64
58
  log_recoverable_error(e)
65
59
  end
66
60
 
67
- success = system("sleep #{Koyo::Repl.config.sql_delay}")
68
- break unless success
61
+ break unless sleep_success?
69
62
  end
70
63
  end
71
64
 
65
+ # Does a single check of the replication slot
66
+ # If test_mode=true uses peek, which will
67
+ # leave data in the replication slot (for testing/debugging)
68
+ def check
69
+ read_sql_results.each do |sql_res|
70
+ rows = Koyo::Repl::Data.new(sql_res).rows # returns ReplDataRow
71
+ rows.each do |row|
72
+ check_row(row)
73
+ end
74
+ end
75
+ end
76
+
77
+ # Attempts to re-establish DB connection. If it finds any other
78
+ # error this is fatal and server crashes at this point
79
+ # @param [StandardError] err Error that kicked off this retry loop
80
+ def attempt_reconnect(err)
81
+ @retry ||= 0
82
+ msg = "Error: Attempting to reconnect to DB. Retry: #{@retry}."
83
+ log_recoverable_error(err)
84
+
85
+ return false unless sleep_success?
86
+
87
+ Koyo::Repl::Database.re_establish_conn
88
+ msg = 'Re-established DB connection'
89
+ log_repl_info(msg)
90
+ true
91
+ rescue StandardError => e
92
+ unless bad_connection_error?(e)
93
+ msg = 'Fatal: Found erorr unrelated to PG::ConnectionBad while '\
94
+ "trying to reconnect: Error: #{e.message}"
95
+ log_repl_fatal(msg, err: e)
96
+ return false
97
+ end
98
+
99
+ @retry += 1
100
+ retry
101
+ end
102
+
103
+ # Test error to see if it's db:connection related
104
+ # Mostly here for specs
105
+ # @param [StandardError] err Error to check
106
+ def bad_connection_error?(err)
107
+ err.cause.exception.is_a?(PG::ConnectionBad)
108
+ end
109
+
110
+ # Did sleep succeed... this fails when you do something like hit
111
+ # ctrl-c on the server
112
+ # Mostly here for specs
113
+ def sleep_success?
114
+ system("sleep #{Koyo::Repl.config.sql_delay}")
115
+ end
116
+
72
117
  # Handles erros that aren't fatal. Calls back to
73
118
  # Koyo::Repl::Log@log_repl_error which calls back
74
119
  # to KoyoReplHandlerServer@log_repl_error
@@ -88,18 +133,6 @@ module Koyo
88
133
  @tick_tock = 0
89
134
  end
90
135
 
91
- # Does a single check of the replication slot
92
- # If test_mode=true uses peek, which will
93
- # leave data in the replication slot (for testing/debugging)
94
- def check
95
- read_sql_results.each do |sql_res|
96
- rows = Koyo::Repl::Data.new(sql_res).rows # returns ReplDataRow
97
- rows.each do |row|
98
- check_row(row)
99
- end
100
- end
101
- end
102
-
103
136
  # Reads data from the replication slot
104
137
  # Handles test_mode (so will only peek if true)
105
138
  def read_sql_results
@@ -31,6 +31,11 @@ module Koyo
31
31
  puts 'Running Koyo::Repl::PostgresServer.run!'
32
32
  Koyo::Repl::PostgresServer.run!
33
33
  end
34
+
35
+ desc 'Recreate replication slot'
36
+ task recreate: :environment do
37
+ Koyo::Repl::Database.drop_create_slot!
38
+ end
34
39
  end
35
40
  end
36
41
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Koyo
4
4
  module Repl
5
- VERSION = '0.1.1.pre'
5
+ VERSION = '0.1.4.pre'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koyo-postgres-replication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.pre
4
+ version: 0.1.4.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Wiseley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-18 00:00:00.000000000 Z
11
+ date: 2023-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -115,6 +115,7 @@ files:
115
115
  - ".yardoc/object_types"
116
116
  - ".yardoc/objects/root.dat"
117
117
  - ".yardoc/proxy_types"
118
+ - ".yardopts"
118
119
  - Gemfile
119
120
  - Gemfile.lock
120
121
  - MIT-LICENSE
@@ -143,7 +144,9 @@ homepage: https://github.com/wiseleyb
143
144
  licenses:
144
145
  - MIT
145
146
  metadata:
146
- changelog_uri: https://github.com/wiseleyb/koyo-postgres-replication/changelog.md
147
+ bug_tracker_uri: https://github.com/wiseleyb/koyo-postgres-replication/issues
148
+ changelog_uri: https://github.com/wiseleyb/koyo-postgres-replication/blob/main/changelog.md
149
+ documentation: https://www.rubydoc.info/github/wiseleyb/koyo-postgres-replication/main
147
150
  homepage_uri: https://github.com/wiseleyb
148
151
  source_code_uri: https://github.com/wiseleyb
149
152
  post_install_message: