Track and manage MySQL query changes over time using file-based Git repositories, semantic comments, and repeatable migration scripts.
Audit trails, collaboration and rollback require a history of every change. Storing queries in Git surfaces diffs, code reviews and branching—features SQL alone lacks.
Commit .sql
files containing views, stored procedures, functions, migration scripts, seed data and analytics queries. Keep one feature or object per file for clear diffs.
Create folders like migrations/
, views/
, reports/
and scripts/
.Prefix migration files with an incremental version number—e.g., 001_create_orders.sql
.
Each migration contains an UP
section that applies a change and a matching DOWN
section that reverts it.Use explicit transaction boundaries.
-- 002_add_index_on_orders.sql
-- :up
ALTER TABLE Orders ADD INDEX idx_customer_date (customer_id, order_date);
-- :down
ALTER TABLE Orders DROP INDEX idx_customer_date;
Add semantic comments such as /* v:1.2 */
at the top of long-living queries.Galaxy’s AI copilot can auto-increment these when you save the file.
Tools like Flyway, Liquibase or simple shell scripts loop through migrations/
in order, applying only unseen files based on a schema_version
table.
Store read-only queries in reports/
. Use block comments to explain intent, input parameters and expected columns so team members can discover and reuse them.
1) One DDL change per migration.2) Always test DOWN
steps. 3) Review PRs like application code. 4) Tag production deploys with Git releases.
.
Keeping SQL in the same repo as application code simplifies CI/CD and branch management. Use a db/
folder to separate concerns.
Create scripts/
for one-time patches and reference the related ticket in the filename—for example, fix_1247_correct_refunds.sql
.
Because each script has a DOWN section, you can rerun the previous version or restore from backups. Always test migrations on staging first.