fast_schema_dumper 0.1.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 +4 -4
- data/README.md +24 -7
- data/lib/fast_schema_dumper/fast_dumper.rb +67 -2
- data/lib/fast_schema_dumper/ridgepole.rb +2 -2
- data/lib/fast_schema_dumper/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c7ef2b48990a434194a1ac9ddd7394fbef2a5ad40befb705209ccf01edb19c4a
|
|
4
|
+
data.tar.gz: 6360a00616008fea6f45913597ce0fc9ebbca688243bb1a204fb0abb33922ea7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6a701f24b8eacb312ad56c60c96edd12aa795fa27ecf8e59e511455e55ff36efca84e6ecb9598d7ec197c0f3120a33fd487f3b1d59d34098cf469637c37aec85
|
|
7
|
+
data.tar.gz: 06cd91e908371c31c24bf90ba9d21c8f359d8a0007ceb2698a7e5712280bf191d010edf90360c72b2975b72d8df7a001bf1776591d4c23b1d9a0252301bd307c
|
data/README.md
CHANGED
|
@@ -1,8 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# fast_schema_dumper
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A super fast alternative to ActiveRecord::SchemaDumper. Currently only MySQL is supported.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
### Ridgepole integration
|
|
8
|
+
|
|
9
|
+
Requiring `fast_schema_dumper/ridgepole` will overwrite `Ridgepole::Dumper.dump`, which will force Ridgepole to use fast_schema_dumper.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
RUBYOPT='-rridgepole -rfast_schema_dumper' ridgepole ... --apply
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
#### Environment variables for Ridgepole
|
|
16
|
+
|
|
17
|
+
The Ridgepole integration is configurable via envionment variables.
|
|
18
|
+
|
|
19
|
+
- `FAST_SCHEMA_DUMPER_MODE`:
|
|
20
|
+
- `disabled`: Use the original ActiveRecord dumper
|
|
21
|
+
- `verify`: Run both dumpers and verify output matches (useful for testing)
|
|
22
|
+
- Any other value or unset: Use FastSchemaDumper (default)
|
|
23
|
+
- `FAST_SCHEMA_DUMPER_SUPPRESS_MESSAGE=1`: Suppress the warning message when FastSchemaDumper is enabled
|
|
24
|
+
|
|
25
|
+
I recommend using only fast_schema_dumper in local development environments, and configuring `FAST_SCHEMA_DUMPER_MODE=verify` in CI setups.
|
|
4
26
|
|
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fast_schema_dumper`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
27
|
|
|
7
28
|
## Installation
|
|
8
29
|
|
|
@@ -18,10 +39,6 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
|
18
39
|
gem install fast_schema_dumper
|
|
19
40
|
```
|
|
20
41
|
|
|
21
|
-
## Usage
|
|
22
|
-
|
|
23
|
-
TODO: Write usage instructions here
|
|
24
|
-
|
|
25
42
|
## Development
|
|
26
43
|
|
|
27
44
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -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
|
|
|
@@ -449,7 +508,13 @@ module FastSchemaDumper
|
|
|
449
508
|
end
|
|
450
509
|
|
|
451
510
|
unless order_hash.empty?
|
|
452
|
-
|
|
511
|
+
if index_data[:columns].size == 1
|
|
512
|
+
# For single column index, use simplified syntax
|
|
513
|
+
idx_def += ", order: :#{order_hash.values.first}"
|
|
514
|
+
else
|
|
515
|
+
# For compound index, use hash syntax
|
|
516
|
+
idx_def += ", order: { #{order_hash.map { |k, v| "#{k}: :#{v}" }.join(', ')} }"
|
|
517
|
+
end
|
|
453
518
|
end
|
|
454
519
|
end
|
|
455
520
|
|
|
@@ -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
|
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.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Daisuke Aritomo
|
|
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
58
58
|
- !ruby/object:Gem::Version
|
|
59
59
|
version: '0'
|
|
60
60
|
requirements: []
|
|
61
|
-
rubygems_version: 3.
|
|
61
|
+
rubygems_version: 3.8.0.dev
|
|
62
62
|
specification_version: 4
|
|
63
63
|
summary: A fast alternative to ActiveRecord::SchemaDumper
|
|
64
64
|
test_files: []
|