better_structure_sql 0.2.1 → 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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -0
  3. data/README.md +218 -686
  4. data/app/CLAUDE.md +12 -0
  5. data/app/controllers/better_structure_sql/application_controller.rb +71 -31
  6. data/app/controllers/better_structure_sql/schema_versions_controller.rb +93 -177
  7. data/app/helpers/better_structure_sql/schema_versions_helper.rb +48 -24
  8. data/app/views/better_structure_sql/schema_versions/index.html.erb +46 -49
  9. data/app/views/better_structure_sql/schema_versions/show.html.erb +80 -126
  10. data/app/views/layouts/better_structure_sql/application.html.erb +27 -18
  11. data/config/routes.rb +1 -1
  12. data/lib/CLAUDE.md +73 -0
  13. data/lib/better_structure_sql/adapters/base_adapter.rb +16 -1
  14. data/lib/better_structure_sql/adapters/mysql_adapter.rb +396 -364
  15. data/lib/better_structure_sql/adapters/postgresql_adapter.rb +574 -478
  16. data/lib/better_structure_sql/adapters/registry.rb +18 -15
  17. data/lib/better_structure_sql/adapters/sqlite_adapter.rb +221 -474
  18. data/lib/better_structure_sql/configuration.rb +9 -2
  19. data/lib/better_structure_sql/creation_order.rb +157 -0
  20. data/lib/better_structure_sql/dependency_resolver.rb +4 -2
  21. data/lib/better_structure_sql/dumper.rb +173 -315
  22. data/lib/better_structure_sql/engine.rb +50 -4
  23. data/lib/better_structure_sql/file_writer.rb +108 -97
  24. data/lib/better_structure_sql/generators/base.rb +31 -4
  25. data/lib/better_structure_sql/generators/comment_generator.rb +16 -12
  26. data/lib/better_structure_sql/generators/domain_generator.rb +8 -6
  27. data/lib/better_structure_sql/generators/extension_generator.rb +7 -2
  28. data/lib/better_structure_sql/generators/foreign_key_generator.rb +14 -4
  29. data/lib/better_structure_sql/generators/index_generator.rb +0 -20
  30. data/lib/better_structure_sql/generators/materialized_view_generator.rb +17 -2
  31. data/lib/better_structure_sql/generators/sequence_generator.rb +13 -5
  32. data/lib/better_structure_sql/generators/table_generator.rb +164 -47
  33. data/lib/better_structure_sql/generators/type_generator.rb +25 -14
  34. data/lib/better_structure_sql/generators/view_generator.rb +12 -8
  35. data/lib/better_structure_sql/introspection/extensions.rb +1 -14
  36. data/lib/better_structure_sql/introspection/foreign_keys.rb +1 -14
  37. data/lib/better_structure_sql/introspection/functions.rb +1 -14
  38. data/lib/better_structure_sql/introspection/indexes.rb +1 -14
  39. data/lib/better_structure_sql/introspection/sequences.rb +1 -14
  40. data/lib/better_structure_sql/introspection/tables.rb +1 -14
  41. data/lib/better_structure_sql/introspection/triggers.rb +1 -14
  42. data/lib/better_structure_sql/introspection/types.rb +1 -14
  43. data/lib/better_structure_sql/introspection/views.rb +2 -19
  44. data/lib/better_structure_sql/introspection.rb +12 -1
  45. data/lib/better_structure_sql/migration_patch.rb +39 -139
  46. data/lib/better_structure_sql/railtie.rb +53 -56
  47. data/lib/better_structure_sql/schema_loader.rb +107 -86
  48. data/lib/better_structure_sql/schema_version.rb +3 -2
  49. data/lib/better_structure_sql/schema_versions.rb +70 -71
  50. data/lib/better_structure_sql/version.rb +1 -1
  51. data/lib/better_structure_sql/zip_generator.rb +43 -6
  52. data/lib/better_structure_sql.rb +2 -1
  53. data/lib/generators/better_structure_sql/migration_generator.rb +1 -1
  54. data/lib/generators/better_structure_sql/templates/README +17 -34
  55. data/lib/tasks/better_structure_sql.rake +19 -40
  56. metadata +10 -148
  57. data/config/database.yml +0 -3
@@ -1,51 +1,53 @@
1
+ <% content_for :title, 'Schema Versions' %>
1
2
  <div class="row">
2
3
  <div class="col-12">
3
4
  <div class="d-flex justify-content-between align-items-center mb-4">
4
5
  <h1 class="h2">
5
- <i class="bi bi-database-fill-gear text-primary"></i>
6
+ <i class="bi bi-database-fill-gear text-primary" aria-hidden="true"></i>
6
7
  Schema Versions
7
8
  </h1>
8
- <div class="text-muted">
9
- <i class="bi bi-info-circle"></i>
10
- Total: <%= @schema_versions.count %>
9
+ <div class="text-body-secondary">
10
+ <i class="bi bi-info-circle" aria-hidden="true"></i>
11
+ Total: <%= number_with_delimiter(@total_count) %>
11
12
  </div>
