wal 0.0.6 → 0.0.8
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 +4 -4
- data/exe/wal +1 -1
- data/lib/wal/generators/migration.rb +62 -0
- data/lib/wal/generators/templates/migration.rb.erb +9 -0
- data/lib/wal/migration.rb +47 -0
- data/lib/wal/railtie.rb +17 -0
- data/lib/wal/record_watcher.rb +8 -0
- data/lib/wal/version.rb +1 -1
- data/lib/wal.rb +1 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 267c5bf23d2617d97504613afc0e4fd4ca533a2ac8fd0852fbe38a4e6671abe4
|
4
|
+
data.tar.gz: ebccdc39d41b731395678882e45c63afff1d72b5e607f91e142d871890776d7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f050e89a405581fd20ee704ba89c44a99920baf3d3e2703e678a319c8845c06803b695b2a69a457b05ef8e60106de7e57c253ff43546ad7f01dde2c20e70d9e3
|
7
|
+
data.tar.gz: dce3f46c0bb2688532a46bdc364be1306ef91eb2b1267bd0e37b74a68f9f85d8dd8fe436ffb5b1f44b119da3b7e9605c96faf4fd7109bd30ac09102a8de3415c
|
data/exe/wal
CHANGED
@@ -47,7 +47,7 @@ elsif cli["start"]
|
|
47
47
|
watcher = config["watcher"].constantize.new
|
48
48
|
temporary = config["temporary"] || false
|
49
49
|
publications = config["publications"] || []
|
50
|
-
replicator = config["
|
50
|
+
replicator = config["replicator"].presence&.constantize || Wal::Replicator
|
51
51
|
|
52
52
|
Thread.new(slot, watcher, temporary, publications) do |replication_slot, watcher, use_temporary_slot, publications|
|
53
53
|
replication_slot = "#{replication_slot}_#{SecureRandom.alphanumeric(4)}" if use_temporary_slot
|
@@ -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
|
data/lib/wal/railtie.rb
ADDED
@@ -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
|
data/lib/wal/record_watcher.rb
CHANGED
@@ -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
|
data/lib/wal/version.rb
CHANGED
data/lib/wal.rb
CHANGED
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.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Navarro
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-08-
|
10
|
+
date: 2025-08-22 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
|