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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +77 -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 +93 -177
- 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 +80 -126
- 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 +10 -148
- data/config/database.yml +0 -3
data/README.md
CHANGED
|
@@ -1,766 +1,298 @@
|
|
|
1
|
-
|
|
1
|
+
# BetterStructureSql
|
|
2
|
+
|
|
3
|
+
[](https://github.com/sebyx07/better_structure_sql/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/better_structure_sql)
|
|
5
|
+
[](https://rubygems.org/gems/better_structure_sql)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
[](better_structure_sql.gemspec)
|
|
8
|
+
[](https://developerz.ai)
|
|
9
|
+
[](https://sebyx07.github.io/better_structure_sql/)
|
|
10
|
+
|
|
11
|
+
Clean, deterministic `structure.sql` for Rails, written in pure Ruby. No `pg_dump`, `mysqldump` or `sqlite3` binary: the gem reads the catalog over the ActiveRecord connection and writes the same SQL on every machine. PostgreSQL is the primary target; MySQL (`mysql2` and `trilogy` drivers, MariaDB servers) and SQLite are supported and round-trip tested in CI.
|
|
12
|
+
|
|
13
|
+
```diff
|
|
14
|
+
- --
|
|
15
|
+
- -- PostgreSQL database dump
|
|
16
|
+
- --
|
|
17
|
+
- -- Dumped from database version 16.3
|
|
18
|
+
- -- Dumped by pg_dump version 16.4
|
|
19
|
+
-
|
|
20
|
+
- SET statement_timeout = 0;
|
|
21
|
+
- SET lock_timeout = 0;
|
|
22
|
+
- SET idle_in_transaction_session_timeout = 0;
|
|
23
|
+
- SELECT pg_catalog.set_config('search_path', '', false);
|
|
24
|
+
- SET check_function_bodies = false;
|
|
25
|
+
- SET xmloption = content;
|
|
26
|
+
- SET client_min_messages = warning;
|
|
27
|
+
- SET row_security = off;
|
|
28
|
+
-
|
|
29
|
+
- --
|
|
30
|
+
- -- Name: categories; Type: TABLE; Schema: public; Owner: -
|
|
31
|
+
- --
|
|
32
|
+
-
|
|
33
|
+
- CREATE TABLE public.categories (
|
|
34
|
+
- id bigint NOT NULL,
|
|
35
|
+
- name character varying NOT NULL,
|
|
36
|
+
- ...
|
|
37
|
+
+ SET client_encoding = 'UTF8';
|
|
38
|
+
+ SET standard_conforming_strings = on;
|
|
39
|
+
+
|
|
40
|
+
+ -- Extensions
|
|
41
|
+
+ CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
|
42
|
+
+
|
|
43
|
+
+ SET search_path TO "$user", public;
|
|
44
|
+
+
|
|
45
|
+
+ -- Tables
|
|
46
|
+
+ CREATE TABLE IF NOT EXISTS "categories" (
|
|
47
|
+
+ "id" bigint NOT NULL DEFAULT nextval('categories_id_seq'::regclass),
|
|
48
|
+
+ "name" varchar NOT NULL,
|
|
49
|
+
+ "parent_id" integer,
|
|
50
|
+
+ PRIMARY KEY ("id")
|
|
51
|
+
+ );
|
|
52
|
+
+
|
|
53
|
+
+ -- Indexes
|
|
54
|
+
+ CREATE UNIQUE INDEX index_categories_on_slug ON public.categories USING btree (slug);
|
|
55
|
+
+
|
|
56
|
+
+ -- Foreign Keys
|
|
57
|
+
+ ALTER TABLE "categories" ADD CONSTRAINT "fk_rails_82f48f7407" FOREIGN KEY (parent_id) REFERENCES categories(id) ON DELETE CASCADE;
|
|
58
|
+
```
|
|
2
59
|
|
|
3
|
-
|
|
60
|
+
## Why
|
|
4
61
|
|
|
5
|
-
|
|
6
|
-
|
|
62
|
+
| Problem with the stock dump | What this gem does |
|
|
63
|
+
|---|---|
|
|
64
|
+
| Tool and server versions leak into the file, so every upgrade is a diff | No version comments, a fixed header, sorted objects |
|
|
65
|
+
| Needs the database client binary on every developer machine and in CI | Pure Ruby over the ActiveRecord connection |
|
|
66
|
+
| One file with tens of thousands of lines | Optional [multi-file output](#multi-file-output): numbered directories, ~500-line files |
|
|
67
|
+
| No record of what the schema looked like at a deploy | Optional [schema versions](#schema-versions-and-web-ui) stored in the database, browsable in a mounted engine |
|
|
7
68
|
|
|
8
|
-
|
|
9
|
-
[](https://opensource.org/licenses/MIT)
|
|
10
|
-
[](https://www.ruby-lang.org/)
|
|
11
|
-
[](https://rubyonrails.org/)
|
|
12
|
-
|
|
13
|
-
**[๐ Documentation](https://sebyx07.github.io/better_structure_sql/)** โข **[๐ GitHub](https://github.com/sebyx07/better_structure_sql)** โข **[๐ RubyGems](https://rubygems.org/gems/better_structure_sql)**
|
|
14
|
-
|
|
15
|
-
---
|
|
16
|
-
|
|
17
|
-
</div>
|
|
18
|
-
|
|
19
|
-
> **โ ๏ธ Beta Notice**: Version 0.2.1 is feature-complete and production-ready for **PostgreSQL**. Multi-database support (MySQL, SQLite) is implemented but considered experimental. APIs are stable but may see minor refinements before v1.0. We welcome feedback and contributions!
|
|
20
|
-
|
|
21
|
-
## โจ Why BetterStructureSql?
|
|
22
|
-
|
|
23
|
-
Rails' database dump tools (`pg_dump`, `mysqldump`, etc.) create noisy `structure.sql` files with version-specific comments, inconsistent formatting, and metadata that pollutes git diffs.
|
|
24
|
-
|
|
25
|
-
**BetterStructureSql** uses pure Ruby introspection to generate clean schema files:
|
|
26
|
-
|
|
27
|
-
<table>
|
|
28
|
-
<tr>
|
|
29
|
-
<td width="50%">
|
|
30
|
-
|
|
31
|
-
### ๐ฏ Core Benefits
|
|
32
|
-
|
|
33
|
-
- โ
**Clean diffs** - Only actual schema changes
|
|
34
|
-
- โ
**No external tools** - Pure Ruby introspection
|
|
35
|
-
- โ
**Multi-database** - PostgreSQL, MySQL, SQLite
|
|
36
|
-
- โ
**Deterministic** - Same input = identical output
|
|
37
|
-
|
|
38
|
-
</td>
|
|
39
|
-
<td width="50%">
|
|
40
|
-
|
|
41
|
-
### ๐ Advanced Features
|
|
42
|
-
|
|
43
|
-
- โ
**Complete coverage** - Tables, views, triggers, functions
|
|
44
|
-
- โ
**Schema versioning** - Store & retrieve versions
|
|
45
|
-
- โ
**Multi-file output** - Handle massive schemas
|
|
46
|
-
- โ
**Rails integration** - Drop-in replacement
|
|
47
|
-
|
|
48
|
-
</td>
|
|
49
|
-
</tr>
|
|
50
|
-
</table>
|
|
51
|
-
|
|
52
|
-
---
|
|
53
|
-
|
|
54
|
-
## ๐๏ธ Database Support
|
|
55
|
-
|
|
56
|
-
| Feature | PostgreSQL 12+ | MySQL 8.0+ | SQLite 3.35+ |
|
|
57
|
-
|---------|----------------|------------|--------------|
|
|
58
|
-
| **Tables & Columns** | โ
Full | โ
Full | โ
Full |
|
|
59
|
-
| **Indexes** | โ
btree, gin, gist, hash, brin | โ
btree, hash, fulltext | โ
btree |
|
|
60
|
-
| **Foreign Keys** | โ
All actions | โ
All actions | โ
Inline with CREATE TABLE |
|
|
61
|
-
| **Unique Constraints** | โ
| โ
| โ
|
|
|
62
|
-
| **Check Constraints** | โ
| โ
(8.0.16+) | โ
|
|
|
63
|
-
| **Extensions** | โ
pgcrypto, uuid-ossp, pg_trgm, etc. | โ | โ (PRAGMA settings instead) |
|
|
64
|
-
| **Custom Types (ENUM)** | โ
CREATE TYPE | โ (inline ENUM/SET) | โ (CHECK constraints) |
|
|
65
|
-
| **Sequences** | โ
CREATE SEQUENCE | โ (AUTO_INCREMENT) | โ (AUTOINCREMENT) |
|
|
66
|
-
| **Views** | โ
Regular views | โ
Regular views | โ
Regular views |
|
|
67
|
-
| **Materialized Views** | โ
| โ | โ |
|
|
68
|
-
| **Functions** | โ
plpgsql, sql | โ
Stored procedures | โ |
|
|
69
|
-
| **Triggers** | โ
BEFORE/AFTER/INSTEAD OF | โ
BEFORE/AFTER | โ
BEFORE/AFTER |
|
|
70
|
-
| **Partitioned Tables** | ๐ง Planned | โ | โ |
|
|
71
|
-
| **Domains** | โ
| โ | โ |
|
|
72
|
-
|
|
73
|
-
### Getting Started by Database
|
|
74
|
-
|
|
75
|
-
- **PostgreSQL**: [Installation โ](https://www.postgresql.org/download/) | [Rails Guide โ](https://guides.rubyonrails.org/configuring.html#configuring-a-postgresql-database)
|
|
76
|
-
- **MySQL**: [Installation โ](https://dev.mysql.com/downloads/mysql/) | [Rails Guide โ](https://guides.rubyonrails.org/configuring.html#configuring-a-mysql-or-mariadb-database)
|
|
77
|
-
- **SQLite**: [Installation โ](https://www.sqlite.org/download.html) | [Rails Guide โ](https://guides.rubyonrails.org/configuring.html#configuring-a-sqlite3-database)
|
|
78
|
-
|
|
79
|
-
๐ See [Feature Compatibility Matrix](docs/features/multi-database-adapter-support/README.md#feature-compatibility-matrix) for detailed comparison.
|
|
80
|
-
|
|
81
|
-
## Features
|
|
82
|
-
|
|
83
|
-
### Core Features
|
|
84
|
-
- **Pure Ruby implementation** - No external tool dependencies (pg_dump, mysqldump, sqlite3 CLI)
|
|
85
|
-
- **Multi-database adapter pattern** - Auto-detects database type from ActiveRecord connection
|
|
86
|
-
- **Clean structure.sql** - Only essential schema information
|
|
87
|
-
- **Complete database support**:
|
|
88
|
-
- Tables with all column types and defaults
|
|
89
|
-
- Primary keys, foreign keys, and constraints
|
|
90
|
-
- Indexes (including partial, unique, and expression indexes)
|
|
91
|
-
- Views (and materialized views for PostgreSQL)
|
|
92
|
-
- Functions/stored procedures and triggers (database-dependent)
|
|
93
|
-
- Extensions (PostgreSQL)
|
|
94
|
-
- Sequences (PostgreSQL)
|
|
95
|
-
- Custom types and enums (PostgreSQL, MySQL SET/ENUM)
|
|
96
|
-
|
|
97
|
-
### Multi-File Schema Output (Optional)
|
|
98
|
-
- **Massive schema support** - Designed to handle tens of thousands of database objects
|
|
99
|
-
- **Directory-based output** - Split schema across organized, numbered directories
|
|
100
|
-
- **Smart chunking** - 500 LOC per file (configurable) with intelligent overflow handling
|
|
101
|
-
- **Better git diffs** - See only changed files, not entire schema
|
|
102
|
-
- **ZIP downloads** - Download complete directory structure as archive
|
|
103
|
-
- **Easy navigation** - Find tables quickly in `05_tables/`, triggers in `09_triggers/`, etc.
|
|
104
|
-
|
|
105
|
-
### Schema Versioning (Optional)
|
|
106
|
-
- Store schema versions in database with metadata
|
|
107
|
-
- **Hash-based deduplication** - Automatically skip storing when schema unchanged
|
|
108
|
-
- Track database type and version, format type (SQL/Ruby), creation timestamp
|
|
109
|
-
- ZIP archive storage for multi-file schemas
|
|
110
|
-
- Configurable retention policy (keep last N versions)
|
|
111
|
-
- Browse and download versions via web UI (mountable Rails engine)
|
|
112
|
-
- Works with both `structure.sql` and `schema.rb`
|
|
113
|
-
- Works across all database types (PostgreSQL, MySQL, SQLite)
|
|
114
|
-
- Restore from any stored version
|
|
115
|
-
|
|
116
|
-
### Web UI Engine
|
|
117
|
-
- **Mountable Rails Engine** - Browse schema versions in any Rails app
|
|
118
|
-
- **Bootstrap 5 interface** - No asset compilation required (CDN-based)
|
|
119
|
-
- **View schema versions** - List, view formatted schema, download raw text
|
|
120
|
-
- **Configurable authentication** - Integrate with Devise, Pundit, or custom auth
|
|
121
|
-
- **Developer onboarding** - Easy access to latest schema for new team members
|
|
122
|
-
|
|
123
|
-
### Rails Integration
|
|
124
|
-
- Drop-in replacement: `rake db:schema:dump` โ uses BetterStructureSql (when enabled)
|
|
125
|
-
- Configuration via `config/initializers/better_structure_sql.rb`
|
|
126
|
-
- **Rake Tasks**:
|
|
127
|
-
- `db:schema:dump_better` - Explicitly dump schema using BetterStructureSql
|
|
128
|
-
- `db:schema:load_better` - Load schema (supports both file and directory mode)
|
|
129
|
-
- `db:schema:store` - Store current schema as a version in database
|
|
130
|
-
- `db:schema:versions` - List all stored schema versions
|
|
131
|
-
- `db:schema:cleanup` - Remove old versions based on retention limit
|
|
132
|
-
- `db:schema:restore[VERSION_ID]` - Restore database from specific version
|
|
133
|
-
|
|
134
|
-
### Docker Development Environment
|
|
135
|
-
- **Single command setup** - `docker compose up` for full environment
|
|
136
|
-
- **PostgreSQL included** - No local database installation needed
|
|
137
|
-
- **Live code reloading** - Changes reflect immediately
|
|
138
|
-
- **Integration app** - Test and demo environment included
|
|
139
|
-
|
|
140
|
-
## ๐ Quick Start
|
|
69
|
+
## Quick start
|
|
141
70
|
|
|
142
71
|
```ruby
|
|
143
72
|
# Gemfile
|
|
144
73
|
gem 'better_structure_sql'
|
|
145
|
-
gem 'pg' # For PostgreSQL (or 'mysql2' for MySQL, or 'sqlite3' for SQLite)
|
|
146
74
|
```
|
|
147
75
|
|
|
148
|
-
**Database adapter is auto-detected** from your `ActiveRecord::Base.connection.adapter_name`. No manual configuration needed!
|
|
149
|
-
|
|
150
76
|
```bash
|
|
151
77
|
bundle install
|
|
152
|
-
rails generate better_structure_sql:install
|
|
153
|
-
rails db:schema:dump_better
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
**๐ Your `db/structure.sql` is now clean and maintainable!**
|
|
157
|
-
|
|
158
|
-
## ๐ฆ Schema Versioning with Deduplication
|
|
159
|
-
|
|
160
|
-
BetterStructureSql automatically tracks schema evolution by storing versions in your database. Hash-based deduplication ensures only meaningful schema changes are recorded.
|
|
161
|
-
|
|
162
|
-
### How It Works
|
|
163
|
-
|
|
164
|
-
When you run `rails db:schema:store`, the gem:
|
|
165
|
-
|
|
166
|
-
1. **Reads** your current schema files (single or multi-file)
|
|
167
|
-
2. **Calculates** MD5 hash of the complete schema content
|
|
168
|
-
3. **Compares** with the most recent stored version's hash
|
|
169
|
-
4. **Skips storage** if hash matches (no changes detected) โจ
|
|
170
|
-
5. **Creates new version** if hash differs (schema changed)
|
|
171
|
-
|
|
172
|
-
### Quick Example
|
|
173
|
-
|
|
174
|
-
```bash
|
|
175
|
-
# After migrations, dump and store schema
|
|
78
|
+
rails generate better_structure_sql:install # initializer + schema versions migration
|
|
176
79
|
rails db:migrate
|
|
177
|
-
rails db:schema:dump_better
|
|
178
|
-
rails db:schema:store
|
|
179
|
-
|
|
180
|
-
# First run (no previous version)
|
|
181
|
-
# =>
|
|
182
|
-
# Stored schema version #1
|
|
183
|
-
# Format: sql
|
|
184
|
-
# Mode: single_file
|
|
185
|
-
# PostgreSQL: 15.4
|
|
186
|
-
# Size: 45.2 KB
|
|
187
|
-
# Hash: a3f5c9d2e8b1f4a6c7e9d3f1b5a8c2e4
|
|
188
|
-
# Total versions: 1
|
|
189
|
-
|
|
190
|
-
# Second run (no schema changes)
|
|
191
|
-
# =>
|
|
192
|
-
# No schema changes detected
|
|
193
|
-
# Current schema matches version #1
|
|
194
|
-
# Hash: a3f5c9d2e8b1f4a6c7e9d3f1b5a8c2e4
|
|
195
|
-
# No new version stored
|
|
196
|
-
# Total versions: 1
|
|
197
|
-
|
|
198
|
-
# After adding a table
|
|
199
|
-
rails db:migrate # Adds new table
|
|
200
|
-
rails db:schema:dump_better
|
|
201
|
-
rails db:schema:store
|
|
202
|
-
|
|
203
|
-
# =>
|
|
204
|
-
# Stored schema version #2
|
|
205
|
-
# Format: sql
|
|
206
|
-
# Mode: single_file
|
|
207
|
-
# PostgreSQL: 15.4
|
|
208
|
-
# Size: 48.7 KB
|
|
209
|
-
# Hash: b7e2d1c4f9a6c3e5d8b2f1a4c9e7d3b6
|
|
210
|
-
# Total versions: 2
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
### Production Workflow
|
|
214
|
-
|
|
215
|
-
Perfect for deployment automation:
|
|
216
|
-
|
|
217
|
-
```ruby
|
|
218
|
-
# config/deploy.rb or GitHub Actions
|
|
219
|
-
namespace :deploy do
|
|
220
|
-
task :update_schema do
|
|
221
|
-
# Run migrations (may be zero)
|
|
222
|
-
# Rails automatically dumps schema after migrations
|
|
223
|
-
execute :rake, 'db:migrate'
|
|
224
|
-
|
|
225
|
-
# Store schema version only if changed (automatic deduplication)
|
|
226
|
-
execute :rake, 'db:schema:store'
|
|
227
|
-
end
|
|
228
|
-
end
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
**Benefits in Production:**
|
|
232
|
-
- โ
Deploys without migrations don't create duplicate versions
|
|
233
|
-
- โ
Developers see clean schema evolution timeline
|
|
234
|
-
- โ
Storage efficient (no duplicate content)
|
|
235
|
-
- โ
Clear audit trail of actual schema changes
|
|
236
|
-
|
|
237
|
-
### Viewing Stored Versions
|
|
238
|
-
|
|
239
|
-
```bash
|
|
240
|
-
# List all versions with hashes
|
|
241
|
-
rails db:schema:versions
|
|
242
|
-
|
|
243
|
-
Schema Versions (3 total)
|
|
244
|
-
|
|
245
|
-
ID | Format | Mode | Files | PostgreSQL | Hash | Created | Size
|
|
246
|
-
-----|--------|-------------|-------|------------|----------|---------------------|-------
|
|
247
|
-
3 | sql | multi_file | 47 | 15.4 | a3f5c9d2 | 2025-01-20 14:30:15 | 125 KB
|
|
248
|
-
2 | sql | single_file | - | 15.4 | b7e2d1c4 | 2025-01-19 10:15:42 | 98 KB
|
|
249
|
-
1 | sql | single_file | - | 15.3 | c9f8a3b2 | 2025-01-18 08:45:30 | 85 KB
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
### Configuration
|
|
253
|
-
|
|
254
|
-
```ruby
|
|
255
|
-
# config/initializers/better_structure_sql.rb
|
|
256
|
-
BetterStructureSql.configure do |config|
|
|
257
|
-
# Enable schema versioning
|
|
258
|
-
config.enable_schema_versions = true
|
|
259
|
-
|
|
260
|
-
# Retain 10 most recent unique versions (0 = unlimited)
|
|
261
|
-
config.schema_versions_limit = 10
|
|
262
|
-
end
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
### Web UI Access
|
|
266
|
-
|
|
267
|
-
Developers can view stored schema versions via the web UI without database access:
|
|
268
|
-
|
|
269
|
-
```ruby
|
|
270
|
-
# config/routes.rb
|
|
271
|
-
authenticate :user, ->(user) { user.admin? } do
|
|
272
|
-
mount BetterStructureSql::Engine, at: '/schema_versions'
|
|
273
|
-
end
|
|
80
|
+
rails db:schema:dump_better # writes db/structure.sql
|
|
274
81
|
```
|
|
275
82
|
|
|
276
|
-
|
|
83
|
+
The database adapter is detected from `ActiveRecord::Base.connection.adapter_name`. The gem depends on `pg`; add `mysql2` (or `trilogy`) or `sqlite3` to your own Gemfile to use those adapters.
|
|
277
84
|
|
|
278
|
-
|
|
85
|
+
`rails generate better_structure_sql:install --skip-migration` skips the schema versions table. To make `rails db:schema:dump` and `rails db:schema:load` use this gem, set `replace_default_dump` and `replace_default_load` in the initializer (see [Configuration](#configuration)). On MySQL and MariaDB a dump loads only through this gem (`replace_default_load` or `db:schema:load_better`): Rails' own load pipes the file through the `mysql` client, which needs `DELIMITER` directives the dump does not contain.
|
|
279
86
|
|
|
280
|
-
##
|
|
87
|
+
## Feature matrix
|
|
281
88
|
|
|
282
|
-
|
|
89
|
+
What the code dumps today. A dash means the adapter returns nothing for that object type. Every column is round-trip tested in CI (dump, load into an empty database, dump again, compare); the servers and drivers are the `integration-*` jobs in [`.github/workflows/ci.yml`](.github/workflows/ci.yml).
|
|
283
90
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
91
|
+
| Object | PostgreSQL | MySQL, MariaDB | SQLite |
|
|
92
|
+
|---|---|---|---|
|
|
93
|
+
| Tables, columns, defaults, primary keys | yes, plus `UNLOGGED`, storage parameters (`WITH (fillfactor=70)`) and inheritance (`INHERITS`, parents first) | yes: server column types verbatim (`int unsigned`, `datetime(6)`, `tinyint(1)`), `AUTO_INCREMENT`, `ON UPDATE`, `ENGINE` / `COLLATE` table options | the original `CREATE TABLE` statement, verbatim (`AUTOINCREMENT`, `WITHOUT ROWID`, `STRICT`, collations, composite primary keys) |
|
|
94
|
+
| Identity and generated columns | yes | generated columns | generated columns |
|
|
95
|
+
| Check and unique constraints | yes, plus exclusion constraints and `NOT VALID` | checks (where the server has them), `NOT ENFORCED` kept; unique as indexes | as written in the table: named and unnamed checks, table-level `UNIQUE` |
|
|
96
|
+
| Partitioned tables | `PARTITION BY` and `PARTITION OF ... FOR VALUES` | - | - |
|
|
97
|
+
| Indexes | full definition (method, partial, expression, unique) | full definition (unique, `FULLTEXT`, `SPATIAL`, prefix lengths, `DESC`, functional key parts, `INVISIBLE`) | original `CREATE INDEX` statement, partial and expression included |
|
|
98
|
+
| Foreign keys | `ALTER TABLE ... ADD CONSTRAINT`: composite, `MATCH FULL`, `DEFERRABLE`, `NOT VALID` | `ALTER TABLE ... ADD CONSTRAINT`, composite included | inline in `CREATE TABLE`, composite included |
|
|
99
|
+
| Views | yes, ordered by dependency | yes, from `SHOW CREATE VIEW` (no database name), ordered by dependency | original `CREATE VIEW` statement |
|
|
100
|
+
| Materialized views | yes, with their indexes | - | - |
|
|
101
|
+
| Functions | functions and procedures | stored procedures and functions | - |
|
|
102
|
+
| Triggers | yes | yes, firing order kept | yes |
|
|
103
|
+
| Extensions | yes | - | in their place, the PRAGMAs `application_id`, `auto_vacuum`, `user_version` when non-zero |
|
|
104
|
+
| Enum, composite and range types | `CREATE TYPE` | inline `enum(...)` / `set(...)` column types | - |
|
|
105
|
+
| Domains | `CREATE DOMAIN` with default, `NOT NULL` and checks | - | - |
|
|
106
|
+
| Sequences | `CREATE SEQUENCE`, `OWNED BY` for serial columns | - | - |
|
|
107
|
+
| Comments | tables, columns, indexes, views, functions, procedures | tables (column comments are written as SQL comments) | - |
|
|
108
|
+
| `schema_migrations` rows | yes | yes | yes |
|
|
287
109
|
|
|
288
|
-
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
See [DOCKER.md](DOCKER.md) for complete Docker documentation.
|
|
292
|
-
|
|
293
|
-
## Documentation ๐
|
|
294
|
-
|
|
295
|
-
### ๐ Documentation Website
|
|
296
|
-
**[Visit the full documentation site โ](https://sebyx07.github.io/better_structure_sql/)**
|
|
297
|
-
|
|
298
|
-
Interactive documentation with tutorials, database-specific guides, and real-world examples showing how to use SQL databases to their fullest with BetterStructureSql. Features include:
|
|
299
|
-
- Step-by-step tutorials for PostgreSQL, MySQL, and SQLite
|
|
300
|
-
- Real-world examples using advanced database features (triggers, views, functions)
|
|
301
|
-
- Production deployment guides with automatic schema versioning
|
|
302
|
-
- API reference and configuration examples
|
|
303
|
-
- AI-friendly multi-file schema benefits
|
|
304
|
-
|
|
305
|
-
### General Documentation
|
|
306
|
-
- [Installation](docs/installation.md) - Setup and configuration
|
|
307
|
-
- [Configuration](docs/configuration.md) - All configuration options
|
|
308
|
-
- [Usage](docs/usage.md) - Rake tasks and examples
|
|
309
|
-
- [Schema Versions](docs/schema_versions.md) - Version storage feature
|
|
310
|
-
- [Troubleshooting](docs/usage.md#troubleshooting) - Common issues and solutions
|
|
311
|
-
- [Multi-File Schema Output](docs/features/multi-file-schema-output/README.md) - Handle massive schemas
|
|
312
|
-
- [Web UI Engine](docs/features/dev-environment-docker-web-ui/README.md) - Browse versions via web interface
|
|
313
|
-
- [Docker Development](DOCKER.md) - Complete Docker environment guide
|
|
314
|
-
- [Testing](docs/testing.md) - RSpec testing guide
|
|
315
|
-
- [MVP Phases](docs/mvp/) - Implementation roadmap
|
|
316
|
-
|
|
317
|
-
### Multi-Database Support
|
|
318
|
-
- [Multi-Database Architecture](docs/features/multi-database-adapter-support/README.md) - Overview and feature matrix
|
|
319
|
-
- [Database Adapters Architecture](docs/features/multi-database-adapter-support/architecture.md) - Technical deep dive
|
|
320
|
-
- [Implementation Phases](docs/features/multi-database-adapter-support/plan/) - Phased rollout plan
|
|
321
|
-
|
|
322
|
-
## ๐ Example Output
|
|
323
|
-
|
|
324
|
-
<table>
|
|
325
|
-
<tr>
|
|
326
|
-
<td width="50%">
|
|
327
|
-
|
|
328
|
-
### โ Before (pg_dump)
|
|
329
|
-
|
|
330
|
-
```sql
|
|
331
|
-
--
|
|
332
|
-
-- PostgreSQL database dump
|
|
333
|
-
--
|
|
334
|
-
|
|
335
|
-
-- Dumped from database version 14.5
|
|
336
|
-
-- Dumped by pg_dump version 14.5
|
|
337
|
-
|
|
338
|
-
SET statement_timeout = 0;
|
|
339
|
-
SET lock_timeout = 0;
|
|
340
|
-
SET idle_in_transaction_session_timeout = 0;
|
|
341
|
-
SET client_encoding = 'UTF8';
|
|
342
|
-
SET standard_conforming_strings = on;
|
|
343
|
-
SELECT pg_catalog.set_config('search_path', '', false);
|
|
344
|
-
SET check_function_bodies = false;
|
|
345
|
-
SET xmloption = content;
|
|
346
|
-
SET client_min_messages = warning;
|
|
347
|
-
SET row_security = off;
|
|
348
|
-
-- ... 50+ more lines ...
|
|
349
|
-
```
|
|
110
|
+
Not supported:
|
|
350
111
|
|
|
351
|
-
|
|
352
|
-
-
|
|
353
|
-
-
|
|
354
|
-
-
|
|
355
|
-
- Hard to review diffs
|
|
112
|
+
- PostgreSQL aggregates, rules, row-level security policies.
|
|
113
|
+
- PostgreSQL schemas other than `public`: every object is read from `public` only. `config.schemas` is validated but not used.
|
|
114
|
+
- Comments on types, sequences, constraints and triggers.
|
|
115
|
+
- Dependency ordering across sections: the section order is fixed (extensions, types, functions, sequences, tables, indexes, foreign keys, views, triggers, comments, migrations). Within it, PostgreSQL types, domains, functions, views and materialized views and MySQL views are dependency-ordered.
|
|
356
116
|
|
|
357
|
-
|
|
358
|
-
<td width="50%">
|
|
117
|
+
More detail per adapter: [docs/features/multi-database.md](docs/features/multi-database.md).
|
|
359
118
|
|
|
360
|
-
|
|
119
|
+
## Configuration
|
|
361
120
|
|
|
362
|
-
|
|
363
|
-
SET client_encoding = 'UTF8';
|
|
364
|
-
|
|
365
|
-
-- Extensions
|
|
366
|
-
CREATE EXTENSION IF NOT EXISTS plpgsql
|
|
367
|
-
WITH SCHEMA pg_catalog;
|
|
368
|
-
CREATE EXTENSION IF NOT EXISTS pgcrypto
|
|
369
|
-
WITH SCHEMA public;
|
|
370
|
-
|
|
371
|
-
-- Tables
|
|
372
|
-
CREATE TABLE users (
|
|
373
|
-
id bigserial PRIMARY KEY,
|
|
374
|
-
email varchar NOT NULL,
|
|
375
|
-
created_at timestamp(6) NOT NULL,
|
|
376
|
-
updated_at timestamp(6) NOT NULL
|
|
377
|
-
);
|
|
378
|
-
|
|
379
|
-
CREATE INDEX index_users_on_email
|
|
380
|
-
ON users (email);
|
|
381
|
-
```
|
|
382
|
-
|
|
383
|
-
**๐ฏ Benefits:**
|
|
384
|
-
- Clean, minimal output
|
|
385
|
-
- Deterministic
|
|
386
|
-
- Easy to review
|
|
387
|
-
- Version control friendly
|
|
388
|
-
|
|
389
|
-
</td>
|
|
390
|
-
</tr>
|
|
391
|
-
</table>
|
|
392
|
-
|
|
393
|
-
## โ๏ธ Configuration
|
|
394
|
-
|
|
395
|
-
### ๐ Single-File Output (Default)
|
|
121
|
+
`config/initializers/better_structure_sql.rb`. Every option, with the default from [`lib/better_structure_sql/configuration.rb`](lib/better_structure_sql/configuration.rb):
|
|
396
122
|
|
|
397
123
|
```ruby
|
|
398
|
-
# config/initializers/better_structure_sql.rb
|
|
399
124
|
BetterStructureSql.configure do |config|
|
|
400
|
-
#
|
|
401
|
-
config.output_path = 'db/structure.sql'
|
|
125
|
+
config.output_path = 'db/structure.sql' # a path without a file extension means multi-file mode
|
|
402
126
|
|
|
403
|
-
#
|
|
404
|
-
|
|
405
|
-
config.replace_default_dump = false
|
|
406
|
-
config.replace_default_load = false
|
|
127
|
+
config.replace_default_dump = false # true: rails db:schema:dump uses this gem
|
|
128
|
+
config.replace_default_load = false # true: rails db:schema:load uses this gem
|
|
407
129
|
|
|
408
|
-
# Schema version storage (optional)
|
|
409
|
-
config.enable_schema_versions = true
|
|
410
|
-
config.schema_versions_limit = 10 # Keep last 10 versions (0 = unlimited)
|
|
411
|
-
|
|
412
|
-
# Customize output (feature toggles)
|
|
413
|
-
config.include_extensions = true
|
|
414
|
-
config.include_functions = true
|
|
415
|
-
config.include_triggers = true
|
|
416
|
-
config.include_views = true
|
|
417
|
-
config.include_materialized_views = true # PostgreSQL only
|
|
418
|
-
config.include_domains = true # PostgreSQL only
|
|
419
|
-
config.include_sequences = true # PostgreSQL only
|
|
420
|
-
config.include_custom_types = true # PostgreSQL ENUM, MySQL ENUM/SET
|
|
421
|
-
# config.include_rules = false # Not yet implemented
|
|
422
|
-
# config.include_comments = false # Not yet implemented
|
|
423
|
-
|
|
424
|
-
# Search path and schema filtering
|
|
425
|
-
config.search_path = '"$user", public'
|
|
426
|
-
config.schemas = ['public'] # Which schemas to dump
|
|
427
|
-
end
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
### ๐ Multi-File Output (Recommended for Large Projects)
|
|
431
|
-
|
|
432
|
-
> **๐ก Recommended:** Use `db/schema` directory mode for projects with 100+ tables for better git diffs, easier navigation, and AI-friendly organization.
|
|
433
|
-
|
|
434
|
-
```ruby
|
|
435
|
-
# config/initializers/better_structure_sql.rb
|
|
436
|
-
BetterStructureSql.configure do |config|
|
|
437
|
-
# Multi-file output - splits schema across directories
|
|
438
|
-
config.output_path = 'db/schema'
|
|
439
|
-
|
|
440
|
-
# Chunking configuration
|
|
441
|
-
config.max_lines_per_file = 500 # Soft limit per file (default: 500)
|
|
442
|
-
config.overflow_threshold = 1.1 # 10% overflow allowed (default: 1.1)
|
|
443
|
-
config.generate_manifest = true # Create _manifest.json (default: true)
|
|
444
|
-
|
|
445
|
-
# Schema version storage with ZIP archives
|
|
446
|
-
config.enable_schema_versions = true
|
|
447
|
-
config.schema_versions_limit = 10
|
|
448
|
-
|
|
449
|
-
# Feature toggles (same as single-file mode)
|
|
450
130
|
config.include_extensions = true
|
|
131
|
+
config.include_custom_types = true
|
|
132
|
+
config.include_domains = true
|
|
133
|
+
config.include_sequences = true
|
|
451
134
|
config.include_functions = true
|
|
452
|
-
config.include_triggers = true
|
|
453
135
|
config.include_views = true
|
|
454
136
|
config.include_materialized_views = true
|
|
455
|
-
config.
|
|
456
|
-
config.
|
|
457
|
-
config.include_custom_types = true
|
|
137
|
+
config.include_triggers = true
|
|
138
|
+
config.include_comments = true
|
|
458
139
|
|
|
459
|
-
#
|
|
460
|
-
config.
|
|
461
|
-
config.
|
|
462
|
-
config.
|
|
463
|
-
end
|
|
464
|
-
```
|
|
140
|
+
config.search_path = '"$user", public' # PostgreSQL: written as SET search_path TO ...
|
|
141
|
+
config.sort_tables = true # alphabetical table order
|
|
142
|
+
config.indent_size = 2
|
|
143
|
+
config.add_section_spacing = true
|
|
465
144
|
|
|
466
|
-
|
|
145
|
+
config.max_lines_per_file = 500 # multi-file: soft limit per file
|
|
146
|
+
config.overflow_threshold = 1.1 # multi-file: a file may grow to limit * threshold
|
|
147
|
+
config.generate_manifest = true # multi-file: write _manifest.json
|
|
467
148
|
|
|
468
|
-
|
|
149
|
+
config.enable_schema_versions = false # needed by db:schema:store / versions / cleanup
|
|
150
|
+
config.schema_versions_limit = 10 # versions kept; 0 keeps all
|
|
469
151
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
โโโ _header.sql # SET statements and search path
|
|
473
|
-
โโโ _manifest.json # Metadata and load order
|
|
474
|
-
โโโ 01_extensions/
|
|
475
|
-
โ โโโ 000001.sql
|
|
476
|
-
โโโ 02_types/
|
|
477
|
-
โ โโโ 000001.sql
|
|
478
|
-
โโโ 03_functions/
|
|
479
|
-
โ โโโ 000001.sql
|
|
480
|
-
โโโ 04_sequences/
|
|
481
|
-
โ โโโ 000001.sql
|
|
482
|
-
โโโ 05_tables/
|
|
483
|
-
โ โโโ 000001.sql # ~500 lines per file
|
|
484
|
-
โ โโโ 000002.sql
|
|
485
|
-
โ โโโ 000003.sql
|
|
486
|
-
โโโ 06_indexes/
|
|
487
|
-
โ โโโ 000001.sql
|
|
488
|
-
โโโ 07_foreign_keys/
|
|
489
|
-
โ โโโ 000001.sql
|
|
490
|
-
โโโ 08_views/
|
|
491
|
-
โ โโโ 000001.sql
|
|
492
|
-
โโโ 09_triggers/
|
|
493
|
-
โ โโโ 000001.sql
|
|
494
|
-
โโโ 10_comments/
|
|
495
|
-
โ โโโ 000001.sql
|
|
496
|
-
โโโ 20_migrations/
|
|
497
|
-
โโโ 000001.sql
|
|
152
|
+
config.adapter = :auto # :auto, :postgresql, :mysql, :sqlite
|
|
153
|
+
end
|
|
498
154
|
```
|
|
499
155
|
|
|
500
|
-
|
|
501
|
-
- โ
Memory efficient - incremental file writing
|
|
502
|
-
- โ
Git friendly - only changed files in diffs
|
|
503
|
-
- โ
Easy navigation - find specific tables in `05_tables/`, triggers in `09_triggers/`, etc.
|
|
504
|
-
- โ
ZIP downloads - complete directory as single archive
|
|
505
|
-
- โ
Scalable - handles 50,000+ database objects
|
|
506
|
-
- โ
AI-friendly - 500-line chunks work better with LLM context windows
|
|
507
|
-
|
|
508
|
-
**Manifest File (_manifest.json)**:
|
|
509
|
-
|
|
510
|
-
The manifest tracks metadata and provides load order information:
|
|
511
|
-
|
|
512
|
-
```json
|
|
513
|
-
{
|
|
514
|
-
"version": "1.0",
|
|
515
|
-
"total_files": 11,
|
|
516
|
-
"total_lines": 2345,
|
|
517
|
-
"max_lines_per_file": 500,
|
|
518
|
-
"directories": {
|
|
519
|
-
"01_extensions": { "files": 1, "lines": 3 },
|
|
520
|
-
"02_types": { "files": 1, "lines": 13 },
|
|
521
|
-
"03_functions": { "files": 1, "lines": 332 },
|
|
522
|
-
"04_sequences": { "files": 1, "lines": 289 },
|
|
523
|
-
"05_tables": { "files": 2, "lines": 979 },
|
|
524
|
-
"06_indexes": { "files": 1, "lines": 397 },
|
|
525
|
-
"07_foreign_keys": { "files": 1, "lines": 67 },
|
|
526
|
-
"08_views": { "files": 1, "lines": 217 },
|
|
527
|
-
"09_triggers": { "files": 1, "lines": 35 },
|
|
528
|
-
"10_comments": { "files": 1, "lines": 9 },
|
|
529
|
-
"20_migrations": { "files": 1, "lines": 13 }
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
```
|
|
156
|
+
The initializer written by `better_structure_sql:install` differs from these defaults in three places: it sets `enable_schema_versions = true`, `sort_tables = false` and `search_path = 'public'`.
|
|
533
157
|
|
|
534
|
-
|
|
158
|
+
`replace_default_dump` also switches Rails to `schema_format = :sql`. Both `replace_default_*` options are ignored when `output_path` ends in `.rb`.
|
|
535
159
|
|
|
536
|
-
|
|
160
|
+
Full reference, validation rules and options that exist but have no effect yet: [docs/configuration.md](docs/configuration.md).
|
|
537
161
|
|
|
538
|
-
|
|
162
|
+
## Multi-file output
|
|
539
163
|
|
|
540
|
-
|
|
541
|
-
# Dump schema using BetterStructureSql (explicit)
|
|
542
|
-
rails db:schema:dump_better
|
|
164
|
+
Point `output_path` at a directory (any path without a file extension) and the dump is split by object type. The numeric prefix is the load order.
|
|
543
165
|
|
|
544
|
-
|
|
545
|
-
|
|
166
|
+
```ruby
|
|
167
|
+
config.output_path = 'db/schema'
|
|
546
168
|
```
|
|
547
169
|
|
|
548
|
-
### Schema Versioning Tasks
|
|
549
|
-
|
|
550
|
-
**Store Current Schema**
|
|
551
|
-
```bash
|
|
552
|
-
# Store the current schema as a version in the database
|
|
553
|
-
rails db:schema:store
|
|
554
170
|
```
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
171
|
+
db/schema/
|
|
172
|
+
โโโ _header.sql
|
|
173
|
+
โโโ _manifest.json
|
|
174
|
+
โโโ 01_extensions/000001.sql
|
|
175
|
+
โโโ 02_types/000001.sql # enums, composite types, domains
|
|
176
|
+
โโโ 03_functions/000001.sql
|
|
177
|
+
โโโ 04_sequences/000001.sql
|
|
178
|
+
โโโ 05_tables/000001.sql
|
|
179
|
+
โโโ 05_tables/000002.sql
|
|
180
|
+
โโโ 06_indexes/000001.sql
|
|
181
|
+
โโโ 07_foreign_keys/000001.sql
|
|
182
|
+
โโโ 08_views/000001.sql # views and materialized views
|
|
183
|
+
โโโ 09_triggers/000001.sql
|
|
184
|
+
โโโ 10_comments/000001.sql
|
|
185
|
+
โโโ 20_migrations/000001.sql
|
|
567
186
|
```
|
|
568
187
|
|
|
569
|
-
|
|
570
|
-
```
|
|
571
|
-
Total versions: 3
|
|
188
|
+
A directory appears only when it has content. Files are filled up to `max_lines_per_file`; one object is never split across files. `rails db:schema:load_better` loads either a file or a directory.
|
|
572
189
|
|
|
573
|
-
|
|
574
|
-
-----------------------------------------------------------------------------------------------
|
|
575
|
-
3 sql multi_file 12 15.3 2025-01-15 10:30:22 56.42 KB
|
|
576
|
-
2 sql single_file 1 15.3 2025-01-14 15:20:10 45.21 KB
|
|
577
|
-
1 sql single_file 1 15.2 2025-01-13 09:45:33 44.03 KB
|
|
578
|
-
```
|
|
190
|
+
Details: [docs/features/multi-file-output.md](docs/features/multi-file-output.md).
|
|
579
191
|
|
|
580
|
-
|
|
192
|
+
## Schema versions and web UI
|
|
581
193
|
|
|
582
|
-
|
|
583
|
-
```bash
|
|
584
|
-
# Restore database from a specific version
|
|
585
|
-
rails db:schema:restore[5]
|
|
194
|
+
Store each distinct schema in the database, so a developer can read the production schema without database access.
|
|
586
195
|
|
|
587
|
-
|
|
588
|
-
|
|
196
|
+
```ruby
|
|
197
|
+
config.enable_schema_versions = true
|
|
198
|
+
config.schema_versions_limit = 10
|
|
589
199
|
```
|
|
590
200
|
|
|
591
|
-
**Cleanup Old Versions**
|
|
592
201
|
```bash
|
|
593
|
-
|
|
594
|
-
rails db:schema:
|
|
202
|
+
rails db:schema:dump_better
|
|
203
|
+
rails db:schema:store # stores a version, or skips when the content hash matches the latest one
|
|
204
|
+
rails db:schema:versions # lists stored versions
|
|
205
|
+
rails db:schema:cleanup # applies schema_versions_limit
|
|
206
|
+
CONFIRM=1 rails db:schema:restore[5] # replaces the database schema with a stored version
|
|
595
207
|
```
|
|
596
208
|
|
|
597
|
-
|
|
209
|
+
`db:schema:store` reads the file or directory at `output_path`; it does not dump and does not modify it. In multi-file mode it also stores a ZIP of the directory.
|
|
598
210
|
|
|
599
|
-
|
|
211
|
+
`db:schema:restore` **purges the database** (every table and all data, the stored versions included), then loads the version. It needs `CONFIRM=1` and refuses protected environments such as production, like `db:schema:load`.
|
|
600
212
|
|
|
601
|
-
|
|
602
|
-
# config/routes.rb
|
|
603
|
-
Rails.application.routes.draw do
|
|
604
|
-
# With authentication (recommended for production)
|
|
605
|
-
authenticate :user, ->(user) { user.admin? } do
|
|
606
|
-
mount BetterStructureSql::Engine, at: '/schema_versions'
|
|
607
|
-
end
|
|
608
|
-
|
|
609
|
-
# Or without authentication (development only)
|
|
610
|
-
mount BetterStructureSql::Engine, at: '/schema_versions' if Rails.env.development?
|
|
611
|
-
end
|
|
612
|
-
```
|
|
613
|
-
|
|
614
|
-
Access at `http://localhost:3000/schema_versions` to:
|
|
615
|
-
- View list of up to 100 most recent schema versions (pagination-ready)
|
|
616
|
-
- Browse formatted schema content with syntax highlighting (for files <200KB)
|
|
617
|
-
- Download raw SQL/Ruby schema files as text
|
|
618
|
-
- Download ZIP archives for multi-file schemas
|
|
619
|
-
- View manifest metadata for multi-file schemas
|
|
620
|
-
- Stream large files efficiently (>2MB) without memory issues
|
|
621
|
-
- Compare database versions and formats
|
|
622
|
-
|
|
623
|
-
**Authentication Examples**:
|
|
213
|
+
Mount the engine to browse, view and download versions:
|
|
624
214
|
|
|
625
215
|
```ruby
|
|
626
|
-
#
|
|
216
|
+
# config/routes.rb
|
|
627
217
|
authenticate :user, ->(user) { user.admin? } do
|
|
628
|
-
mount BetterStructureSql::Engine, at: '/admin/schema'
|
|
629
|
-
end
|
|
630
|
-
|
|
631
|
-
# Custom constraint class
|
|
632
|
-
class AdminConstraint
|
|
633
|
-
def matches?(request)
|
|
634
|
-
user = request.env['warden']&.user
|
|
635
|
-
user&.admin?
|
|
636
|
-
end
|
|
637
|
-
end
|
|
638
|
-
|
|
639
|
-
constraints AdminConstraint.new do
|
|
640
218
|
mount BetterStructureSql::Engine, at: '/schema_versions'
|
|
641
219
|
end
|
|
642
220
|
|
|
643
|
-
#
|
|
644
|
-
|
|
645
|
-
# Add your production auth here
|
|
646
|
-
else
|
|
647
|
-
mount BetterStructureSql::Engine, at: '/schema_versions'
|
|
648
|
-
end
|
|
221
|
+
# config/initializers/better_structure_sql.rb
|
|
222
|
+
BetterStructureSql::Engine.authentication_handled_by_routes!
|
|
649
223
|
```
|
|
650
224
|
|
|
651
|
-
|
|
225
|
+
The UI shows your whole schema, so it fails closed: outside development and test every request gets `403 Forbidden` until you declare how access is controlled. Either state that the routes do it (above) or register a check:
|
|
652
226
|
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
227
|
+
```ruby
|
|
228
|
+
BetterStructureSql::Engine.authenticate_with do |controller|
|
|
229
|
+
controller.request.env['warden']&.user&.admin?
|
|
230
|
+
end
|
|
657
231
|
```
|
|
658
232
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
233
|
+
Details: [docs/schema_versions.md](docs/schema_versions.md), [docs/features/web-ui.md](docs/features/web-ui.md).
|
|
234
|
+
|
|
235
|
+
## Rake tasks
|
|
236
|
+
|
|
237
|
+
| Task | Does |
|
|
238
|
+
|---|---|
|
|
239
|
+
| `db:schema:dump_better` | Dump to `output_path` |
|
|
240
|
+
| `db:schema:load_better` | Load the file or directory at `output_path` (on MySQL, the way to load a dump unless `replace_default_load` is set) |
|
|
241
|
+
| `db:schema:store` | Store the dumped schema as a version (skips unchanged content) |
|
|
242
|
+
| `db:schema:versions` | List stored versions |
|
|
243
|
+
| `db:schema:cleanup` | Delete versions beyond `schema_versions_limit` |
|
|
244
|
+
| `db:schema:restore[ID]` | Replace the database schema with a stored version: purges the database first, needs `CONFIRM=1` (`VERSION_ID=5` also works) |
|
|
245
|
+
| `db:schema:dump`, `db:schema:load` | Use this gem when `replace_default_dump` / `replace_default_load` are set |
|
|
246
|
+
|
|
247
|
+
## Documentation
|
|
248
|
+
|
|
249
|
+
| Topic | Where |
|
|
250
|
+
|---|---|
|
|
251
|
+
| Docs site with tutorials | <https://sebyx07.github.io/better_structure_sql/> |
|
|
252
|
+
| Install | [docs/installation.md](docs/installation.md) |
|
|
253
|
+
| Configuration reference | [docs/configuration.md](docs/configuration.md) |
|
|
254
|
+
| Tasks and Ruby API | [docs/usage.md](docs/usage.md) |
|
|
255
|
+
| Schema versions | [docs/schema_versions.md](docs/schema_versions.md) |
|
|
256
|
+
| Multi-file output | [docs/features/multi-file-output.md](docs/features/multi-file-output.md) |
|
|
257
|
+
| Adapters | [docs/features/multi-database.md](docs/features/multi-database.md) |
|
|
258
|
+
| Web UI | [docs/features/web-ui.md](docs/features/web-ui.md) |
|
|
259
|
+
| Moving from `schema.rb` | [docs/migration-guides/from-schema-rb-to-structure-sql.md](docs/migration-guides/from-schema-rb-to-structure-sql.md) |
|
|
260
|
+
| Internals | [docs/architecture.md](docs/architecture.md) |
|
|
261
|
+
| One-file reference for coding agents | [llms.txt](llms.txt) |
|
|
262
|
+
| Changes | [CHANGELOG.md](CHANGELOG.md) |
|
|
263
|
+
|
|
264
|
+
## AI-maintained
|
|
265
|
+
|
|
266
|
+
**This project is maintained by [developerz.ai](https://developerz.ai).** Its maintainer agent triages issues, reviews every pull request, merges a pull request once its author marks it ready and CI is green, checks project health daily, and runs a monthly improvement pass. The policy it follows is committed in [`.dz/maintainer/maintainer.yml`](.dz/maintainer/maintainer.yml). Breaking changes and major releases stay with the owner.
|
|
267
|
+
|
|
268
|
+
- **Using the gem in your app?** Point your agent at [`llms.txt`](llms.txt). Paste this into your app's `CLAUDE.md` / `AGENTS.md`:
|
|
269
|
+
```markdown
|
|
270
|
+
## Schema dumps
|
|
271
|
+
`db/structure.sql` (or `db/schema/`) is generated by the better_structure_sql gem; never edit it by hand.
|
|
272
|
+
Before changing its configuration read https://github.com/sebyx07/better_structure_sql/blob/main/llms.txt
|
|
273
|
+
```
|
|
274
|
+
- **Working on this repo?** [`CLAUDE.md`](CLAUDE.md) holds the commands, the code map and the rules; [CONTRIBUTING.md](CONTRIBUTING.md) covers pull requests.
|
|
275
|
+
|
|
276
|
+
## Development
|
|
668
277
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
#
|
|
672
|
-
|
|
673
|
-
run: |
|
|
674
|
-
rails db:migrate
|
|
675
|
-
rails db:schema:store
|
|
278
|
+
```bash
|
|
279
|
+
bin/setup # install dependencies and prepare the test database
|
|
280
|
+
bin/check # the gate CI runs: tests and linters
|
|
281
|
+
bin/dev # start the integration app
|
|
676
282
|
```
|
|
677
283
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
| Component | Version | Notes |
|
|
681
|
-
|-----------|---------|-------|
|
|
682
|
-
| **Ruby** | 2.7+ | Tested up to Ruby 3.4.7 |
|
|
683
|
-
| **Rails** | 7.0+ | Works with Rails 8.1.1+ |
|
|
684
|
-
| **rubyzip** | โฅ 2.0.0 | Required for ZIP archive support |
|
|
685
|
-
| **Database Adapter** | | |
|
|
686
|
-
| `pg` | โฅ 1.0 | **Required dependency**. Works with PostgreSQL 12+ |
|
|
687
|
-
| `mysql2` | โฅ 0.5 | Optional. For MySQL 8.0+ (experimental) |
|
|
688
|
-
| `sqlite3` | โฅ 1.4 | Optional. For SQLite 3.35+ (experimental) |
|
|
689
|
-
|
|
690
|
-
**Note**: The gem currently requires the `pg` gem as a dependency. Multi-database support (MySQL, SQLite) is implemented but requires manual gem installation. Future versions may make database adapters optional.
|
|
691
|
-
|
|
692
|
-
## Migration Guides
|
|
693
|
-
|
|
694
|
-
### Migrating from schema.rb to structure.sql
|
|
695
|
-
|
|
696
|
-
If you're currently using Rails' `schema.rb` (Ruby format) and want to switch to `structure.sql` (SQL format) with BetterStructureSql, we have a comprehensive guide:
|
|
697
|
-
|
|
698
|
-
**[๐ Migration Guide: From schema.rb to structure.sql](docs/migration-guides/from-schema-rb-to-structure-sql.md)**
|
|
699
|
-
|
|
700
|
-
This guide covers:
|
|
701
|
-
- Why migrate from schema.rb to structure.sql
|
|
702
|
-
- Step-by-step migration process
|
|
703
|
-
- Configuration for both formats
|
|
704
|
-
- Switching between formats dynamically
|
|
705
|
-
- Comparing SQL vs Ruby schema versions
|
|
706
|
-
- Rollback procedures
|
|
707
|
-
- Best practices and troubleshooting
|
|
708
|
-
|
|
709
|
-
BetterStructureSql supports **both** `schema.rb` and `structure.sql` formats, allowing you to:
|
|
710
|
-
- Store versions of either format
|
|
711
|
-
- Switch between formats using `SCHEMA_FORMAT` environment variable
|
|
712
|
-
- Compare different formats in the web UI
|
|
713
|
-
- Migrate gradually from Ruby to SQL format
|
|
714
|
-
|
|
715
|
-
---
|
|
716
|
-
|
|
717
|
-
## ๐ Project Stats
|
|
718
|
-
|
|
719
|
-
**Codebase Metrics** (as of v0.1.0):
|
|
720
|
-
- **47 Ruby files** in `lib/` (~5,296 total lines)
|
|
721
|
-
- **25 test files** in `spec/` (~3,022 lines)
|
|
722
|
-
- **8 adapter files** (PostgreSQL, MySQL, SQLite, Registry, Configs)
|
|
723
|
-
- **13 SQL generators** (Tables, Indexes, Functions, Triggers, Views, etc.)
|
|
724
|
-
- **9 introspection modules** (Extensions, Types, Tables, Indexes, Foreign Keys, etc.)
|
|
725
|
-
- **3 integration apps** (PostgreSQL, MySQL, SQLite) with Docker support
|
|
726
|
-
- **React documentation site** deployed to GitHub Pages
|
|
727
|
-
|
|
728
|
-
**Test Coverage**: Comprehensive RSpec test suite with unit and integration tests across all major components.
|
|
729
|
-
|
|
730
|
-
**Real-World Example**: The integration app generates a multi-file schema with:
|
|
731
|
-
- 11 SQL files across 10 directories
|
|
732
|
-
- 2,345 total lines of SQL
|
|
733
|
-
- Complete PostgreSQL feature coverage (extensions, types, functions, triggers, materialized views)
|
|
734
|
-
|
|
735
|
-
**Production Status**:
|
|
736
|
-
- โ
**PostgreSQL**: Fully implemented and tested (primary focus)
|
|
737
|
-
- โ
**Multi-file output**: Complete with ZIP storage and streaming
|
|
738
|
-
- โ
**Schema versioning**: Full CRUD with web UI
|
|
739
|
-
- โ
**Rails integration**: Drop-in replacement for default tasks
|
|
740
|
-
- ๐งช **MySQL**: Adapter implemented, integration app available (experimental)
|
|
741
|
-
- ๐งช **SQLite**: Adapter implemented, basic testing (experimental)
|
|
742
|
-
|
|
743
|
-
---
|
|
744
|
-
|
|
745
|
-
## ๐ค Contributing
|
|
746
|
-
|
|
747
|
-
We welcome contributions! Bug reports and pull requests are welcome on [GitHub](https://github.com/sebyx07/better_structure_sql).
|
|
284
|
+
Tests need a reachable PostgreSQL. See [CONTRIBUTING.md](CONTRIBUTING.md) and [docs/testing.md](docs/testing.md).
|
|
748
285
|
|
|
749
|
-
|
|
286
|
+
Releases are cut by merging a version bump: see [RELEASE.md](RELEASE.md).
|
|
750
287
|
|
|
751
|
-
|
|
752
|
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
753
|
-
3. Run the tests (`bundle exec rspec`)
|
|
754
|
-
4. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
755
|
-
5. Push to the branch (`git push origin feature/amazing-feature`)
|
|
756
|
-
6. Open a Pull Request
|
|
288
|
+
## Security
|
|
757
289
|
|
|
758
|
-
|
|
290
|
+
Report vulnerabilities privately: [SECURITY.md](SECURITY.md).
|
|
759
291
|
|
|
760
|
-
|
|
292
|
+
## License
|
|
761
293
|
|
|
762
|
-
|
|
294
|
+
MIT. See [LICENSE](LICENSE).
|
|
763
295
|
|
|
764
|
-
|
|
296
|
+
## Star history
|
|
765
297
|
|
|
766
|
-
|
|
298
|
+
[](https://star-history.com/#sebyx07/better_structure_sql&Date)
|