12
13
  </div>
13
14
 
14
15
  <% if @schema_versions.empty? %>
15
16
  <div class="empty-state">
16
- <i class="bi bi-inbox" style="font-size: 4rem;"></i>
17
- <h3 class="mt-3">No Schema Versions Yet</h3>
18
- <p class="text-muted">
17
+ <i class="bi bi-inbox empty-state-icon" aria-hidden="true"></i>
18
+ <h2 class="h3 mt-3">No Schema Versions Yet</h2>
19
+ <p>
19
20
  Schema versions will appear here after running:<br>
20
- <code>rails db:schema:dump</code> (with versioning enabled)
21
+ <code>rails db:schema:store</code> (with <code>enable_schema_versions</code> on)
21
22
  </p>
22
23
  </div>
23
24
  <% else %>
24
25
  <div class="card shadow-sm">
25
26
  <div class="table-responsive">
26
27
  <table class="table table-hover table-striped mb-0">
28
+ <caption class="visually-hidden">Stored schema versions, newest first</caption>
27
29
  <thead class="table-light">
28
30
  <tr>
29
- <th scope="col" class="text-center" style="width: 80px;">ID</th>
30
- <th scope="col" style="width: 120px;">Hash</th>
31
- <th scope="col" style="width: 120px;">Format</th>
32
- <th scope="col" style="width: 150px;">Mode</th>
33
- <th scope="col" style="width: 180px;">PostgreSQL</th>
31
+ <th scope="col" class="text-center">ID</th>
32
+ <th scope="col">Hash</th>
33
+ <th scope="col">Format</th>
34
+ <th scope="col">Mode</th>
35
+ <th scope="col">Database</th>
34
36
  <th scope="col">Created At</th>
35
- <th scope="col" style="width: 100px;">Files</th>
36
- <th scope="col" style="width: 120px;">Size</th>
37
- <th scope="col" class="text-end" style="width: 200px;">Actions</th>
37
+ <th scope="col">Files</th>
38
+ <th scope="col">Size</th>
39
+ <th scope="col" class="text-end">Actions</th>
38
40
  </tr>
39
41
  </thead>
40
42
  <tbody>
41
43
  <% @schema_versions.each do |version| %>
42
44
  <tr>
43
- <td class="text-center fw-bold text-muted">
45
+ <th scope="row" class="text-center">
44
46
  #<%= version.id %>
45
- </td>
47
+ </th>
46
48
  <td>
47
- <code class="text-muted small" title="<%= version.content_hash %>">
48
- <%= version.content_hash[0..7] %>
49
+ <code class="text-body-secondary small" title="<%= version.content_hash %>">
50
+ <%= version.content_hash.to_s[0..7] %>
49
51
  </code>
50
52
  </td>
51
53
  <td>
@@ -55,45 +57,35 @@
55
57
  <%= format_output_mode(version.output_mode) %>
56
58
  </td>
57
59
  <td>
58
- <span class="text-muted">
59
- <i class="bi bi-server"></i>
60
- <%= version.pg_version %>
61
- </span>
60
+ <i class="bi bi-server" aria-hidden="true"></i>
61
+ <%= version.pg_version %>
62
62
  </td>
63
63
  <td>
64
- <i class="bi bi-calendar-event"></i>
65
- <%= version.created_at.strftime('%Y-%m-%d %H:%M:%S') %>
66
- <small class="text-muted d-block">
64
+ <i class="bi bi-calendar-event" aria-hidden="true"></i>
65
+ <%= schema_version_timestamp(version) %>
66
+ <small class="text-body-secondary d-block">
67
67
  <%= time_ago_in_words(version.created_at) %> ago
68
68
  </small>
69
69
  </td>
70
70
  <td>
71
- <% if version.file_count %>
72
- <span class="badge bg-light text-dark">
73
- <%= version.file_count %> files
74
- </span>
75
- <% else %>
76
- <span class="text-muted">1 file</span>
77
- <% end %>
71
+ <%= pluralize(version.file_count || 1, 'file') %>
78
72
  </td>
79
73
  <td>
80
- <span class="badge bg-secondary">
81
- <%= version.formatted_size %>
82
- </span>
74
+ <%= version.formatted_size %>
83
75
  </td>
84
76
  <td class="text-end">
85
- <div class="btn-group btn-group-sm" role="group">
86
- <a href="<%= better_structure_sql.schema_version_path(version) %>"
77
+ <div class="btn-group btn-group-sm" role="group" aria-label="Actions for version #<%= version.id %>">
78
+ <a href="<%= schema_version_path(version) %>"
87
79
  class="btn btn-outline-primary"
88
- title="View details">
89
- <i class="bi bi-eye"></i>
80
+ aria-label="View version #<%= version.id %>">
81
+ <i class="bi bi-eye" aria-hidden="true"></i>
90
82
  View
