wal 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f69fdfa02c0ec9015408b179140ab2086041ffc4f895e2d0cfd8f1702e32ad5b
4
- data.tar.gz: a6bbc4b59d098e39f159875e782e174f4d2700b5bad3281979e1f25deb40ad12
3
+ metadata.gz: '08cf62e21e9cd21bca29345d2e666d2d390e886d4ea8c43b9926b2807d7732ac'
4
+ data.tar.gz: 6396204641b2b13f6dc64291f61072f2283a808a271fbed4f1a6b84dfe59245f
5
5
  SHA512:
6
- metadata.gz: fd490b22e2db6e1a0a6e5f2b667234c3a2f8c6fc488f54bedfcb21660f2259ffbf47498f0227da72ce94ac59ee0177b2363c3fc244a8b300fde48777ee3ad79b
7
- data.tar.gz: f7d8e93fd8a91e0e1d4d37d7fc25d5d698eadf9e145db45d4078a137e9ffc5e5ca8a9c1f3f26ee19c9e7920a863dd6482cc2322d6a81b296635ab003e8f0bb92
6
+ metadata.gz: 4aa20fe9dd40ba5c66eb8d9499be96590bd2ab51e835c49d09e91307c1b978809e266e781312e502264bd446294e273a5796ad98dd9eece2ea7cd9a98f86a6e3
7
+ data.tar.gz: ed72b21ca9e0e6d770d0cf2121f8ec9fb22aacf47b6cfd46d70946ec5200bc2d57feeb211d7edc65875171540f48f94dc376ccb0d3e4c38ffc855812eb6fb089
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module Wal
7
+ module Generators
8
+ class Migration < Rails::Generators::Base
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path("templates", __dir__)
12
+
13
+ argument :watcher, type: :string
14
+
15
+ def self.next_migration_number(dir)
16
+ ::ActiveRecord::Generators::Base.next_migration_number(dir)
17
+ end
18
+
19
+ def create_migration_file
20
+ migration_template("migration.rb.erb", "db/migrate/#{migration_class_name.underscore}.rb")
21
+ end
22
+
23
+ private
24
+
25
+ def activerecord_migration_class
26
+ if ActiveRecord::Migration.respond_to? :current_version
27
+ "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
28
+ else
29
+ "ActiveRecord::Migration"
30
+ end
31
+ end
32
+
33
+ def publication_name
34
+ "#{watcher.underscore}_publication"
35
+ end
36
+
37
+ def class_name
38
+ watcher.camelize
39
+ end
40
+
41
+ def migration_class_name
42
+ "Set#{class_name}Publication"
43
+ end
44
+
45
+ def publication_tables
46
+ if (watcher = class_name.constantize) && watcher < Wal::RecordWatcher
47
+ tables = watcher
48
+ .change_callbacks
49
+ .map { |table, configs| [table.to_s, configs.flat_map { |config| config[:changed] || [] }.uniq] }
50
+ .to_h
51
+
52
+ watcher
53
+ .delete_callbacks
54
+ .keys
55
+ .reduce(tables) { |tables, table| tables.reverse_merge(table => []) }
56
+ else
57
+ []
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ class <%= migration_class_name %> < <%= activerecord_migration_class %>
2
+ def change
3
+ define_publication :<%= publication_name %> do |p|
4
+ <% publication_tables.each do |table, columns| -%>
5
+ p.table :<%= table %><%= columns.present? ? ", columns: #{columns}" : "" %>
6
+ <% end -%>
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wal
4
+ module Migration
5
+ class PublicationDefinition
6
+ def initialize(name)
7
+ @name = name
8
+ @tables = {}
9
+ end
10
+
11
+ def table(name, columns: [])
12
+ @tables[name] ||= [].to_set
13
+ @tables[name].merge(columns.map(&:to_s))
14
+ end
15
+
16
+ def tables
17
+ @tables.keys
18
+ end
19
+
20
+ def to_sql
21
+ tables_sql = @tables.reduce([]) do |sql, (table, columns)|
22
+ table_sql = "TABLE #{table}"
23
+ table_sql << " (#{columns.join(", ")})" unless columns.empty?
24
+ sql << table_sql
25
+ end
26
+ "ALTER PUBLICATION #{@name} SET #{tables_sql.join(", ")}"
27
+ end
28
+ end
29
+
30
+ def define_publication(name)
31
+ execute <<~SQL
32
+ DO $$
33
+ BEGIN
34
+ CASE WHEN NOT EXISTS (SELECT 1 FROM pg_publication WHERE pubname = '#{name}') THEN
35
+ CREATE PUBLICATION #{name};
36
+ END CASE;
37
+ END;
38
+ $$;
39
+ SQL
40
+
41
+ definition = PublicationDefinition.new(name)
42
+ yield definition
43
+
44
+ execute definition.to_sql
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module Waldit
6
+ class Railtie < Rails::Railtie
7
+ generators do
8
+ require_relative "generators/migration"
9
+ end
10
+
11
+ config.before_configuration do
12
+ require_relative "migration"
13
+ ActiveRecord::Migration.include Wal::Migration
14
+ ActiveRecord::Schema.include Wal::Migration
15
+ end
16
+ end
17
+ end
@@ -65,6 +65,14 @@ module Wal
65
65
  @@delete_callbacks[table].push(block: block)
