fast_schema_dumper 0.2.0 → 0.3.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: c7ef2b48990a434194a1ac9ddd7394fbef2a5ad40befb705209ccf01edb19c4a
4
+ data.tar.gz: 6360a00616008fea6f45913597ce0fc9ebbca688243bb1a204fb0abb33922ea7
5
5
  SHA512:
6
- metadata.gz: bad68066eed1d18fdb2df665d0f17dfa07654ddeb5dec4d4ad2cf4292e7c73f0303f9ea969a3410c76589625551bc29b6dfdb68fb7f06bdb63c50365ff1c55c0
7
- data.tar.gz: e9ea6b8ef54edfabb6ee4c24265a02ddc8544aefda247c3bf48e21bba8b5952b093f8cacfce31a765ba965809e1514a06233531e466950780a1c837a5f5cc7db
6
+ metadata.gz: 6a701f24b8eacb312ad56c60c96edd12aa795fa27ecf8e59e511455e55ff36efca84e6ecb9598d7ec197c0f3120a33fd487f3b1d59d34098cf469637c37aec85
7
+ data.tar.gz: 06cd91e908371c31c24bf90ba9d21c8f359d8a0007ceb2698a7e5712280bf191d010edf90360c72b2975b72d8df7a001bf1776591d4c23b1d9a0252301bd307c
@@ -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,31 @@ 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
+ if check_clause.start_with?("(") && check_clause.end_with?(")")
342
+ check_clause = check_clause[1..-2]
343
+ end
344
+
345
+ check_clause.gsub!(/\\'/, "'") # don't escape single quotes for compatibility with the original dumper
346
+
347
+ ck_line = " t.check_constraint \"#{check_clause}\""
348
+
349
+ # Check if constraint name is custom (doesn't start with "chk_rails_")
350
+ # Rails generates constraint names starting with "chk_rails_" followed by a hash
351
+ if !constraint[:constraint_name].start_with?("chk_rails_")
352
+ ck_line += ", name: \"#{constraint[:constraint_name]}\""
353
+ end
354
+
355
+ @output << ck_line
356
+ end
357
+
299
358
  @output << "end"
300
359
  end
301
360
 
@@ -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.3.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.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Aritomo