91
83
  </a>
92
- <a href="<%= better_structure_sql.download_schema_version_path(version) %>"
84
+ <a href="<%= download_schema_version_path(version) %>"
93
85
  class="btn btn-outline-success"
94
- title="Download"
86
+ aria-label="Download version #<%= version.id %>"
95
87
  data-turbo="false">
96
- <i class="bi bi-download"></i>
88
+ <i class="bi bi-download" aria-hidden="true"></i>
97
89
  Download
98
90
  </a>
99
91
  </div>
@@ -105,11 +97,16 @@
105
97
  </div>
106
98
  </div>
107
99
 
108
- <% if @schema_versions.count >= 100 %>
109
- <div class="alert alert-info mt-3">
110
- <i class="bi bi-info-circle"></i>
111
- Showing the 100 most recent versions. Older versions are still stored in the database.
112
- </div>
100
+ <% if @total_pages > 1 %>
101
+ <nav class="mt-3" aria-label="Schema versions pages">
102
+ <ul class="pagination justify-content-center mb-0">
103
+ <%= pagination_item('Newer', @page - 1, enabled: @page > 1) %>
104
+ <li class="page-item active" aria-current="page">
105
+ <span class="page-link">Page <%= @page %> of <%= @total_pages %></span>
106
+ </li>
107
+ <%= pagination_item('Older', @page + 1, enabled: @page < @total_pages) %>
108
+ </ul>
109
+ </nav>
113
110
  <% end %>
114
111
  <% end %>
115
112
  </div>
@@ -1,13 +1,14 @@
1
+ <% content_for :title, "Schema Version ##{@schema_version.id}" %>
1
2
  <div class="row">
2
3
  <div class="col-12">
3
4
  <!-- Header with Back Button -->
4
5
  <div class="d-flex justify-content-between align-items-center mb-4">
5
6
  <h1 class="h2">
6
- <i class="bi bi-file-earmark-code text-primary"></i>
7
+ <i class="bi bi-file-earmark-code text-primary" aria-hidden="true"></i>
7
8
  Schema Version #<%= @schema_version.id %>
8
9
  </h1>
9
- <a href="<%= better_structure_sql.schema_versions_path %>" class="btn btn-outline-secondary">
10
- <i class="bi bi-arrow-left"></i>
10
+ <a href="<%= schema_versions_path %>" class="btn btn-outline-secondary">
11
+ <i class="bi bi-arrow-left" aria-hidden="true"></i>
11
12
  Back to List
12
13
  </a>
13
14
  </div>
@@ -15,157 +16,111 @@
15
16
  <!-- Metadata Card -->
16
17
  <div class="card metadata-card shadow-sm mb-4">
17
18
  <div class="card-body">
18
- <div class="row">
19
- <div class="col-md-3">
20
- <h6 class="text-muted mb-1">
21
- <i class="bi bi-hash"></i>
22
- Version ID
23
- </h6>
24
- <p class="mb-0 fw-bold">#<%= @schema_version.id %></p>
19
+ <h2 class="visually-hidden">Details</h2>
20
+ <dl class="row mb-0">
21
+ <div class="col-md-3 mb-3">
22
+ <dt><i class="bi bi-hash" aria-hidden="true"></i> Version ID</dt>
23
+ <dd class="mb-0 fw-bold">#<%= @schema_version.id %></dd>
25
24
  </div>
26
- <div class="col-md-3">
27
- <h6 class="text-muted mb-1">
28
- <i class="bi bi-filetype-sql"></i>
29
- Format
30
- </h6>
31
- <p class="mb-0">
32
- <%= format_type_badge(@schema_version.format_type) %>
33
- </p>
25
+ <div class="col-md-3 mb-3">
26
+ <dt><i class="bi bi-filetype-sql" aria-hidden="true"></i> Format</dt>
27
+ <dd class="mb-0"><%= format_type_badge(@schema_version.format_type) %></dd>
34
28
  </div>
35
- <div class="col-md-3">
36
- <h6 class="text-muted mb-1">
37
- <i class="bi bi-folder"></i>
38
- Output Mode
39
- </h6>
40
- <p class="mb-0">
41
- <%= format_output_mode(@schema_version.output_mode) %>
42
- </p>
29
+ <div class="col-md-3 mb-3">
30
+ <dt><i class="bi bi-folder" aria-hidden="true"></i> Output Mode</dt>
31
+ <dd class="mb-0"><%= format_output_mode(@schema_version.output_mode) %></dd>
43
32
  </div>
44
- <div class="col-md-3">
45
- <h6 class="text-muted mb-1">
46
- <i class="bi bi-server"></i>
47
- PostgreSQL Version
48
- </h6>
49
- <p class="mb-0"><%= @schema_version.pg_version %></p>
33
+ <div class="col-md-3 mb-3">
34
+ <dt><i class="bi bi-server" aria-hidden="true"></i> Database Version</dt>
35
+ <dd class="mb-0"><%= @schema_version.pg_version %></dd>
50
36
  </div>