66
66
  end
67
67
 
68
+ def self.change_callbacks
69
+ @@change_callbacks
70
+ end
71
+
72
+ def self.delete_callbacks
73
+ @@delete_callbacks
74
+ end
75
+
68
76
  def on_record_changed(event)
69
77
  case event
70
78
  when InsertEvent
@@ -104,8 +112,8 @@ module Wal
104
112
  #
105
113
  # These strategies can be defined per transaction, and by default it will uses the memory one, and only fallback
106
114
  # to the temporary table if the transaction size is roughly 2 gigabytes or more.
107
- def aggregation_strategy(event)
108
- if event.estimated_size > 1024.pow(3) * 2
115
+ def aggregation_strategy(begin_transaction_event)
116
+ if begin_transaction_event.estimated_size > 1024.pow(3) * 2
109
117
  :temporary_table
110
118
  else
111
119
  :memory
data/lib/wal/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wal
4
- VERSION = "0.0.7"
4
+ VERSION = "0.0.9"
5
5
  end
data/lib/wal.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "wal/record_watcher"
8
8
  require_relative "wal/streaming_watcher"
9
9
  require_relative "wal/replicator"
10
10
  require_relative "wal/active_record_context_extension"
11
+ require_relative "wal/railtie"
11
12
  require_relative "wal/version"
12
13
 
13
14
  module Wal
data/rbi/wal.rbi CHANGED
@@ -7,7 +7,7 @@ module Wal
7
7
  UpdateEvent,
8
8
  DeleteEvent,
9
9
  ) }
10
- VERSION = "0.0.3"
10
+ VERSION = "0.0.9"
11
11
 
12
12
  class BeginTransactionEvent < T::Struct
13
13
  prop :transaction_id, Integer, immutable: true
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Navarro
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-08-10 00:00:00.000000000 Z
10
+ date: 2025-08-30 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: pg
@@ -93,7 +93,11 @@ files:
93
93
  - exe/wal
94
94
  - lib/wal.rb
95
95
  - lib/wal/active_record_context_extension.rb
96
+ - lib/wal/generators/migration.rb
97
+ - lib/wal/generators/templates/migration.rb.erb
98
+ - lib/wal/migration.rb
96
99
  - lib/wal/noop_watcher.rb
100
+ - lib/wal/railtie.rb
97
101
  - lib/wal/record_watcher.rb
98
102
  - lib/wal/replicator.rb
99
103
  - lib/wal/streaming_watcher.rb