pg_logical_replicator 0.1.5 → 0.1.7

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: 2aab5bea887e74a1be5a02da58eb98b525c7ce3c786c7943136332f1b953c203
4
- data.tar.gz: 201bed281909128a3bf8569360eb30f2d8d85601d9696207fd31919df5a45155
3
+ metadata.gz: 582bdae775918a4f57393f3ba52de1186a898a734b66499047c4d31600838e73
4
+ data.tar.gz: 378623f6b3dd59cd8c278df4c239a3cb6fffe273bd94a5988728addfb112b172
5
5
  SHA512:
6
- metadata.gz: 0d41c1e2e0e75e51ed0dccaeffd47efdb6fd7900c720ab0203e9ac7a2c0247b3d73071b3ae91fff3d29b4b9fd3b32b1598cf7be3dfa3c59248ee75c4e685f4a7
7
- data.tar.gz: 9bfa6c1923a933b4ac6d5c8c094da0de2a2230cea61b4d1957f38ecfc9ea8916afe1a4619e32f8abe4711ecd165dfdc833007546644397440a0656f04c1aba36
6
+ metadata.gz: 7800390f37a520191ae6bb393bc6d518aea61bc99affaa1ba308eee51f2c0e901e56ec72162e5ab9682a5ec3418038f1654ffd556a85eac588177b4411886b27
7
+ data.tar.gz: ff0fa62c9845edb78c5f3326c6e714b8c0c40dc846725cf1ea4b39ef31db0f775ca97873877f5f83a647b66153142ba3533f2beb7700229df3dadf52baac9596
@@ -19,7 +19,7 @@ module PgLogicalReplicator
19
19
  method_option :target_rep_password, type: :string
20
20
  method_option :num_slots, type: :numeric, default: 10
21
21
  method_option :groups, type: :array
22
-
22
+
23
23
  def setup
24
24
  source_host = options[:source_host]
25
25
  source_port = options[:source_port]
@@ -35,11 +35,11 @@ module PgLogicalReplicator
35
35
  target_rep_password = options[:target_rep_password] || target_password
36
36
  num_slots = options[:num_slots]
37
37
  target_groups = options[:groups].map(&:to_i) if options[:groups]
38
-
38
+
39
39
  conn = PG.connect(dbname: source_database, user: source_username, password: source_password, host: source_host, port: source_port)
40
-
40
+
41
41
  query = "SELECT tablename FROM pg_tables WHERE schemaname = 'public' order by tablename ASC;"
42
-
42
+
43
43
  tables = begin
44
44
  result = conn.exec(query)
45
45
  result.map { |row| row['tablename'] }
@@ -47,45 +47,52 @@ module PgLogicalReplicator
47
47
  conn.close if conn
48
48
  end
49
49
  puts "Total Tables: #{tables.size}"
50
-
50
+
51
51
  tables_per_group = tables.size / num_slots
52
52
  puts "Tables per group: #{tables_per_group}"
53
-
53
+
54
54
  dump_dir_root = "~/db-dumps-#{Time.now.strftime('%Y%m%d%H%M%S')}"
55
55
  system("mkdir -p #{dump_dir_root}")
56
-
56
+
57
57
  puts "Target Groups: #{target_groups}"
58
-
58
+
59
59
  tables.each_slice(tables_per_group).with_index do |table_group, group_idx|
60
60
  puts "Processing group #{group_idx + 1} size: #{table_group.size}"
61
-
61
+
62
62
  group_number = group_idx + 1
63
-
63
+
64
64
  next unless target_groups.nil? || target_groups.include?(group_number)
65
-
65
+
66
66
  table_names = table_group.join(',')
67
-
67
+
68
68
  dump_dir = "#{dump_dir_root}/#{group_number}"
69
-
69
+
70
70
  puts "Removing directory: #{dump_dir}"
71
-
71
+
72
72
  FileUtils.rm_rf(dump_dir)