51
- </div>
52
-
53
- <div class="row mt-3">
54
- <div class="col-md-3">
55
- <h6 class="text-muted mb-1">
56
- <i class="bi bi-calendar-event"></i>
57
- Created
58
- </h6>
59
- <p class="mb-0">
60
- <%= @schema_version.created_at.strftime('%Y-%m-%d %H:%M:%S') %>
61
- <small class="text-muted d-block">
37
+ <div class="col-md-3 mb-3">
38
+ <dt><i class="bi bi-calendar-event" aria-hidden="true"></i> Created</dt>
39
+ <dd class="mb-0">
40
+ <%= schema_version_timestamp(@schema_version) %>
41
+ <small class="text-body-secondary d-block">
62
42
  <%= time_ago_in_words(@schema_version.created_at) %> ago
63
43
  </small>
64
- </p>
44
+ </dd>
65
45
  </div>
66
- <div class="col-md-3">
67
- <h6 class="text-muted mb-1">
68
- <i class="bi bi-file-earmark-bar-graph"></i>
69
- Size
70
- </h6>
71
- <p class="mb-0">
72
- <span class="badge bg-secondary"><%= @schema_version.formatted_size %></span>
73
- </p>
46
+ <div class="col-md-3 mb-3">
47
+ <dt><i class="bi bi-file-earmark-bar-graph" aria-hidden="true"></i> Size</dt>
48
+ <dd class="mb-0"><%= @schema_version.formatted_size %></dd>
74
49
  </div>
75
- <div class="col-md-3">
76
- <h6 class="text-muted mb-1">
77
- <i class="bi bi-info-circle"></i>
78
- Lines
79
- </h6>
80
- <p class="mb-0"><%= number_with_delimiter(@schema_version.line_count) %> lines</p>
50
+ <div class="col-md-3 mb-3">
51
+ <dt><i class="bi bi-info-circle" aria-hidden="true"></i> Lines</dt>
52
+ <dd class="mb-0"><%= number_with_delimiter(@schema_version.line_count) %> lines</dd>
81
53
  </div>
82
- <div class="col-md-3">
83
- <h6 class="text-muted mb-1">
84
- <i class="bi bi-files"></i>
85
- Files
86
- </h6>
87
- <p class="mb-0">
88
- <% if @schema_version.file_count %>
89
- <span class="badge bg-light text-dark"><%= @schema_version.file_count %> files</span>
90
- <% else %>
91
- <span class="text-muted">1 file</span>
92
- <% end %>
93
- </p>
54
+ <div class="col-md-3 mb-3">
55
+ <dt><i class="bi bi-files" aria-hidden="true"></i> Files</dt>
56
+ <dd class="mb-0"><%= pluralize(@schema_version.file_count || 1, 'file') %></dd>
94
57
  </div>
95
- </div>
96
-
97
- <div class="row mt-3">
98
58
  <div class="col-md-12">
99
- <h6 class="text-muted mb-1">
100
- <i class="bi bi-fingerprint"></i>
101
- Content Hash (MD5)
102
- </h6>
103
- <p class="mb-0">
104
- <code class="text-muted"><%= @schema_version.content_hash %></code>
105
- </p>
59
+ <dt><i class="bi bi-fingerprint" aria-hidden="true"></i> Content Hash (MD5)</dt>
60
+ <dd class="mb-0"><code class="text-body-secondary"><%= @schema_version.content_hash %></code></dd>
106
61
  </div>
107
- </div>
62
+ </dl>
108
63
  </div>
109
64
  </div>
110
65
 
111
66
  <!-- Actions -->
112
67
  <div class="mb-3">
113
- <a href="<%= better_structure_sql.download_schema_version_path(@schema_version) %>"
68
+ <a href="<%= download_schema_version_path(@schema_version) %>"
114
69
  class="btn btn-primary btn-lg"
115
70
  data-turbo="false">
116
71
  <% if @schema_version.multi_file? %>
117
- <i class="bi bi-file-zip"></i>
72
+ <i class="bi bi-file-zip" aria-hidden="true"></i>
118
73
  Download ZIP Archive
119
74
  <% else %>
120
- <i class="bi bi-download"></i>
75
+ <i class="bi bi-download" aria-hidden="true"></i>
121
76
  Download File
122
77
  <% end %>
123
78
  </a>
124
- <% if @schema_version.respond_to?(:content) && @schema_version.content.present? %>
125
- <button type="button" class="btn btn-outline-secondary btn-lg" onclick="copyToClipboard()">
126
- <i class="bi bi-clipboard"></i>
79
+ <% if @schema_version.content.present? %>
80
+ <button type="button" class="btn btn-outline-secondary btn-lg" data-copy-target="schema-content">
81
+ <i class="bi bi-clipboard" aria-hidden="true"></i>
127
82
  Copy to Clipboard
