real_data_tests 0.3.10 → 0.3.12

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: f76c93165cf56a1bb2e90311afc6bf92f42f17553271a977dec2e17f39afc67f
4
- data.tar.gz: f1caa35f8eaa904dba0b608484d43d8beffaa7208646e067f046b8add97db10d
3
+ metadata.gz: e9613e7cdfc987dd88b0726c3cfe9f9bc31cf4877267988c70fe2c52bf96b163
4
+ data.tar.gz: 654f32e804769aca3cc0415b0455d0d4f481b6fc8e01625a3bd52fc43e11ee6d
5
5
  SHA512:
6
- metadata.gz: 1097a749d0dd8ad287130dbab5a75c0f526de6689e9877d4f218657d6c03ad49ccd8c0c3cd02697306d1fee41c11c0915f11e4c264e2254cb21013e266d130aa
7
- data.tar.gz: c0b6f6c9f84558801e5140eea8da6c5f068ad5adbe4dd3ba520e87fe2ef2259921374d52713e8e3a52775b64bb439f426be72a698d83e5a0144b16a6282cfc14
6
+ metadata.gz: 80f42c596ef4fef11a44272c8a94089700428cc414030024410531e74ee6709d55fdff7b0de3b47a733b73aaf5e576cf519c746f9d4af60bfad978feefa5235f
7
+ data.tar.gz: 0656dede260673942b237c12e94566156fdbed104b864c23b2617c69871088af97afaf6d21fcce6772e3e4a25dcc80e24450f189600d863b6b7a4992c1e9d491
@@ -17,7 +17,7 @@ module RealDataTests
17
17
  end
18
18
  end
19
19
 
20
- # New method that doesn't rely on system commands
20
+ # Native Ruby implementation
21
21
  def load_real_test_data_native(name)
22
22
  dump_path = File.join(RealDataTests.configuration.dump_path, "#{name}.sql")
23
23
  raise Error, "Test data file not found: #{dump_path}" unless File.exist?(dump_path)
@@ -75,32 +75,41 @@ module RealDataTests
75
75
  statements = []
76
76
  current_statement = ''
77
77
  in_string = false
78
- escaped = false
78
+ in_conflict = false
79
+ quote_char = nil
79
80
 
80
81
  sql.each_char do |char|
81
- case char
82
- when '\\'
83
- escaped = !escaped
84
- when "'"
85
- in_string = !in_string unless escaped
86
- escaped = false
87
- when ';'
88
- if !in_string
89
- statements << current_statement.strip
90
- current_statement = ''
91
- else
92
- current_statement << char
93
- end
94
- else
95
- escaped = false
96
- current_statement << char
82
+ # Detect start of quoted strings
83
+ if (char == "'" || char == '"') && !in_string
84
+ in_string = true
85
+ quote_char = char
86
+ # Detect end of quoted strings (matching quote type)
87
+ elsif (char == "'" || char == '"') && in_string && char == quote_char
88
+ in_string = false
89
+ # Track if we're in an ON CONFLICT clause
90
+ elsif char == 'O' && !in_string && current_statement.strip.end_with?(')')
91
+ # Look ahead to see if this is 'ON CONFLICT'
92
+ potential_conflict = sql[sql.index(char, sql.rindex(current_statement)), 11]
93
+ in_conflict = potential_conflict&.upcase == 'ON CONFLICT'
94
+ end
95
+
96
+ current_statement << char
97
+
98
+ # Only split on semicolon if we're not in a string and not in an ON CONFLICT clause
99
+ if char == ';' && !in_string && !in_conflict
100
+ statements << current_statement.strip
101
+ current_statement = ''
102
+ in_conflict = false
97
103
  end
98
104
  end
99
105
 
100
- # Add the last statement if it doesn't end with a semicolon
106
+ # Add any remaining statement
101
107
  statements << current_statement.strip if current_statement.strip.length > 0
102
108
 
103
- statements
109
+ statements.map do |stmt|
110
+ # Ensure each statement ends with a semicolon
111
+ stmt.end_with?(';') ? stmt : "#{stmt};"
112
+ end
104
113
  end
105
114
 
106
115
  def extract_conflict_clause(statement)
@@ -202,38 +211,42 @@ module RealDataTests
202
211
  "'#{value}'" # Other strings
203
212
  end
204
213
  end
214
+ end
205
215
 
206
- def clean_values(values_str)
207
- values = []
208
- current_value = ''
209
- in_quotes = false
210
- nested_level = 0
216
+ def import
217
+ @logger.info "Starting SQL import..."
211
218
 
212
- values_str.chars.each do |char|
213
- case char
214
- when "'"
215
- in_quotes = !in_quotes
216
- current_value << char
217
- when '{'
218
- nested_level += 1
219
- current_value << char
220
- when '}'
221
- nested_level -= 1
222
- current_value << char
223
- when ','
224
- if !in_quotes && nested_level == 0
225
- values << clean_value(current_value.strip)
226
- current_value = ''
227
- else
228
- current_value << char
219
+ ActiveRecord::Base.transaction do
220
+ begin
221
+ # Disable foreign key checks and triggers temporarily
222
+ ActiveRecord::Base.connection.execute('SET session_replication_role = replica;')
223
+
224
+ # Split the SQL content into individual statements
225
+ statements = split_sql_statements(@sql_content)
226
+
227
+ statements.each_with_index do |statement, index|
228
+ next if statement.strip.empty?
229
+
230
+ begin
231
+ @logger.info "Executing statement #{index + 1} of #{statements.length}"
232
+ cleaned_statement = clean_sql_statement(statement)
233
+ ActiveRecord::Base.connection.execute(cleaned_statement)
234
+ rescue ActiveRecord::StatementInvalid => e
235
+ @logger.error "Error executing statement #{index + 1}: #{e.message}"
236
+ @logger.error "Statement: #{cleaned_statement[0..100]}..."
237
+ raise
229
238
  end
230
- else
231
- current_value << char
232
239
  end
233
- end
234
240
 
235
- values << clean_value(current_value.strip)
236
- values.join(', ')
241
+ @logger.info "Successfully imported all SQL statements"
242
+ rescue StandardError => e
243
+ @logger.error "Error during import: #{e.message}"
244
+ @logger.error e.backtrace.join("\n")
245
+ raise
246
+ ensure
247
+ # Re-enable foreign key checks and triggers
248
+ ActiveRecord::Base.connection.execute('SET session_replication_role = DEFAULT;')
249
+ end
237
250
  end
238
251
  end
239
252
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RealDataTests
4
- VERSION = "0.3.10"
4
+ VERSION = "0.3.12"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: real_data_tests
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.10
4
+ version: 0.3.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Dias