better_structure_sql 0.2.2 → 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/CHANGELOG.md +69 -0
- data/README.md +218 -686
- data/app/CLAUDE.md +12 -0
- data/app/controllers/better_structure_sql/application_controller.rb +71 -31
- data/app/controllers/better_structure_sql/schema_versions_controller.rb +94 -153
- data/app/helpers/better_structure_sql/schema_versions_helper.rb +48 -24
- data/app/views/better_structure_sql/schema_versions/index.html.erb +46 -49
- data/app/views/better_structure_sql/schema_versions/show.html.erb +73 -120
- data/app/views/layouts/better_structure_sql/application.html.erb +27 -18
- data/config/routes.rb +1 -1
- data/lib/CLAUDE.md +73 -0
- data/lib/better_structure_sql/adapters/base_adapter.rb +16 -1
- data/lib/better_structure_sql/adapters/mysql_adapter.rb +396 -364
- data/lib/better_structure_sql/adapters/postgresql_adapter.rb +574 -478
- data/lib/better_structure_sql/adapters/registry.rb +18 -15
- data/lib/better_structure_sql/adapters/sqlite_adapter.rb +221 -474
- data/lib/better_structure_sql/configuration.rb +9 -2
- data/lib/better_structure_sql/creation_order.rb +157 -0
- data/lib/better_structure_sql/dependency_resolver.rb +4 -2
- data/lib/better_structure_sql/dumper.rb +173 -315
- data/lib/better_structure_sql/engine.rb +50 -4
- data/lib/better_structure_sql/file_writer.rb +108 -97
- data/lib/better_structure_sql/generators/base.rb +31 -4
- data/lib/better_structure_sql/generators/comment_generator.rb +16 -12
- data/lib/better_structure_sql/generators/domain_generator.rb +8 -6
- data/lib/better_structure_sql/generators/extension_generator.rb +7 -2
- data/lib/better_structure_sql/generators/foreign_key_generator.rb +14 -4
- data/lib/better_structure_sql/generators/index_generator.rb +0 -20
- data/lib/better_structure_sql/generators/materialized_view_generator.rb +17 -2
- data/lib/better_structure_sql/generators/sequence_generator.rb +13 -5
- data/lib/better_structure_sql/generators/table_generator.rb +164 -47
- data/lib/better_structure_sql/generators/type_generator.rb +25 -14
- data/lib/better_structure_sql/generators/view_generator.rb +12 -8
- data/lib/better_structure_sql/introspection/extensions.rb +1 -14
- data/lib/better_structure_sql/introspection/foreign_keys.rb +1 -14
- data/lib/better_structure_sql/introspection/functions.rb +1 -14
- data/lib/better_structure_sql/introspection/indexes.rb +1 -14
- data/lib/better_structure_sql/introspection/sequences.rb +1 -14
- data/lib/better_structure_sql/introspection/tables.rb +1 -14
- data/lib/better_structure_sql/introspection/triggers.rb +1 -14
- data/lib/better_structure_sql/introspection/types.rb +1 -14
- data/lib/better_structure_sql/introspection/views.rb +2 -19
- data/lib/better_structure_sql/introspection.rb +12 -1
- data/lib/better_structure_sql/migration_patch.rb +39 -139
- data/lib/better_structure_sql/railtie.rb +53 -56
- data/lib/better_structure_sql/schema_loader.rb +107 -86
- data/lib/better_structure_sql/schema_version.rb +3 -2
- data/lib/better_structure_sql/schema_versions.rb +70 -71
- data/lib/better_structure_sql/version.rb +1 -1
- data/lib/better_structure_sql/zip_generator.rb +43 -6
- data/lib/better_structure_sql.rb +2 -1
- data/lib/generators/better_structure_sql/migration_generator.rb +1 -1
- data/lib/generators/better_structure_sql/templates/README +17 -34
- data/lib/tasks/better_structure_sql.rake +19 -40
- metadata +9 -147
- data/config/database.yml +0 -3
|
@@ -7,6 +7,21 @@ module BetterStructureSql
|
|
|
7
7
|
# Supports both single-file and multi-file dump modes with optional
|
|
8
8
|
# schema version storage.
|
|
9
9
|
class Dumper
|
|
10
|
+
# Lines an INSERT statement adds around its list of versions
|
|
11
|
+
MIGRATION_INSERT_OVERHEAD_LINES = 3
|
|
12
|
+
|
|
13
|
+
# Titles of the runs a dependency-ordered sequence is split into in the single-file dump
|
|
14
|
+
SECTION_TITLES = {
|
|
15
|
+
types: '-- Custom Types',
|
|
16
|
+
domains: '-- Domains',
|
|
17
|
+
functions: '-- Functions',
|
|
18
|
+
views: '-- Views',
|
|
19
|
+
materialized_views: '-- Materialized Views'
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
# Sections whose statements are separated by a newline instead of a blank line
|
|
23
|
+
COMPACT_SECTIONS = %i[types domains].freeze
|
|
24
|
+
|
|
10
25
|
attr_reader :config, :connection, :adapter
|
|
11
26
|
|
|
12
27
|
def initialize(config = BetterStructureSql.configuration, connection = ActiveRecord::Base.connection)
|
|
@@ -22,18 +37,25 @@ module BetterStructureSql
|
|
|
22
37
|
# @raise [Error] If configuration is invalid
|
|
23
38
|
def dump(store_version: nil)
|
|
24
39
|
config.validate!
|
|
40
|
+
@creation_order = @functions = @custom_types = nil
|
|
25
41
|
|
|
26
42
|
output_mode = detect_output_mode
|
|
27
43
|
|
|
28
44
|
if output_mode == :multi_file
|
|
29
|
-
dump_multi_file(store_version:
|
|
45
|
+
dump_multi_file(store_version:)
|
|
30
46
|
else
|
|
31
|
-
dump_single_file(store_version:
|
|
47
|
+
dump_single_file(store_version:)
|
|
32
48
|
end
|
|
33
49
|
end
|
|
34
50
|
|
|
35
51
|
private
|
|
36
52
|
|
|
53
|
+
# Whether this dump should also store a schema version (nil defers to configuration)
|
|
54
|
+
def store_version?(requested)
|
|
55
|
+
requested = config.enable_schema_versions if requested.nil?
|
|
56
|
+
requested && config.enable_schema_versions
|
|
57
|
+
end
|
|
58
|
+
|
|
37
59
|
# Detect if we're in single-file or multi-file mode
|
|
38
60
|
def detect_output_mode
|
|
39
61
|
FileWriter.new(config).detect_output_mode(config.output_path)
|
|
@@ -45,26 +67,21 @@ module BetterStructureSql
|
|
|
45
67
|
output << header
|
|
46
68
|
output << extensions_section if config.include_extensions
|
|
47
69
|
output << set_schema_section
|
|
48
|
-
output
|
|
49
|
-
output
|
|
50
|
-
output << functions_section if config.include_functions
|
|
70
|
+
output.concat(titled_runs(type_statements))
|
|
71
|
+
output.concat(titled_runs(early_statements))
|
|
51
72
|
output << sequences_section if config.include_sequences
|
|
52
73
|
output << tables_section
|
|
53
74
|
output << indexes_section
|
|
54
75
|
output << foreign_keys_section
|
|
55
|
-
output
|
|
56
|
-
output << materialized_views_section if config.include_materialized_views
|
|
76
|
+
output.concat(titled_runs(relation_dependent_statements))
|
|
57
77
|
output << triggers_section if config.include_triggers
|
|
58
78
|
output << comments_section if config.include_comments
|
|
59
79
|
output << schema_migrations_section
|
|
60
80
|
output << footer
|
|
61
81
|
|
|
62
82
|
formatted_output = Formatter.new(config).format(output.compact.join("\n\n"))
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
# Store version if requested and enabled
|
|
66
|
-
store_version = config.enable_schema_versions if store_version.nil?
|
|
67
|
-
store_schema_version(formatted_output) if store_version && config.enable_schema_versions
|
|
83
|
+
FileWriter.new(config).write_single_file(config.output_path, formatted_output)
|
|
84
|
+
store_schema_version if store_version?(store_version)
|
|
68
85
|
|
|
69
86
|
formatted_output
|
|
70
87
|
end
|
|
@@ -79,22 +96,9 @@ module BetterStructureSql
|
|
|
79
96
|
header_content = [header, set_schema_section].compact.join("\n\n")
|
|
80
97
|
file_map = writer.write_multi_file(config.output_path, sections, header_content)
|
|
81
98
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
manifest_content = manifest_generator.generate(file_map)
|
|
86
|
-
|
|
87
|
-
manifest_path = Rails.root.join(config.output_path, '_manifest.json')
|
|
88
|
-
File.write(manifest_path, manifest_content)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
# Store version if requested and enabled
|
|
92
|
-
# For multi-file, combine all sections for storage
|
|
93
|
-
store_version = config.enable_schema_versions if store_version.nil?
|
|
94
|
-
if store_version && config.enable_schema_versions
|
|
95
|
-
combined_content = combine_sections_for_storage(sections, header_content)
|
|
96
|
-
store_schema_version(combined_content)
|
|
97
|
-
end
|
|
99
|
+
manifest_content = ManifestGenerator.new(config).generate(file_map) if config.generate_manifest
|
|
100
|
+
writer.write_manifest(config.output_path, manifest_content)
|
|
101
|
+
store_schema_version if store_version?(store_version)
|
|
98
102
|
|
|
99
103
|
file_map
|
|
100
104
|
end
|
|
@@ -106,18 +110,16 @@ module BetterStructureSql
|
|
|
106
110
|
sections = {}
|
|
107
111
|
|
|
108
112
|
sections[:extensions] = generate_extensions_section if config.include_extensions
|
|
109
|
-
sections[:types] =
|
|
110
|
-
sections[:
|
|
111
|
-
sections[:functions] = generate_functions_section if config.include_functions
|
|
113
|
+
sections[:types] = type_statements.map(&:last).presence
|
|
114
|
+
sections[:functions] = early_statements.map(&:last).presence
|
|
112
115
|
sections[:sequences] = generate_sequences_section if config.include_sequences
|
|
113
116
|
sections[:tables] = generate_tables_section
|
|
114
117
|
sections[:indexes] = generate_indexes_section
|
|
115
118
|
sections[:foreign_keys] = generate_foreign_keys_section unless sqlite_adapter?
|
|
116
|
-
sections[:views] =
|
|
117
|
-
sections[:materialized_views] = generate_materialized_views_section if config.include_materialized_views
|
|
119
|
+
sections[:views] = relation_dependent_statements.map(&:last).presence
|
|
118
120
|
sections[:triggers] = generate_triggers_section if config.include_triggers
|
|
119
121
|
sections[:comments] = generate_comments_section if config.include_comments
|
|
120
|
-
sections[:migrations] = generate_migrations_section
|
|
122
|
+
sections[:migrations] = generate_migrations_section
|
|
121
123
|
|
|
122
124
|
sections.compact
|
|
123
125
|
end
|
|
@@ -133,39 +135,6 @@ module BetterStructureSql
|
|
|
133
135
|
extensions.map { |ext| generator.generate(ext) }
|
|
134
136
|
end
|
|
135
137
|
|
|
136
|
-
# Generate custom types section (enums, composite types)
|
|
137
|
-
#
|
|
138
|
-
# @return [Array<String>, nil] Array of CREATE TYPE statements or nil if empty
|
|
139
|
-
def generate_types_section
|
|
140
|
-
types = Introspection.fetch_custom_types(connection).reject { |t| t[:type] == 'domain' }
|
|
141
|
-
return nil if types.empty?
|
|
142
|
-
|
|
143
|
-
generator = Generators::TypeGenerator.new(config)
|
|
144
|
-
types.filter_map { |type| generator.generate(type) }
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
# Generate domains section
|
|
148
|
-
#
|
|
149
|
-
# @return [Array<String>, nil] Array of CREATE DOMAIN statements or nil if empty
|
|
150
|
-
def generate_domains_section
|
|
151
|
-
domains = Introspection.fetch_custom_types(connection).select { |t| t[:type] == 'domain' }
|
|
152
|
-
return nil if domains.empty?
|
|
153
|
-
|
|
154
|
-
generator = Generators::DomainGenerator.new(config)
|
|
155
|
-
domains.map { |domain| generator.generate(domain) }
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
# Generate functions section
|
|
159
|
-
#
|
|
160
|
-
# @return [Array<String>, nil] Array of CREATE FUNCTION statements or nil if empty
|
|
161
|
-
def generate_functions_section
|
|
162
|
-
functions = Introspection.fetch_functions(connection)
|
|
163
|
-
return nil if functions.empty?
|
|
164
|
-
|
|
165
|
-
generator = Generators::FunctionGenerator.new(config)
|
|
166
|
-
functions.map { |func| generator.generate(func) }
|
|
167
|
-
end
|
|
168
|
-
|
|
169
138
|
# Generate sequences section
|
|
170
139
|
#
|
|
171
140
|
# @return [Array<String>, nil] Array of CREATE SEQUENCE statements or nil if empty
|
|
@@ -181,12 +150,9 @@ module BetterStructureSql
|
|
|
181
150
|
#
|
|
182
151
|
# @return [Array<String>, nil] Array of CREATE TABLE statements or nil if empty
|
|
183
152
|
def generate_tables_section
|
|
184
|
-
tables =
|
|
185
|
-
tables = tables.sort_by { |t| t[:name] } if config.sort_tables
|
|
153
|
+
tables = ordered_tables
|
|
186
154
|
return nil if tables.empty?
|
|
187
155
|
|
|
188
|
-
attach_foreign_keys_for_sqlite(tables) if sqlite_adapter?
|
|
189
|
-
|
|
190
156
|
generator = Generators::TableGenerator.new(config, adapter)
|
|
191
157
|
tables.map { |table| generator.generate(table) }
|
|
192
158
|
end
|
|
@@ -213,28 +179,6 @@ module BetterStructureSql
|
|
|
213
179
|
foreign_keys.map { |fk| generator.generate(fk) }
|
|
214
180
|
end
|
|
215
181
|
|
|
216
|
-
# Generate views section
|
|
217
|
-
#
|
|
218
|
-
# @return [Array<String>, nil] Array of CREATE VIEW statements or nil if empty
|
|
219
|
-
def generate_views_section
|
|
220
|
-
views = Introspection.fetch_views(connection)
|
|
221
|
-
return nil if views.empty?
|
|
222
|
-
|
|
223
|
-
generator = Generators::ViewGenerator.new(config)
|
|
224
|
-
views.map { |view| generator.generate(view) }
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
# Generate materialized views section
|
|
228
|
-
#
|
|
229
|
-
# @return [Array<String>, nil] Array of CREATE MATERIALIZED VIEW statements or nil if empty
|
|
230
|
-
def generate_materialized_views_section
|
|
231
|
-
matviews = Introspection.fetch_materialized_views(connection)
|
|
232
|
-
return nil if matviews.empty?
|
|
233
|
-
|
|
234
|
-
generator = Generators::MaterializedViewGenerator.new(config)
|
|
235
|
-
matviews.map { |mv| generator.generate(mv) }
|
|
236
|
-
end
|
|
237
|
-
|
|
238
182
|
# Generate triggers section
|
|
239
183
|
#
|
|
240
184
|
# @return [Array<String>, nil] Array of CREATE TRIGGER statements or nil if empty
|
|
@@ -255,34 +199,19 @@ module BetterStructureSql
|
|
|
255
199
|
all_comments = adapter.fetch_comments(connection)
|
|
256
200
|
generator = Generators::CommentGenerator.new(config)
|
|
257
201
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
generator.generate(object_type: :table, object_name: table_name, comment: comment)
|
|
202
|
+
comments = all_comments[:tables].map do |table_name, comment|
|
|
203
|
+
generator.generate(object_type: :table, object_name: table_name, comment:)
|
|
261
204
|
end
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
all_comments[:columns].each do |column_identifier, comment|
|
|
265
|
-
comments_array << generator.generate(object_type: :column, object_name: column_identifier, comment: comment)
|
|
266
|
-
end
|
|
267
|
-
|
|
268
|
-
# Generate index comments (PostgreSQL only)
|
|
269
|
-
all_comments[:indexes].each do |index_name, comment|
|
|
270
|
-
comments_array << generator.generate(object_type: :index, object_name: index_name, comment: comment)
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
# Generate view comments (PostgreSQL only)
|
|
274
|
-
all_comments[:views].each do |view_name, comment|
|
|
275
|
-
comments_array << generator.generate(object_type: :view, object_name: view_name, comment: comment)
|
|
205
|
+
all_comments[:columns].each do |(table_name, column_name), comment|
|
|
206
|
+
comments << generator.generate(object_type: :column, table_name:, object_name: column_name, comment:)
|
|
276
207
|
end
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
208
|
+
{ index: :indexes, view: :views, function: :functions, procedure: :procedures }.each do |object_type, key|
|
|
209
|
+
all_comments.fetch(key).each do |object_name, comment|
|
|
210
|
+
comments << generator.generate(object_type:, object_name:, comment:)
|
|
211
|
+
end
|
|
281
212
|
end
|
|
282
213
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
comments_array
|
|
214
|
+
comments.presence
|
|
286
215
|
end
|
|
287
216
|
|
|
288
217
|
# Generate migrations section
|
|
@@ -294,21 +223,14 @@ module BetterStructureSql
|
|
|
294
223
|
versions = fetch_schema_migration_versions
|
|
295
224
|
return nil if versions.empty?
|
|
296
225
|
|
|
297
|
-
|
|
298
|
-
version_chunks = versions.each_slice(chunk_size).to_a
|
|
299
|
-
|
|
300
|
-
version_chunks.map { |chunk| generate_migrations_batch(chunk) }
|
|
226
|
+
versions.each_slice(migrations_per_insert).map { |chunk| generate_migrations_batch(chunk) }
|
|
301
227
|
end
|
|
302
228
|
|
|
303
|
-
#
|
|
229
|
+
# Versions per INSERT statement, leaving room for the INSERT and conflict lines in a file
|
|
304
230
|
#
|
|
305
|
-
# @
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
all_foreign_keys = Introspection.fetch_foreign_keys(connection)
|
|
309
|
-
tables.each do |table|
|
|
310
|
-
table[:foreign_keys] = all_foreign_keys.select { |fk| fk[:table] == table[:name] }
|
|
311
|
-
end
|
|
231
|
+
# @return [Integer] Number of versions (at least one)
|
|
232
|
+
def migrations_per_insert
|
|
233
|
+
[config.max_lines_per_file - MIGRATION_INSERT_OVERHEAD_LINES, 1].max
|
|
312
234
|
end
|
|
313
235
|
|
|
314
236
|
# Check if current adapter is SQLite
|
|
@@ -318,42 +240,13 @@ module BetterStructureSql
|
|
|
318
240
|
adapter.class.name == 'BetterStructureSql::Adapters::SqliteAdapter'
|
|
319
241
|
end
|
|
320
242
|
|
|
321
|
-
# Combine sections for database storage (when versioning is enabled)
|
|
322
|
-
def combine_sections_for_storage(sections, header_content)
|
|
323
|
-
output = [header_content]
|
|
324
|
-
|
|
325
|
-
section_order = %i[
|
|
326
|
-
extensions types domains functions sequences
|
|
327
|
-
tables indexes foreign_keys views materialized_views triggers
|
|
328
|
-
]
|
|
329
|
-
|
|
330
|
-
section_order.each do |section_key|
|
|
331
|
-
next unless sections.key?(section_key)
|
|
332
|
-
|
|
333
|
-
section_content = sections[section_key]
|
|
334
|
-
next if section_content.blank?
|
|
335
|
-
|
|
336
|
-
# Add section header
|
|
337
|
-
header = "-- #{section_key.to_s.split('_').map(&:capitalize).join(' ')}"
|
|
338
|
-
output << header
|
|
339
|
-
output << section_content.join("\n\n")
|
|
340
|
-
end
|
|
341
|
-
|
|
342
|
-
# Add schema migrations
|
|
343
|
-
migrations = schema_migrations_section
|
|
344
|
-
output << migrations if migrations
|
|
345
|
-
|
|
346
|
-
output << footer
|
|
347
|
-
|
|
348
|
-
Formatter.new(config).format(output.compact.join("\n\n"))
|
|
349
|
-
end
|
|
350
|
-
|
|
351
243
|
def header
|
|
352
244
|
case adapter.class.name
|
|
353
245
|
when 'BetterStructureSql::Adapters::PostgresqlAdapter'
|
|
354
246
|
<<~HEADER.strip
|
|
355
247
|
SET client_encoding = 'UTF8';
|
|
356
248
|
SET standard_conforming_strings = on;
|
|
249
|
+
SET check_function_bodies = false;
|
|
357
250
|
HEADER
|
|
358
251
|
when 'BetterStructureSql::Adapters::SqliteAdapter'
|
|
359
252
|
# SQLite PRAGMA statements for optimal behavior and compatibility
|
|
@@ -394,18 +287,6 @@ module BetterStructureSql
|
|
|
394
287
|
end
|
|
395
288
|
end
|
|
396
289
|
|
|
397
|
-
def custom_types_section
|
|
398
|
-
# Only include enums and composite types (not domains, they have their own section)
|
|
399
|
-
types = Introspection.fetch_custom_types(connection).reject { |t| t[:type] == 'domain' }
|
|
400
|
-
return nil if types.empty?
|
|
401
|
-
|
|
402
|
-
generator = Generators::TypeGenerator.new(config)
|
|
403
|
-
lines = types.filter_map { |type| generator.generate(type) }
|
|
404
|
-
return nil if lines.empty?
|
|
405
|
-
|
|
406
|
-
(['-- Custom Types'] + lines).join("\n")
|
|
407
|
-
end
|
|
408
|
-
|
|
409
290
|
def sequences_section
|
|
410
291
|
sequences = Introspection.fetch_sequences(connection)
|
|
411
292
|
return nil if sequences.empty?
|
|
@@ -416,19 +297,34 @@ module BetterStructureSql
|
|
|
416
297
|
lines.join("\n")
|
|
417
298
|
end
|
|
418
299
|
|
|
419
|
-
|
|
300
|
+
# Tables in dump order: by name when sort_tables is set, except that an
|
|
301
|
+
# inheritance child is deferred until right after the last of its parents
|
|
302
|
+
def ordered_tables
|
|
420
303
|
tables = Introspection.fetch_tables(connection)
|
|
421
304
|
tables = tables.sort_by { |t| t[:name] } if config.sort_tables
|
|
305
|
+
names = tables.to_set { |table| table[:name] }
|
|
306
|
+
|
|
307
|
+
placed = Set.new
|
|
308
|
+
waiting = []
|
|
309
|
+
ordered = []
|
|
310
|
+
place = lambda do |table|
|
|
311
|
+
ordered << table
|
|
312
|
+
placed << table[:name]
|
|
313
|
+
ready, waiting = waiting.partition { |child| parents_placed?(child, names, placed) }
|
|
314
|
+
ready.each(&place)
|
|
315
|
+
end
|
|
316
|
+
tables.each { |table| parents_placed?(table, names, placed) ? place.call(table) : waiting << table }
|
|
317
|
+
ordered + waiting
|
|
318
|
+
end
|
|
422
319
|
|
|
423
|
-
|
|
320
|
+
def parents_placed?(table, names, placed)
|
|
321
|
+
Array(table[:inherits]).all? { |parent| placed.include?(parent) || names.exclude?(parent) }
|
|
322
|
+
end
|
|
424
323
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
table[:foreign_keys] = all_foreign_keys.select { |fk| fk[:table] == table[:name] }
|
|
430
|
-
end
|
|
431
|
-
end
|
|
324
|
+
def tables_section
|
|
325
|
+
tables = ordered_tables
|
|
326
|
+
|
|
327
|
+
return '-- Tables' if tables.empty?
|
|
432
328
|
|
|
433
329
|
generator = Generators::TableGenerator.new(config, adapter)
|
|
434
330
|
lines = []
|
|
@@ -469,44 +365,101 @@ module BetterStructureSql
|
|
|
469
365
|
lines.join("\n")
|
|
470
366
|
end
|
|
471
367
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
368
|
+
# Dependency information for this dump (queried once)
|
|
369
|
+
def creation_order
|
|
370
|
+
@creation_order ||= CreationOrder.for(adapter, connection)
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def functions
|
|
374
|
+
@functions ||= config.include_functions ? Introspection.fetch_functions(connection) : []
|
|
375
|
+
end
|
|
475
376
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
377
|
+
def keyed_functions
|
|
378
|
+
functions.map { |function| [CreationOrder.function_key(function), function] }
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# Enums, composites, ranges and domains the dump includes, in name order within each kind
|
|
382
|
+
def custom_types
|
|
383
|
+
@custom_types ||= begin
|
|
384
|
+
types = Introspection.fetch_custom_types(connection)
|
|
385
|
+
domains, others = types.partition { |type| type[:type] == 'domain' }
|
|
386
|
+
(config.include_custom_types ? others : []) + (config.include_domains ? domains : [])
|
|
387
|
+
end
|
|
480
388
|
end
|
|
481
389
|
|
|
482
|
-
def
|
|
483
|
-
|
|
484
|
-
|
|
390
|
+
def keyed_type_statements
|
|
391
|
+
generator = Generators::TypeGenerator.new(config)
|
|
392
|
+
custom_types.filter_map do |type|
|
|
393
|
+
statement = generator.generate(type)
|
|
394
|
+
statement && [CreationOrder.type_key(type[:name]), [type[:type] == 'domain' ? :domains : :types, statement]]
|
|
395
|
+
end
|
|
396
|
+
end
|
|
485
397
|
|
|
398
|
+
# Custom types that need no routine, dependency-ordered: created first
|
|
399
|
+
#
|
|
400
|
+
# @return [Array<Array(Symbol, String)>] Pairs of section and SQL statement
|
|
401
|
+
def type_statements
|
|
402
|
+
creation_order.sort(keyed_type_statements.reject { |key, _statement| creation_order.needs_function?(key) })
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# Functions that need no relation, together with the custom types that need a
|
|
406
|
+
# routine (a domain CHECK calling a function, a range's canonical function),
|
|
407
|
+
# dependency-ordered: created before tables, whose defaults, checks and indexes may use them
|
|
408
|
+
#
|
|
409
|
+
# @return [Array<Array(Symbol, String)>] Pairs of section and SQL statement
|
|
410
|
+
def early_statements
|
|
486
411
|
generator = Generators::FunctionGenerator.new(config)
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
412
|
+
function_statements = keyed_functions.reject { |key, _function| creation_order.needs_relation?(key) }.map do |key, function|
|
|
413
|
+
[key, [:functions, generator.generate(function)]]
|
|
414
|
+
end
|
|
415
|
+
type_statements = keyed_type_statements.select { |key, _statement| creation_order.needs_function?(key) }
|
|
416
|
+
|
|
417
|
+
creation_order.sort(type_statements + function_statements)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# Views, materialized views and the functions that need a relation, as one
|
|
421
|
+
# sequence in which every object follows the objects it depends on
|
|
422
|
+
#
|
|
423
|
+
# @return [Array<Array(Symbol, String)>] Pairs of section and SQL statement
|
|
424
|
+
def relation_dependent_statements
|
|
425
|
+
creation_order.sort(view_statements + materialized_view_statements + relation_dependent_function_statements)
|
|
490
426
|
end
|
|
491
427
|
|
|
492
|
-
def
|
|
493
|
-
|
|
494
|
-
return nil if views.empty?
|
|
428
|
+
def view_statements
|
|
429
|
+
return [] unless config.include_views
|
|
495
430
|
|
|
496
431
|
generator = Generators::ViewGenerator.new(config)
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
432
|
+
Introspection.fetch_views(connection).map do |view|
|
|
433
|
+
[CreationOrder.relation_key(view[:name]), [:views, generator.generate(view)]]
|
|
434
|
+
end
|
|
500
435
|
end
|
|
501
436
|
|
|
502
|
-
def
|
|
503
|
-
|
|
504
|
-
return nil if matviews.empty?
|
|
437
|
+
def materialized_view_statements
|
|
438
|
+
return [] unless config.include_materialized_views
|
|
505
439
|
|
|
506
440
|
generator = Generators::MaterializedViewGenerator.new(config)
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
441
|
+
Introspection.fetch_materialized_views(connection).map do |matview|
|
|
442
|
+
[CreationOrder.relation_key(matview[:name]), [:materialized_views, generator.generate(matview)]]
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
def relation_dependent_function_statements
|
|
447
|
+
generator = Generators::FunctionGenerator.new(config)
|
|
448
|
+
keyed_functions.select { |key, _function| creation_order.needs_relation?(key) }.map do |key, function|
|
|
449
|
+
[key, [:functions, generator.generate(function)]]
|
|
450
|
+
end
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
# Groups dependency-ordered statements into titled runs for the single-file dump
|
|
454
|
+
#
|
|
455
|
+
# @param statements [Array<Array(Symbol, String)>] Pairs of section and SQL statement
|
|
456
|
+
# @return [Array<String>] One text block per run of statements of the same section
|
|
457
|
+
def titled_runs(statements)
|
|
458
|
+
statements.chunk_while { |(left, _), (right, _)| left == right }.map do |run|
|
|
459
|
+
section = run.first.first
|
|
460
|
+
separator = COMPACT_SECTIONS.include?(section) ? "\n" : "\n\n"
|
|
461
|
+
([SECTION_TITLES.fetch(section)] + run.map(&:last)).join(separator)
|
|
462
|
+
end
|
|
510
463
|
end
|
|
511
464
|
|
|
512
465
|
def triggers_section
|
|
@@ -520,130 +473,44 @@ module BetterStructureSql
|
|
|
520
473
|
end
|
|
521
474
|
|
|
522
475
|
def comments_section
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
all_comments = adapter.fetch_comments(connection)
|
|
526
|
-
generator = Generators::CommentGenerator.new(config)
|
|
527
|
-
|
|
528
|
-
# Generate table comments
|
|
529
|
-
comments_array = all_comments[:tables].map do |table_name, comment|
|
|
530
|
-
generator.generate(object_type: :table, object_name: table_name, comment: comment)
|
|
531
|
-
end
|
|
532
|
-
|
|
533
|
-
# Generate column comments
|
|
534
|
-
all_comments[:columns].each do |column_identifier, comment|
|
|
535
|
-
comments_array << generator.generate(object_type: :column, object_name: column_identifier, comment: comment)
|
|
536
|
-
end
|
|
537
|
-
|
|
538
|
-
# Generate index comments (PostgreSQL only)
|
|
539
|
-
all_comments[:indexes].each do |index_name, comment|
|
|
540
|
-
comments_array << generator.generate(object_type: :index, object_name: index_name, comment: comment)
|
|
541
|
-
end
|
|
542
|
-
|
|
543
|
-
# Generate view comments (PostgreSQL only)
|
|
544
|
-
all_comments[:views].each do |view_name, comment|
|
|
545
|
-
comments_array << generator.generate(object_type: :view, object_name: view_name, comment: comment)
|
|
546
|
-
end
|
|
547
|
-
|
|
548
|
-
# Generate function comments (PostgreSQL only)
|
|
549
|
-
all_comments[:functions].each do |function_name, comment|
|
|
550
|
-
comments_array << generator.generate(object_type: :function, object_name: function_name, comment: comment)
|
|
551
|
-
end
|
|
552
|
-
|
|
553
|
-
return nil if comments_array.empty?
|
|
554
|
-
|
|
555
|
-
lines = ['-- Comments']
|
|
556
|
-
lines += comments_array
|
|
557
|
-
lines.join("\n")
|
|
476
|
+
statements = generate_comments_section
|
|
477
|
+
statements && (['-- Comments'] + statements).join("\n")
|
|
558
478
|
end
|
|
559
479
|
|
|
560
480
|
def schema_migrations_section
|
|
561
|
-
# SQLite doesn't include schema_migrations in structure.sql
|
|
562
|
-
# Rails manages this table separately
|
|
563
|
-
return nil if adapter.class.name == 'BetterStructureSql::Adapters::SqliteAdapter'
|
|
564
|
-
|
|
565
481
|
return nil unless table_exists?('schema_migrations')
|
|
566
482
|
|
|
567
483
|
versions = fetch_schema_migration_versions
|
|
568
484
|
return nil if versions.empty?
|
|
569
485
|
|
|
570
|
-
|
|
571
|
-
table_quote = quote_table_name('schema_migrations')
|
|
572
|
-
|
|
573
|
-
lines = ['-- Schema Migrations']
|
|
574
|
-
|
|
575
|
-
# Use adapter-specific conflict resolution and quoting
|
|
576
|
-
case adapter.class.name
|
|
577
|
-
when 'BetterStructureSql::Adapters::PostgresqlAdapter'
|
|
578
|
-
lines << "INSERT INTO #{table_quote} (version) VALUES"
|
|
579
|
-
lines << versions.map { |v| "('#{v}')" }.join(",\n")
|
|
580
|
-
lines << 'ON CONFLICT DO NOTHING;'
|
|
581
|
-
when 'BetterStructureSql::Adapters::MysqlAdapter'
|
|
582
|
-
# MySQL uses INSERT IGNORE and backticks
|
|
583
|
-
lines << "INSERT IGNORE INTO #{table_quote} (version) VALUES"
|
|
584
|
-
lines << versions.map { |v| "('#{v}')" }.join(",\n")
|
|
585
|
-
lines << ';'
|
|
586
|
-
else
|
|
587
|
-
lines << "INSERT INTO #{table_quote} (version) VALUES"
|
|
588
|
-
lines << versions.map { |v| "('#{v}')" }.join(",\n")
|
|
589
|
-
lines << ';'
|
|
590
|
-
end
|
|
591
|
-
|
|
592
|
-
lines.join("\n")
|
|
593
|
-
end
|
|
594
|
-
|
|
595
|
-
def search_path_section
|
|
596
|
-
"SET search_path TO #{config.search_path};"
|
|
486
|
+
"-- Schema Migrations\n#{generate_migrations_batch(versions)}"
|
|
597
487
|
end
|
|
598
488
|
|
|
599
489
|
def footer
|
|
600
490
|
'' # Just a blank line to ensure newline at end
|
|
601
491
|
end
|
|
602
492
|
|
|
603
|
-
def write_to_file(content)
|
|
604
|
-
file_path = Rails.root.join(config.output_path)
|
|
605
|
-
FileUtils.mkdir_p(File.dirname(file_path))
|
|
606
|
-
File.write(file_path, content)
|
|
607
|
-
end
|
|
608
|
-
|
|
609
493
|
def table_exists?(table_name)
|
|
610
494
|
connection.table_exists?(table_name)
|
|
611
495
|
end
|
|
612
496
|
|
|
613
497
|
def fetch_schema_migration_versions
|
|
614
498
|
connection.select_values('SELECT version FROM schema_migrations ORDER BY version')
|
|
615
|
-
rescue ActiveRecord::StatementInvalid
|
|
616
|
-
[]
|
|
617
499
|
end
|
|
618
500
|
|
|
619
501
|
def generate_migrations_batch(versions)
|
|
620
|
-
|
|
502
|
+
values = versions.map { |version| "(#{connection.quote(version)})" }.join(",\n")
|
|
621
503
|
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
# Generate single batch INSERT with all versions
|
|
626
|
-
# This will be chunked by FileWriter if it exceeds max_lines_per_file
|
|
627
|
-
lines = []
|
|
504
|
+
[migrations_insert_prefix, values, migrations_insert_suffix].join("\n")
|
|
505
|
+
end
|
|
628
506
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
lines << versions.map { |v| "('#{v}')" }.join(",\n")
|
|
634
|
-
lines << 'ON CONFLICT DO NOTHING;'
|
|
635
|
-
when 'BetterStructureSql::Adapters::MysqlAdapter'
|
|
636
|
-
# MySQL uses INSERT IGNORE and backticks
|
|
637
|
-
lines << "INSERT IGNORE INTO #{table_quote} (version) VALUES"
|
|
638
|
-
lines << versions.map { |v| "('#{v}')" }.join(",\n")
|
|
639
|
-
lines << ';'
|
|
640
|
-
else
|
|
641
|
-
lines << "INSERT INTO #{table_quote} (version) VALUES"
|
|
642
|
-
lines << versions.map { |v| "('#{v}')" }.join(",\n")
|
|
643
|
-
lines << ';'
|
|
644
|
-
end
|
|
507
|
+
def migrations_insert_prefix
|
|
508
|
+
keyword = adapter.class.name == 'BetterStructureSql::Adapters::MysqlAdapter' ? 'INSERT IGNORE' : 'INSERT'
|
|
509
|
+
"#{keyword} INTO #{quote_table_name('schema_migrations')} (version) VALUES"
|
|
510
|
+
end
|
|
645
511
|
|
|
646
|
-
|
|
512
|
+
def migrations_insert_suffix
|
|
513
|
+
adapter.class.name == 'BetterStructureSql::Adapters::PostgresqlAdapter' ? 'ON CONFLICT DO NOTHING;' : ';'
|
|
647
514
|
end
|
|
648
515
|
|
|
649
516
|
def quote_table_name(table_name)
|
|
@@ -655,18 +522,9 @@ module BetterStructureSql
|
|
|
655
522
|
end
|
|
656
523
|
end
|
|
657
524
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
SchemaVersions.store(
|
|
662
|
-
content: content,
|
|
663
|
-
format_type: 'sql',
|
|
664
|
-
pg_version: pg_version,
|
|
665
|
-
connection: connection
|
|
666
|
-
)
|
|
667
|
-
rescue StandardError => e
|
|
668
|
-
# Log error but don't fail the dump
|
|
669
|
-
warn "Warning: Failed to store schema version: #{e.message}"
|
|
525
|
+
# Stores the schema that was just written; failures propagate so they are never mistaken for success
|
|
526
|
+
def store_schema_version
|
|
527
|
+
SchemaVersions.store_current(connection, config:)
|
|
670
528
|
end
|
|
671
529
|
end
|
|
672
530
|
end
|