128
83
  </button>
84
+ <span class="visually-hidden" role="status" id="copy-status"></span>
129
85
  <% end %>
130
86
  </div>
131
87
 
132
88
  <!-- Multi-file Info -->
133
- <% if @schema_version.multi_file? && @manifest %>
89
+ <% if @schema_version.multi_file? %>
134
90
  <div class="alert alert-info mb-4">
135
- <h6 class="alert-heading">
136
- <i class="bi bi-info-circle"></i>
91
+ <h2 class="alert-heading h6">
92
+ <i class="bi bi-info-circle" aria-hidden="true"></i>
137
93
  Multi-File Schema
138
- </h6>
139
- <p class="mb-2">This schema was generated in multi-file format with <%= @schema_version.file_count %> files across organized directories.</p>
140
- <% if @manifest['directories'] %>
141
- <p class="mb-0 small">
142
- <strong>Directories:</strong>
143
- <%= @manifest['directories'].keys.sort.join(', ') %>
144
- </p>
145
- <% end %>
94
+ </h2>
95
+ <p class="mb-0">
96
+ This schema was generated in multi-file format with <%= pluralize(@schema_version.file_count || 0, 'file') %> across organized directories.
97
+ Download the ZIP archive to explore the directory structure and individual files.
98
+ </p>
146
99
  </div>
147
100
  <% end %>
148
101
 
149
102
  <!-- Schema Content -->
150
103
  <div class="card shadow-sm">
151
104
  <div class="card-header bg-light">
152
- <h5 class="card-title mb-0">
153
- <i class="bi bi-code-square"></i>
105
+ <h2 class="card-title h5 mb-0" id="schema-content-heading">
106
+ <i class="bi bi-code-square" aria-hidden="true"></i>
154
107
  Schema Content
155
108
  <% if @schema_version.multi_file? %>
156
- <small class="text-muted">(combined from all files)</small>
109
+ <small class="text-body-secondary">(combined from all files)</small>
157
110
  <% end %>
158
- </h5>
111
+ </h2>
159
112
  </div>
160
113
  <div class="card-body p-0">
161
- <% if @schema_version.respond_to?(:content) && @schema_version.content.present? %>
162
- <pre class="code-block m-0" id="schema-content"><%= @schema_version.content %></pre>
114
+ <% if @schema_version.content.present? %>
115
+ <pre class="code-block m-0" id="schema-content" tabindex="0" role="region" aria-labelledby="schema-content-heading"><%= @schema_version.content %></pre>
163
116
  <% else %>
164
117
  <div class="alert alert-warning m-3">
165
- <i class="bi bi-exclamation-triangle"></i>
166
- <strong>File too large to display</strong>
118
+ <i class="bi bi-exclamation-triangle" aria-hidden="true"></i>
119
+ <strong>Content not loaded</strong>
167
120
  <p class="mb-0 mt-2">
168
- This schema file is too large to display in the browser (limit: 1 MB).
121
+ This schema file is too large to display in the browser
122
+ (limit: <%= number_to_human_size(BetterStructureSql::SchemaVersionsController::MAX_DISPLAY_SIZE) %>,
123
+ actual size: <%= number_to_human_size(@schema_version.content_size) %>).
169
124
  Please use the "Download" button above to download and view it locally.
170
125
  </p>
171
126
  </div>
@@ -175,24 +130,23 @@
175
130
  </div>
176
131
  </div>
177
132
 
