@@ -9,16 +9,17 @@ Create SQL migration files following naming conventions that enable proper versi
99Migration filenames must follow this structure:
1010
1111```
12- <Version>__<Description>_<Suffix >.sql
12+ <Version>_<Description >.sql
1313```
1414
1515** Components:**
1616
17- 1 . ** Version** (required) - Must begin with a number, optional ` v ` or ` V ` prefix
18- 2 . ** Double underscore** (` __ ` ) separator
17+ 1 . ** Version** (required) - Optional ` v ` or ` V ` prefix, followed by one or more numbers separated by dots
18+ - Pattern: ` ^[vV]?(\d+(\.\d+)*) `
19+ - Examples: ` v1.2.3 ` , ` 1.0 ` , ` V2 `
20+ 2 . ** Underscore** (` _ ` ) separator
19213 . ** Description** - Human-readable description using underscores or hyphens
20- 4 . ** Suffix** (optional) - Migration type indicator
21- 5 . ** ` .sql ` ** file extension
22+ 4 . ** ` .sql ` ** file extension
2223
2324## Version Formats
2425
@@ -29,8 +30,8 @@ Choose a versioning strategy that fits your team:
2930 ** Timestamp-Based** - Recommended for teams with parallel development
3031
3132 ```
32- 20250120143000__add_user_email .sql
33- 20250121091500__create_orders_table .sql
33+ 20250120143000_add_user_email .sql
34+ 20250121091500_create_orders_table .sql
3435 ```
3536
3637 Format: ` YYYYMMDDHHmmss `
@@ -46,10 +47,10 @@ Choose a versioning strategy that fits your team:
4647 ** Semantic Versioning** - Meaningful version numbers
4748
4849 ```
49- v1.0.0__initial_release .sql
50- v1.1.0__add_user_profiles .sql
51- v1.1.1__fix_profile_constraint .sql
52- v2.0.0__redesign_authentication .sql
50+ v1.0.0_initial_release .sql
51+ v1.1.0_add_user_profiles .sql
52+ v1.1.1_fix_profile_constraint .sql
53+ v2.0.0_redesign_authentication .sql
5354 ```
5455
5556 ✅ Conveys change significance
@@ -63,9 +64,9 @@ Choose a versioning strategy that fits your team:
6364 ** Simple Sequential** - Easy to understand
6465
6566 ```
66- 001__initial_schema .sql
67- 002__add_users .sql
68- 003__add_products .sql
67+ 001_initial_schema .sql
68+ 002_add_users .sql
69+ 003_add_products .sql
6970 ```
7071
7172 ✅ Simple and clear
@@ -75,104 +76,21 @@ Choose a versioning strategy that fits your team:
7576 </Tab >
7677</Tabs >
7778
78- ## Change Type Suffixes
79+ ## Migration Type (MySQL Only)
7980
80- Specify the migration type using an optional suffix:
81-
82- | Suffix | Type | Description | Use Cases |
83- | --------| ------| -------------| -----------|
84- | * (none)* | ** DDL** | Data Definition Language | CREATE, ALTER, DROP tables, indexes, constraints |
85- | ` _dml ` | ** DML** | Data Manipulation Language | INSERT, UPDATE, DELETE, data migrations |
86- | ` _ghost ` | ** Ghost** | gh-ost online migration | Zero-downtime MySQL schema changes |
87-
88- ** Examples:**
89-
90- ```
91- v1.0.0__create_schema.sql (DDL - default)
92- v1.1.0__seed_initial_data_dml.sql (DML - data changes)
93- v1.2.0__add_user_index_ghost.sql (Ghost - online schema change)
94- ```
95-
96- <Card title = " Online Schema Migration" icon = " bolt" href = " /change-database/online-schema-migration-for-mysql" >
97- Learn about gh-ost for zero-downtime MySQL migrations
98- </Card >
99-
100- ## File Organization
101-
102- ** Recommended structure:**
103-
104- ```
105- migrations/
106- ├── 001__initial_schema.sql
107- ├── 002__add_users.sql
108- ├── 003__add_products_dml.sql
109- └── 004__add_indexes.sql
110- ```
111-
112- ** Or with subdirectories:**
113-
114- ```
115- migrations/
116- ├── baseline/
117- │ └── 000__initial_schema.sql
118- ├── features/
119- │ ├── 001__users.sql
120- │ ├── 002__products.sql
121- │ └── 003__orders.sql
122- └── hotfixes/
123- └── 004__fix_index.sql
124- ```
125-
126- ## Best Practices
127-
128- ** Keep migrations small and focused:**
129-
130- ``` sql
131- ✅ Good - Single, clear purpose
132- -- 005__add_user_email.sql
133- ALTER TABLE users ADD COLUMN email TEXT ;
134- ```
81+ For zero-downtime MySQL schema changes, add this comment at the top of your file:
13582
13683``` sql
137- ❌ Bad - Too large and unfocused
138- -- 005__big_refactor.sql
139- ALTER TABLE users ADD COLUMN email TEXT ;
140- ALTER TABLE users ADD COLUMN phone TEXT ;
141- CREATE TABLE user_preferences (...);
142- -- 500 more lines
143- ```
144-
145- ** Add comments for complex logic:**
84+ -- migration-type: ghost
14685
147- ``` sql
148- -- 020__migrate_legacy_permissions_dml.sql
149- -- Migrates old role system to new permission model
150- -- Old: roles.name -> New: permissions.scope + permissions.action
151-
152- UPDATE permissions
153- SET scope = CASE
154- WHEN roles .name = ' admin' THEN ' database'
155- WHEN roles .name = ' editor' THEN ' database'
156- END
157- FROM roles
158- WHERE permissions .role_id = roles .id ;
86+ ALTER TABLE users ADD COLUMN email VARCHAR (255 );
15987```
16088
161- ** Use transactions when possible:**
162-
163- ``` sql
164- -- 015__multi_step_migration_dml.sql
165- BEGIN ;
166-
167- UPDATE users SET status = ' migrated' WHERE status = ' legacy' ;
89+ This uses [ gh-ost] ( https://github.com/github/gh-ost ) to apply changes without blocking your database.
16890
169- INSERT INTO user_audit_log (user_id, action)
170- SELECT id, ' status_migrated'
171- FROM users
172- WHERE status = ' migrated' ;
173-
174- COMMIT ;
175- ```
91+ <Card title = " Online Schema Migration" icon = " bolt" href = " /change-database/online-schema-migration-for-mysql" >
92+ Learn more about gh-ost
93+ </Card >
17694
17795---
17896
0 commit comments