fast_schema_dumper 0.2.0 → 0.4.0

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: 59d91cf2640474385f1f84f60c205fd9f42aa160e3c71fd8857d56f1ea7747d8
4
- data.tar.gz: 30c873a8eb2cd833ec53126f1a6642a1b6f5f1f2d2d3495610f460773477b596
3
+ metadata.gz: 9e74649c95b51a718a9e5d2d4583adb0eeaa752f72116e4a21235e94b7aed768
4
+ data.tar.gz: b17d033b4449cd527eed2b50ab3c5b5851eca822c9329fd8d698d98733bb50e9
5
5
  SHA512:
6
- metadata.gz: bad68066eed1d18fdb2df665d0f17dfa07654ddeb5dec4d4ad2cf4292e7c73f0303f9ea969a3410c76589625551bc29b6dfdb68fb7f06bdb63c50365ff1c55c0
7
- data.tar.gz: e9ea6b8ef54edfabb6ee4c24265a02ddc8544aefda247c3bf48e21bba8b5952b093f8cacfce31a765ba965809e1514a06233531e466950780a1c837a5f5cc7db
6
+ metadata.gz: 8a94ed1a0bf13a02910a3a7a69f154dc1d5f5926d6bdeafedd5a241855052e6d03507825ad9cf93d2788f59a2ca19e031fd81a370fca578d4b88deed5c9db4fb
7
+ data.tar.gz: 53b42423236881b8887180819cee487cad61d90433750a18f5759059dd87dc1460fc98314d512c1f48d451e3d9da284b2e5e7429410d956ccd21b68781a21582
@@ -121,6 +121,39 @@ module FastSchemaDumper
121
121
  ORDER BY kcu.TABLE_NAME, kcu.CONSTRAINT_NAME
122
122
  ")
123
123
 
124
+ # Get CHECK constraints (MySQL 8.0.16+)
125
+ result = conn.exec_query("
126
+ SELECT COUNT(*) as count
127
+ FROM INFORMATION_SCHEMA.TABLES
128
+ WHERE TABLE_SCHEMA = 'information_schema'
129
+ AND TABLE_NAME = 'CHECK_CONSTRAINTS'
130
+ ")
131
+ check_constraints_data = if result.first['count'] > 0
132
+ conn.exec_query("
133
+ SELECT
134
+ tc.CONSTRAINT_NAME,
135
+ tc.TABLE_NAME,
136
+ cc.CHECK_CLAUSE
137
+ FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
138
+ JOIN INFORMATION_SCHEMA.CHECK_CONSTRAINTS cc
139
+ ON tc.CONSTRAINT_SCHEMA = cc.CONSTRAINT_SCHEMA
140
+ AND tc.CONSTRAINT_NAME = cc.CONSTRAINT_NAME
141
+ WHERE tc.TABLE_SCHEMA = DATABASE()
142
+ AND tc.CONSTRAINT_TYPE = 'CHECK'
143
+ ORDER BY tc.TABLE_NAME, tc.CONSTRAINT_NAME
144
+ ")
145
+ else
146
+ []
147
+ end
148
+
149
+ check_constraints_by_table = check_constraints_data.each_with_object({}) do |ck, hash|
150
+ hash[ck['TABLE_NAME']] ||= []
151
+ hash[ck['TABLE_NAME']] << {
152
+ constraint_name: ck['CONSTRAINT_NAME'],
153
+ check_clause: ck['CHECK_CLAUSE']
154
+ }
155
+ end
156
+
124
157
  # Organize columns by table
125
158
  columns_by_table = columns_data.each_with_object({}) do |col, hash|
126
159
  hash[col['TABLE_NAME']] ||= []
@@ -133,6 +166,7 @@ module FastSchemaDumper
133
166
  table_name,
134
167
  columns: columns_by_table[table_name] || [],
135
168
  indexes: indexes_by_table[table_name] || {},
169
+ check_constraints: check_constraints_by_table[table_name] || {},
136
170
  options: table_options[table_name]
137
171
  )
138
172
  @output << ""
@@ -218,7 +252,7 @@ module FastSchemaDumper
218
252
  end
219
253
  end
220
254
 
221
- def dump_table(table_name, columns:, indexes:, options:)
255
+ def dump_table(table_name, columns:, indexes:, check_constraints:, options:)
222
256
  table_def = "create_table \"#{table_name}\""
223
257
 
224
258
  # id (primary key)
@@ -296,6 +330,37 @@ module FastSchemaDumper
296
330
  @output << " #{format_index(index_name, index_data)}"
297
331
  end
298
332
 
333
+ # Respect the CHECK constraint
334
+ #
335
+ # NOTE: original dumper sorts it according to the clause
336
+ # ref: https://github.com/rails/rails/blob/cddcba97c369e12e2573af5af9eda16e6f530a29/activerecord/lib/active_record/schema_dumper.rb#L284
337
+ check_constraints.sort_by { |c| c[:check_clause] }.each do |constraint|
338
+ check_clause = constraint[:check_clause]
339
+
340
+ # drop redundant parentheses at the most outer level for compatibility with the original dumper
341
+ # Example: "((a > 0) and (b > 0))" => "(a > 0) and (b > 0)"
342
+ if check_clause.start_with?("(") && check_clause.end_with?(")")
343
+ # Check if removing outer parens would break the expression by ensuring parentheses are balanced after removal
344
+ # For example, it shouldn’t remove the outer parentheses as in the following case, because doing so would break the balance: "(a > 0) and (b > 0)"
345
+ inner = check_clause[1..-2]
346
+ if balanced_parentheses?(inner)
347
+ check_clause = inner
348
+ end
349
+ end
350
+
351
+ check_clause.gsub!(/\\'/, "'") # don't escape single quotes for compatibility with the original dumper
352
+
353
+ ck_line = " t.check_constraint \"#{check_clause}\""
354
+
355
+ # Check if constraint name is custom (doesn't start with "chk_rails_")
356
+ # Rails generates constraint names starting with "chk_rails_" followed by a hash
357
+ if !constraint[:constraint_name].start_with?("chk_rails_")
358
+ ck_line += ", name: \"#{constraint[:constraint_name]}\""
359
+ end
360
+
361
+ @output << ck_line
362
+ end
363
+
299
364
  @output << "end"
300
365
  end
301
366
 
@@ -466,5 +531,15 @@ module FastSchemaDumper
466
531
 
467
532
  idx_def
468
533
  end
534
+
535
+ def balanced_parentheses?(str)
536
+ depth = 0
537
+ str.each_char do |char|
538
+ depth += 1 if char == '('
539
+ depth -= 1 if char == ')'
540
+ return false if depth < 0
541
+ end
542
+ depth == 0
543
+ end
469
544
  end
470
545
  end
@@ -17,9 +17,9 @@ module Ridgepole
17
17
  puts "Warning: fast_schema_dumper is enabled in verify mode" unless ENV['FAST_SCHEMA_DUMPER_SUPPRESS_MESSAGE'] == '1'
18
18
  original_results = original_dump
19
19
  fast_results = fast_dump
20
+ File.write("orig.txt", original_results)
21
+ File.write("fast.txt", fast_results)
20
22
  if original_results != fast_results
21
- File.write("orig.txt", original_results)
22
- File.write("fast.txt", fast_results)
23
23
  raise "Dumped schema do not match between ActiveRecord::SchemaDumper and fast_schema_dumper. This is a fast_schema_dumper bug."
24
24
  end
25
25
  fast_results
@@ -1,3 +1,3 @@
1
1
  module FastSchemaDumper
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_schema_dumper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Aritomo