178
- <script>
179
- function copyToClipboard() {
180
- const content = document.getElementById('schema-content').textContent;
181
- navigator.clipboard.writeText(content).then(function() {
182
- // Show success feedback
183
- const btn = event.target.closest('button');
184
- const originalHTML = btn.innerHTML;
185
- btn.innerHTML = '<i class="bi bi-check-lg"></i> Copied!';
186
- btn.classList.remove('btn-outline-secondary');
187
- btn.classList.add('btn-success');
133
+ <% if @schema_version.content.present? %>
134
+ <%= javascript_tag nonce: true do %>
135
+ document.querySelector('[data-copy-target]').addEventListener('click', function (event) {
136
+ const button = event.currentTarget;
137
+ const status = document.getElementById('copy-status');
138
+ const content = document.getElementById(button.dataset.copyTarget).textContent;
188
139
 
189
- setTimeout(function() {
190
- btn.innerHTML = originalHTML;
191
- btn.classList.remove('btn-success');
192
- btn.classList.add('btn-outline-secondary');
193
- }, 2000);
194
- }, function(err) {
195
- alert('Failed to copy to clipboard: ' + err);
140
+ navigator.clipboard.writeText(content).then(function () {
141
+ status.textContent = 'Schema copied to clipboard';
142
+ button.classList.replace('btn-outline-secondary', 'btn-success');
143
+ setTimeout(function () {
144
+ status.textContent = '';
145
+ button.classList.replace('btn-success', 'btn-outline-secondary');
146
+ }, 2000);
147
+ }, function (error) {
148
+ status.textContent = 'Failed to copy to clipboard: ' + error;
149
+ });
196
150
  });
197
- }
198
- </script>
151
+ <% end %>
152
+ <% end %>
@@ -3,15 +3,16 @@
3
3
  <head>
4
4
  <meta charset="utf-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1">
6
- <title>Schema Versions - BetterStructureSql</title>
6
+ <meta name="robots" content="noindex, nofollow">
7
+ <title><%= content_for?(:title) ? "#{content_for(:title)} - BetterStructureSql" : 'Schema Versions - BetterStructureSql' %></title>
7
8
 
8
9
  <!-- Bootstrap 5.3 CSS from CDN -->
9
10
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
10
11
 
11
12
  <!-- Bootstrap Icons from CDN -->
12
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.2/font/bootstrap-icons.min.css">
13
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.2/font/bootstrap-icons.min.css" rel="stylesheet" integrity="sha384-c9MVH4yRDZMY+bSlECVISp9U4xBl1dKb5z4x8IgF6lBKTHsh1AtxHBfHiiA+S/Nr" crossorigin="anonymous">
13
14
 
14
- <style>
15
+ <%= content_tag(:style, nonce: content_security_policy_nonce) do %>
15
16
  .code-block {
16
17
  background: #f8f9fa;
17
18
  border: 1px solid #dee2e6;
@@ -28,6 +29,11 @@
28
29
  border-left: 4px solid #0d6efd;
29
30
  }
30
31
 
32
+ .metadata-card dt {
33
+ font-weight: 500;
34
+ color: #495057;
35
+ }
36
+
31
37
  .navbar-brand {
32
38
  font-weight: 600;
33
39
  }
@@ -35,30 +41,33 @@
35
41
  .empty-state {
36
42
  padding: 4rem 2rem;
37
43
  text-align: center;
38
- color: #6c757d;
44
+ color: #495057;
39
45
  }
40
46
 
41
- .badge-format {
42
- font-weight: 500;
47
+ .empty-state-icon {
48
+ font-size: 4rem;
43
49
  }
44
- </style>
50
+ <% end %>
45
51
  </head>
46
52
  <body>
53
+ <a class="visually-hidden-focusable" href="#main-content">Skip to main content</a>
54
+
47
55
  <!-- Navigation -->
48
- <nav class="navbar navbar-expand-lg navbar-dark bg-primary mb-4">
56
+ <nav class="navbar navbar-expand-lg navbar-dark bg-primary mb-4" aria-label="BetterStructureSql">
49
57
  <div class="container-fluid">
50
- <a class="navbar-brand" href="<%= better_structure_sql.schema_versions_path %>">
51
- <i class="bi bi-database"></i>
58
+ <a class="navbar-brand" href="<%= schema_versions_path %>">
59
+ <i class="bi bi-database" aria-hidden="true"></i>
52
60
  BetterStructureSql
53
61
  </a>
54
- <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
62
+ <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
63
+ aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
55
64
  <span class="navbar-toggler-icon"></span>
56
65
  </button>
57
66
  <div class="collapse navbar-collapse" id="navbarNav">
58
67
  <ul class="navbar-nav ms-auto">
59
68
  <li class="nav-item">
60
- <a class="nav-link" href="<%= better_structure_sql.schema_versions_path %>">
61
- <i class="bi bi-list-ul"></i>
69
+ <a class="nav-link" href="<%= schema_versions_path %>">
70
+ <i class="bi bi-list-ul" aria-hidden="true"></i>
62
71
  All Versions
63
72
  </a>
64
73
  </li>
@@ -72,29 +81,29 @@
72
81
  <% if flash[:notice] %>
73
82
  <div class="alert alert-success alert-dismissible fade show" role="alert">
74
83
  <%= flash[:notice] %>
75
- <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
84
+ <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
76
85
  </div>
77
86
  <% end %>
78
87
 
79
88
  <% if flash[:alert] %>
80
89
  <div class="alert alert-danger alert-dismissible fade show" role="alert">
81
90
  <%= flash[:alert] %>
82
- <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
91
+ <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
83
92
  </div>
84
93
  <% end %>
85
94
  </div>
86
95
 
87
96
  <!-- Main Content -->
88
- <main class="container-fluid">
97
+ <main class="container-fluid" id="main-content">
89
98
  <%= yield %>
90
99
  </main>
91
100
 
92
101
  <!-- Footer -->
93
102
  <footer class="mt-5 py-4 bg-light border-top">
94
- <div class="container-fluid text-center text-muted">
103
+ <div class="container-fluid text-center text-body-secondary">
95
104
  <small>
96
105
  BetterStructureSql Engine &middot;
97
- Clean PostgreSQL schema dumps for Rails
106
+ Clean database schema dumps for Rails
98
107
  </small>
99
108
  </div>
100
109
  </footer>
data/config/routes.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  BetterStructureSql::Engine.routes.draw do
4
- resources :schema_versions, only: %i[index show] do
4
+ resources :schema_versions, only: %i[index show], constraints: { id: /\d+/ } do
5
5
  member do
6
6
  get :raw
7
7
  get :download
data/lib/CLAUDE.md ADDED
@@ -0,0 +1,73 @@
1
+ # lib/ — layers and ownership
2
+
3
+ Root `CLAUDE.md` has the invariants, the commands and the dump order table. This file: who owns what, and the rules that bite while writing code here.
4
+
5
+ Flow of one dumped object: **adapter -> Introspection -> Generator -> Dumper section -> FileWriter**. Adding or extending an object type: skill `add-schema-object`.
6
+
7
+ ## Layer rules
8
+
9
+ | Layer | File(s) | Rule |
10
+ |---|---|---|
11
+ | Entry | `better_structure_sql.rb` | Requires everything eagerly except the three adapters' classes (`Registry` requires them on first use) and `railtie` / `engine` (only when Rails is defined). `BetterStructureSql.configure` / `.configuration` / `.reset_configuration` |
12
+ | Configuration | `better_structure_sql/configuration.rb` | New option: default in `initialize`, check in `validate!` (raise `ConfigurationError` with a fix hint), line in `lib/generators/better_structure_sql/templates/better_structure_sql.rb`, row in `docs/configuration.md` |
13
+ | Registry | `adapters/registry.rb` | Maps `connection.adapter_name` (`postgresql`, `postgis`, `mysql2`, `trilogy`, `sqlite3`) to an adapter; caches per connection object and override. Requires the MySQL / SQLite driver gem lazily and raises `Error` naming the gem. Specs that swap connections call `Registry.clear_cache!` |
14
+ | Adapters | `adapters/base_adapter.rb`, `adapters/{postgresql,mysql,sqlite}_adapter.rb`, `adapters/*_config.rb` | **All catalog SQL lives here.** `fetch_*(connection)` returns arrays of plain hashes with the same keys on every adapter; `supports_*?` states capability |
15
+ | Introspection | `introspection.rb`, `introspection/*.rb` | Facade only: each `fetch_*` delegates to `adapter_for(connection)`. No SQL, no rescue |
16
+ | Generators | `generators/*.rb` | Inherit `Generators::Base`, implement `generate(object)` -> one SQL string. Data in, text out: never a connection, never a query. `TableGenerator` alone also takes the adapter |
17
+ | Dumper | `dumper.rb` | Orchestrates; never writes SQL for an object itself except the header and the `schema_migrations` INSERT |
18
+ | Ordering | `creation_order.rb`, `dependency_resolver.rb` | See below |
19
+ | Formatter | `formatter.rb` | Presentation only (rstrip, collapse blank lines, section spacing). Single-file path only |
20
+ | FileWriter | `file_writer.rb` | `SECTION_DIRECTORIES`, chunking, atomic writes (temp file + rename), removal of stale files from a previous dump |
21
+ | Manifest | `manifest_generator.rb` | `_manifest.json`: totals and per-directory stats. Statistics only; the loader does not need it |
22
+ | Loader | `schema_loader.rb` | Pure-Ruby load of a file or directory: `_header.sql`, then numbered directories sorted by name, files sorted by name. Statement splitting is adapter-specific |
23
+ | ZIP | `zip_generator.rb` | In-memory rubyzip. Extraction enforces `MAX_FILES_IN_ZIP`, `MAX_UNCOMPRESSED_SIZE` and `safe_entry_path`. Loosening any of them needs a spec with a hostile archive |
24
+ | Versions | `schema_versions.rb`, `schema_version.rb`, `store_result.rb` | See below |
25
+ | Rails | `railtie.rb`, `migration_patch.rb`, `tasks/better_structure_sql.rake`, `generators/better_structure_sql/` | See below |
26
+ | Engine | `engine.rb` | Access-control registry; controllers in `app/` (`app/CLAUDE.md`) |
27
+
28
+ ## Adapters
29
+
30
+ - One batched query per object type, never one per table. Build a lookup hash in Ruby.
31
+ - Every query ends in a stable `ORDER BY`, or the result is sorted in Ruby. Unordered rows = a dump that differs between runs, which no unit spec catches.
32
+ - Values go through `connection.quote`. Never interpolate a table, schema or column name taken from the database into SQL unquoted.
33
+ - PostgreSQL dumps one schema: `PostgresqlAdapter::DUMPED_SCHEMA` (`public`). Objects owned by an extension are excluded.
34
+ - Partitions are not top-level tables: each is nested under its parent's `:partitions`, so `TableGenerator` emits it after the parent.
35
+ - A capability added to one adapter: add `fetch_*` and `supports_*?` to `base_adapter.rb`, then on the other two either implement it or return `[]` with the flag false. Spec per adapter in `spec/adapters/`.
36
+ - Unsupported feature = empty section. Never SQL that the producing database cannot load.
37
+ - MySQL covers `mysql2`, `trilogy` and MariaDB (`mariadb?` gates version checks). SQLite has no foreign-key section (inline in `CREATE TABLE`) and no sequences, functions, custom types or materialized views.
38
+ - Other agents may be editing `mysql_adapter.rb` / `sqlite_adapter.rb`: run `git status` and `git diff <file>` before touching them.
39
+
40
+ ## Generators
41
+
42
+ - Identifiers: `quote_identifier` (doubles embedded quote characters; backticks on MySQL) and `qualified_identifier` (omits default schemas). Do not hand-roll quotes.
43
+ - Output is byte-stable: no `Hash` iteration whose order comes from the database, no `Time.now`, no version text.
44
+ - The spec asserts the exact statement (`spec/generators/<name>_generator_spec.rb`).
45
+
46
+ ## Dumper and ordering
47
+
48
+ - Two paths, one contract: `dump_single_file` joins `*_section` strings (with `-- Title` comments); `dump_multi_file` builds arrays from `generate_*_section`. Every object type appears in both.
49
+ - `CreationOrder.for(adapter, connection)` runs one `pg_depend` query on PostgreSQL and returns an empty order for other adapters (they keep the fixed order; MySQL orders its views in `fetch_views`). Keys: `relation:` (tables, views, matviews, sequences), `function:`, `type:` (enums, composites, ranges, domains).
50
+ - `type_statements`: custom types and domains that need no routine, one dependency-sorted list (`02_types`). `early_statements`: functions with no relation dependency plus the types that need a routine (a domain CHECK calling a function), dependency-sorted, written before tables (`03_functions`). `relation_dependent_statements`: views + materialized views + the remaining functions, topologically sorted by `DependencyResolver` into one sequence. Single-file output titles each run; multi-file puts the whole sequence in `08_views`.
51
+ - `DependencyResolver#resolve` returns best-effort order on a cycle (it does not raise).
52
+ - `Dumper#dump(store_version: nil)`: `nil` follows `config.enable_schema_versions`; storing failures propagate. Rake and the Rails dump hook pass `store_version: false`; storing is the explicit `db:schema:store`.
53
+ - `sort_tables` sorts by name; `ordered_tables` then defers an inheritance child (`:inherits`) to right after its last parent. SQLite tables are replayed verbatim from `sqlite_master` (`:sql`), foreign keys included.
54
+ - Multi-file skips `07_foreign_keys` on SQLite. Every adapter dumps the `schema_migrations` table and its INSERT; without them a loaded dump leaves every migration pending.
55
+
56
+ ## Schema versions
57
+
58
+ - Table `better_structure_sql_schema_versions` (model `SchemaVersion`). `content_hash` is the MD5 of exactly the stored content.
59
+ - `SchemaVersions.store_current`: read the dump from disk -> hash -> **skip** when the latest version has the same hash -> else store, then retention `cleanup!`. Cleanup runs only after a real store, never on a skip. Returns `StoreResult` (`stored?` / `skipped?`).
60
+ - Multi-file: content is the concatenation in directory order, plus a ZIP of the directory. Storing never modifies the schema on disk.
61
+ - `SchemaVersions.restore(version, db_config:)` (task `db:schema:restore`, needs `CONFIRM=1`): purges the database, loads the version with `SchemaLoader`, records the environment. Stored versions in that database are lost.
62
+ - Changing what is hashed, or when cleanup runs, needs a spec (`spec/integration/schema_version_deduplication_spec.rb`).
63
+
64
+ ## Rails integration
65
+
66
+ - `Railtie` always applies `MigrationPatch`: `check_schema_file`, `schema_sha1` and `structure_load` accept a schema directory. `maintain_test_schema!` calls Rails' own when either `replace_default_*` option is on (Rails then resolves the directory itself); otherwise it loads the directory at `output_path` through `DatabaseTasks.load_schema`, which records the environment and checksum, and checks pending migrations.
67
+ - `replace_default_dump` prepends onto `ActiveRecord::Tasks::DatabaseTasks`: `schema_dump_path` (primary database -> `config.output_path`; other databases keep the Rails path; `SCHEMA=` wins) and `dump_schema`. `replace_default_load` overrides `structure_load` with `SchemaLoader`. Both are skipped when `output_path` ends in `.rb`.
68
+ - These are prepends on Rails internals: a Rails upgrade can break them silently. The integration apps in CI are the check, not the unit specs.
69
+
70
+ ## Errors
71
+
72
+ - Raise a class from `errors.rb`; `SchemaLoader::LoadError` and `ZipGenerator::ZipError` are the local ones.
73
+ - No new blanket rescue. The existing `rescue StandardError` site (`generators/base.rb` adapter detection) is debt, not a pattern to copy.