|
109 | 109 | s_config = Multiwoven::Integrations::Protocol::SyncConfig.from_json(sync_config.to_json) |
110 | 110 | s_config.sync_run_id = "33" |
111 | 111 | allow(PG).to receive(:connect).and_return(pg_connection) |
| 112 | + allow(pg_connection).to receive(:close) |
| 113 | + allow(pg_connection).to receive(:escape_string) { |str| str } |
112 | 114 |
|
113 | 115 | allow(pg_connection).to receive(:exec).and_return(true) |
114 | 116 |
|
|
126 | 128 | s_config = Multiwoven::Integrations::Protocol::SyncConfig.from_json(sync_config.to_json) |
127 | 129 | s_config.sync_run_id = "33" |
128 | 130 | allow(PG).to receive(:connect).and_return(pg_connection) |
| 131 | + allow(pg_connection).to receive(:close) |
| 132 | + allow(pg_connection).to receive(:escape_string) { |str| str } |
129 | 133 |
|
130 | 134 | allow(pg_connection).to receive(:exec).and_return(true) |
131 | 135 |
|
|
147 | 151 | s_config.sync_run_id = "34" |
148 | 152 |
|
149 | 153 | allow(PG).to receive(:connect).and_return(pg_connection) |
| 154 | + allow(pg_connection).to receive(:close) |
| 155 | + allow(pg_connection).to receive(:escape_string) { |str| str } |
150 | 156 |
|
151 | 157 | allow(pg_connection).to receive(:exec).and_raise(StandardError.new("test error")) |
152 | 158 |
|
|
162 | 168 | end |
163 | 169 | end |
164 | 170 |
|
| 171 | + # bulk write specs |
| 172 | + |
| 173 | + describe "#write (batch)" do |
| 174 | + before do |
| 175 | + allow(PG).to receive(:connect).and_return(pg_connection) |
| 176 | + allow(pg_connection).to receive(:close) |
| 177 | + allow(pg_connection).to receive(:escape_string) { |str| str } |
| 178 | + end |
| 179 | + |
| 180 | + let(:s_config) do |
| 181 | + config = Multiwoven::Integrations::Protocol::SyncConfig.from_json(sync_config.to_json) |
| 182 | + config.sync_run_id = "50" |
| 183 | + config |
| 184 | + end |
| 185 | + |
| 186 | + let(:batch_records) do |
| 187 | + records.map { |r| r.data.transform_keys(&:to_s) } |
| 188 | + end |
| 189 | + |
| 190 | + context "bulk insert" do |
| 191 | + it "inserts multiple records in a single statement" do |
| 192 | + expect(pg_connection).to receive(:exec).with( |
| 193 | + a_string_matching(/INSERT INTO.*VALUES.*,.*/) |
| 194 | + ).once.and_return(true) |
| 195 | + |
| 196 | + tracking = subject.write(s_config, batch_records).tracking |
| 197 | + expect(tracking.success).to eql(2) |
| 198 | + expect(tracking.failed).to eql(0) |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + context "bulk upsert" do |
| 203 | + it "generates ON CONFLICT clause for destination_update" do |
| 204 | + expect(pg_connection).to receive(:exec).with( |
| 205 | + a_string_matching(/ON CONFLICT.*DO UPDATE SET/) |
| 206 | + ).once.and_return(true) |
| 207 | + |
| 208 | + tracking = subject.write(s_config, batch_records, "destination_update").tracking |
| 209 | + expect(tracking.success).to eql(2) |
| 210 | + expect(tracking.failed).to eql(0) |
| 211 | + end |
| 212 | + |
| 213 | + it "generates ON CONFLICT DO NOTHING when update_cols is empty (only primary key)" do |
| 214 | + records_only_pk = [ |
| 215 | + { "id" => "1" }, |
| 216 | + { "id" => "2" } |
| 217 | + ] |
| 218 | + expect(pg_connection).to receive(:exec).with( |
| 219 | + a_string_matching(/ON CONFLICT.*DO NOTHING/) |
| 220 | + ).once.and_return(true) |
| 221 | + |
| 222 | + tracking = subject.write(s_config, records_only_pk, "destination_update").tracking |
| 223 | + expect(tracking.success).to eql(2) |
| 224 | + expect(tracking.failed).to eql(0) |
| 225 | + end |
| 226 | + end |
| 227 | + |
| 228 | + context "fallback on bulk failure" do |
| 229 | + it "falls back to individual writes and tracks success" do |
| 230 | + call_count = 0 |
| 231 | + allow(pg_connection).to receive(:exec) do |
| 232 | + call_count += 1 |
| 233 | + raise StandardError, "bulk failed" if call_count == 1 |
| 234 | + |
| 235 | + true |
| 236 | + end |
| 237 | + |
| 238 | + tracking = subject.write(s_config, batch_records).tracking |
| 239 | + expect(tracking.success).to eql(2) |
| 240 | + expect(tracking.failed).to eql(0) |
| 241 | + end |
| 242 | + |
| 243 | + it "tracks partial failures in individual fallback" do |
| 244 | + call_count = 0 |
| 245 | + allow(pg_connection).to receive(:exec) do |
| 246 | + call_count += 1 |
| 247 | + # bulk fails, first individual succeeds, second individual fails |
| 248 | + raise StandardError, "bulk failed" if call_count == 1 |
| 249 | + raise StandardError, "row failed" if call_count == 3 |
| 250 | + |
| 251 | + true |
| 252 | + end |
| 253 | + |
| 254 | + tracking = subject.write(s_config, batch_records).tracking |
| 255 | + expect(tracking.success).to eql(1) |
| 256 | + expect(tracking.failed).to eql(1) |
| 257 | + end |
| 258 | + |
| 259 | + it "logs info for each successful individual write in fallback" do |
| 260 | + call_count = 0 |
| 261 | + allow(pg_connection).to receive(:exec) do |
| 262 | + call_count += 1 |
| 263 | + raise StandardError, "bulk failed" if call_count == 1 |
| 264 | + |
| 265 | + true |
| 266 | + end |
| 267 | + |
| 268 | + tracking = subject.write(s_config, batch_records).tracking |
| 269 | + expect(tracking.success).to eql(2) |
| 270 | + info_logs = tracking.logs.select { |l| l.level == "info" } |
| 271 | + expect(info_logs.size).to eql(2) |
| 272 | + info_logs.each do |log| |
| 273 | + expect(log.message).to include("request") |
| 274 | + expect(log.message).to include("response") |
| 275 | + end |
| 276 | + end |
| 277 | + end |
| 278 | + |
| 279 | + context "records with inconsistent keys" do |
| 280 | + let(:mixed_records) do |
| 281 | + [ |
| 282 | + { "email" => "user1@example.com", "user_id" => "1" }, |
| 283 | + { "email" => "user2@example.com", "user_id" => "2", "location" => "NYC" } |
| 284 | + ] |
| 285 | + end |
| 286 | + |
| 287 | + it "includes all columns from the union of record keys" do |
| 288 | + expect(pg_connection).to receive(:exec).with( |
| 289 | + a_string_matching(/"email",\s*"user_id",\s*"location"/) |
| 290 | + ).once.and_return(true) |
| 291 | + |
| 292 | + tracking = subject.write(s_config, mixed_records).tracking |
| 293 | + expect(tracking.success).to eql(2) |
| 294 | + expect(tracking.failed).to eql(0) |
| 295 | + end |
| 296 | + |
| 297 | + it "uses NULL for missing keys" do |
| 298 | + expect(pg_connection).to receive(:exec).with( |
| 299 | + a_string_matching(/NULL/) |
| 300 | + ).once.and_return(true) |
| 301 | + |
| 302 | + subject.write(s_config, mixed_records) |
| 303 | + end |
| 304 | + end |
| 305 | + |
| 306 | + context "connection cleanup" do |
| 307 | + it "closes connection on success" do |
| 308 | + allow(pg_connection).to receive(:exec).and_return(true) |
| 309 | + expect(pg_connection).to receive(:close) |
| 310 | + subject.write(s_config, batch_records) |
| 311 | + end |
| 312 | + |
| 313 | + it "closes connection on failure" do |
| 314 | + allow(pg_connection).to receive(:exec).and_raise(StandardError.new("err")) |
| 315 | + expect(pg_connection).to receive(:close) |
| 316 | + subject.write(s_config, batch_records) |
| 317 | + end |
| 318 | + end |
| 319 | + end |
| 320 | + |
165 | 321 | describe "#discover" do |
166 | 322 | it "discovers schema successfully" do |
167 | 323 | allow(PG).to receive(:connect).and_return(pg_connection) |
|
0 commit comments