73
-
74
- puts "Creating directory: #{dump_dir}"
75
- FileUtils.mkdir_p(dump_dir)
76
-
77
- LogicalReplicationInitializer.new({
78
- slot_name: "logical_sub_grp_#{group_number}",
79
- publication_name: "logical_pub_grp_#{group_number}",
80
- primary_conn_str: "host=#{source_host} port=#{source_port} dbname=#{source_database} user=#{source_username} password=#{source_password}",
81
- target_conn_str: "host=#{target_host} port=#{target_port} dbname=#{target_database} user=#{target_username} password=#{target_password}",
82
- target_rep_conn_str: "host=#{target_host} port=#{target_port} dbname=#{target_database} user=#{target_rep_username} password=#{target_rep_password}",
83
- table_names: table_names,
84
- dump_dir: dump_dir
85
- }).start
73
+
74
+ begin
75
+ puts "Creating directory: #{dump_dir}"
76
+ FileUtils.mkdir_p(dump_dir)
77
+
78
+ LogicalReplicationInitializer.new({
79
+ slot_name: "logical_sub_grp_#{group_number}",
80
+ publication_name: "logical_pub_grp_#{group_number}",
81
+ primary_conn_str: "host=#{source_host} port=#{source_port} dbname=#{source_database} user=#{source_username} password=#{source_password}",
82
+ target_conn_str: "host=#{target_host} port=#{target_port} dbname=#{target_database} user=#{target_username} password=#{target_password}",
83
+ target_rep_conn_str: "host=#{target_host} port=#{target_port} dbname=#{target_database} user=#{target_rep_username} password=#{target_rep_password}",
84
+ table_names: table_names,
85
+ dump_dir: dump_dir
86
+ }).start
87
+ ensure
88
+ puts "Removing directory: #{dump_dir}"
89
+ FileUtils.rm_rf(dump_dir)
90
+ end
91
+ rescue StandardError => e
92
+ puts "Error: #{e.message}"
86
93
  end
87
94
  end
88
-
95
+
89
96
  desc 'stop_replication', 'Stop all replication'
90
97
  method_option :source_host, type: :string, required: true
91
98
  method_option :source_port, type: :numeric, default: 5432
@@ -97,7 +104,7 @@ module PgLogicalReplicator
97
104
  method_option :source_password, type: :string, required: true
98
105
  method_option :target_username, type: :string
99
106
  method_option :target_password, type: :string
100
-
107
+
101
108
  def stop_replication
102
109
  source_config = {
103
110
  host: options[:source_host],
@@ -106,7 +113,7 @@ module PgLogicalReplicator
106
113
  user: options[:source_username],
107
114
  password: options[:source_password]
108
115
  }
109
-
116
+
110
117
  target_config = {
111
118
  host: options[:target_host],
112
119
  port: options[:target_port],
@@ -114,10 +121,10 @@ module PgLogicalReplicator
114
121
  user: options[:target_username] || options[:source_username],
115
122
  password: options[:target_password] || options[:source_password]
116
123
  }
117
-
124
+
118
125
  ReplicationStopper.new(source_config, target_config).stop_replication
119
126
  end
120
-
127
+
121
128
  desc 'transfer_schema', 'Transfer schema from source to target database'
122
129
  method_option :source_host, type: :string, required: true
123
130
  method_option :source_port, type: :numeric, default: 5432
@@ -129,7 +136,7 @@ module PgLogicalReplicator
129
136
  method_option :source_password, type: :string, required: true
130
137
  method_option :target_username, type: :string, required: true
131
138
  method_option :target_password, type: :string, required: true
132
-
139
+
133
140
  def transfer_schema
134
141
  SchemaTransfer.new(
135
142
  source_host: options[:source_host],
@@ -1,3 +1,3 @@
1
1
  module PgLogicalReplicator
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_logical_replicator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - eni9889
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  requirements: []
121
- rubygems_version: 3.0.3.1
121
+ rubygems_version: 3.4.10
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: PostgreSQL logical replication setup tool