masterrecord 0.3.68 → 1.0.1
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.
- package/.claude/settings.local.json +3 -1
- package/CHANGES.md +31 -2
- package/Cache/QueryCache.js +10 -4
- package/Cache/RedisQueryCache.js +3 -2
- package/Entity/entityModel.js +1 -1
- package/Entity/entityModelBuilder.js +2 -2
- package/Entity/entityTrackerModel.js +3 -3
- package/Entity/fieldTransformer.js +1 -1
- package/FOREIGN_KEY_STRING_FIX.md +2 -2
- package/MasterRecord.js +6 -43
- package/Migrations/cli.js +44 -113
- package/Migrations/dependencyGraph.js +1 -1
- package/Migrations/migrationMySQLQuery.js +1 -1
- package/Migrations/migrationPostgresQuery.js +1 -1
- package/Migrations/migrationSQLiteQuery.js +1 -1
- package/Migrations/migrationTemplate.js +8 -39
- package/Migrations/migrations.js +16 -14
- package/Migrations/pathUtils.js +2 -5
- package/Migrations/pathUtils.test.js +33 -49
- package/Migrations/schema.js +52 -89
- package/QueryLanguage/queryMethods.js +4 -4
- package/QueryLanguage/queryParameters.js +1 -1
- package/QueryLanguage/queryScript.js +4 -3
- package/SQLLiteEngine.js +30 -8
- package/Tools.js +10 -8
- package/context.js +43 -42
- package/deleteManager.js +2 -2
- package/docs/MIGRATIONS_GUIDE.md +237 -532
- package/docs/POSTGRESQL_SETUP.md +4 -4
- package/docs/QUERY_CACHING_GUIDE.md +8 -7
- package/eslint.config.js +40 -0
- package/insertManager.js +28 -20
- package/mySQLConnect.js +2 -2
- package/mySQLEngine.js +161 -16
- package/package.json +34 -5
- package/postgresEngine.js +74 -26
- package/postgresSyncConnect.js +5 -3
- package/prettier.config.js +22 -0
- package/readme.md +35 -27
- package/test/context-errors.test.js +43 -0
- package/test/count-chained-where.test.js +136 -0
- package/test/esm-imports.test.js +110 -0
- package/test/fixtures/count-chained-where/config/environments/env.countchained.json +1 -0
- package/test/fixtures/count-chained-where/db/countctx.sqlite +0 -0
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/query-builder-parity/config/environments/env.parity.json +1 -0
- package/test/fixtures/query-builder-parity/db/parityctx.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/migration-template.test.js +47 -0
- package/test/new-setter-large-string.test.js +191 -0
- package/test/pool-key.test.js +43 -0
- package/test/query-builder-parity.test.js +260 -0
- package/test/sqlite-lifecycle.test.js +76 -0
- package/.eslintrc.js +0 -290
- package/.prettierrc.js +0 -109
- package/test/anyCommaStringTest.js +0 -237
- package/test/anyMethodTest.js +0 -176
- package/test/attachDetached.test.js +0 -303
- package/test/bulk-operations-test.js +0 -235
- package/test/cache-toObject-test.js +0 -105
- package/test/cacheIntegration.test.js +0 -319
- package/test/config-glob-pattern-test.js +0 -232
- package/test/double-where-bug-test.js +0 -71
- package/test/ensure-ready-test.js +0 -251
- package/test/entity-deduplication-test.js +0 -174
- package/test/entity-methods-test.js +0 -269
- package/test/findByIdTest.js +0 -227
- package/test/foreign-key-string-value-test.js +0 -406
- package/test/global-model-registry-test.js +0 -538
- package/test/id-setting-validation.js +0 -202
- package/test/includesFeatureTest.js +0 -183
- package/test/includesTransformTest.js +0 -110
- package/test/index-bug-fix-test.js +0 -188
- package/test/insert-return-test.js +0 -39
- package/test/lifecycle-hooks-test.js +0 -258
- package/test/multiContextCache.test.js +0 -230
- package/test/new-entity-migration-test.js +0 -162
- package/test/newMethodTest.js +0 -330
- package/test/newMethodUnitTest.js +0 -320
- package/test/optInCache.test.js +0 -221
- package/test/parameterizedPlaceholderTest.js +0 -159
- package/test/pool-registry-test.js +0 -303
- package/test/post-insert-tracking-test.js +0 -258
- package/test/postgresEngineTest.js +0 -463
- package/test/postgresIntegrationTest.js +0 -381
- package/test/promise-detection-test.js +0 -290
- package/test/qa-context-pattern-test.js +0 -319
- package/test/query-helpers-test.js +0 -258
- package/test/query-isolation-test.js +0 -59
- package/test/queryCache.test.js +0 -148
- package/test/securityTest.js +0 -268
- package/test/seed-data-test.js +0 -212
- package/test/seed-deduplication-test.js +0 -313
- package/test/seed-features-integration-test.js +0 -418
- package/test/seed-migration-template-test.js +0 -220
- package/test/singleDollarPlaceholderTest.js +0 -238
- package/test/tablePrefixTest.js +0 -100
- package/test/transformerTest.js +0 -287
- package/test/v0.3.34-bug-fixes-test.js +0 -428
- package/test/validation-test.js +0 -302
- package/test/whereChainingTest.js +0 -88
package/docs/MIGRATIONS_GUIDE.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# MasterRecord Migrations Guide
|
|
2
2
|
|
|
3
|
-
Complete guide for database migrations and seed data with
|
|
3
|
+
Complete guide for database migrations and seed data with MySQL, SQLite, and PostgreSQL.
|
|
4
|
+
|
|
5
|
+
> **ESM only.** MasterRecord v1.0+ is a pure ESM package. Every example below uses `import`/`export default` syntax. Your host project must have `"type": "module"` in its `package.json`.
|
|
4
6
|
|
|
5
7
|
## Table of Contents
|
|
6
8
|
- [Overview](#overview)
|
|
@@ -13,687 +15,390 @@ Complete guide for database migrations and seed data with support for MySQL, SQL
|
|
|
13
15
|
|
|
14
16
|
## Overview
|
|
15
17
|
|
|
16
|
-
MasterRecord migrations
|
|
17
|
-
- Create and modify database tables
|
|
18
|
-
- Track schema changes over time
|
|
19
|
-
- Seed initial or test data
|
|
18
|
+
MasterRecord migrations let you:
|
|
19
|
+
- Create and modify database tables from code-first entity definitions
|
|
20
|
+
- Track schema changes over time with automatic snapshots
|
|
21
|
+
- Seed initial or test data with idempotent inserts
|
|
20
22
|
- Roll back changes when needed
|
|
21
23
|
- Work consistently across MySQL, SQLite, and PostgreSQL
|
|
22
24
|
|
|
23
25
|
## Quick Start
|
|
24
26
|
|
|
25
|
-
### 1.
|
|
27
|
+
### 1. Set up your context
|
|
26
28
|
|
|
27
29
|
```javascript
|
|
28
30
|
// app/models/context.js
|
|
29
|
-
|
|
31
|
+
import context from 'masterrecord/context';
|
|
32
|
+
import User from './User.js';
|
|
33
|
+
import Post from './Post.js';
|
|
30
34
|
|
|
31
35
|
class AppContext extends context {
|
|
32
36
|
constructor() {
|
|
33
37
|
super();
|
|
38
|
+
this.env('config/environments');
|
|
39
|
+
this.dbset(User);
|
|
40
|
+
this.dbset(Post);
|
|
34
41
|
}
|
|
35
42
|
}
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
export default AppContext;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Enable migrations (one-time)
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
masterrecord enable-migrations AppContext
|
|
38
51
|
```
|
|
39
52
|
|
|
40
|
-
|
|
53
|
+
This creates a snapshot file at `app/models/db/migrations/appcontext_contextSnapShot.json`.
|
|
54
|
+
|
|
55
|
+
### 3. Generate a migration
|
|
41
56
|
|
|
42
57
|
```bash
|
|
43
|
-
|
|
44
|
-
masterrecord add-migration InitialCreate context
|
|
58
|
+
masterrecord add-migration InitialCreate AppContext
|
|
45
59
|
```
|
|
46
60
|
|
|
47
|
-
|
|
61
|
+
MasterRecord diffs the current entity definitions against the snapshot and emits a migration file at `app/models/db/migrations/<timestamp>_InitialCreate_migration.js`.
|
|
48
62
|
|
|
49
|
-
###
|
|
63
|
+
### 4. Review the generated migration
|
|
50
64
|
|
|
51
65
|
```javascript
|
|
52
|
-
// migrations/
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
schema.createTable(table.Post);
|
|
60
|
-
|
|
61
|
-
// Add seed data
|
|
62
|
-
schema.seed('User', [
|
|
63
|
-
{ name: 'Admin', email: 'admin@example.com', role: 'admin' },
|
|
64
|
-
{ name: 'User', email: 'user@example.com', role: 'user' }
|
|
65
|
-
]);
|
|
66
|
-
},
|
|
66
|
+
// app/models/db/migrations/1700000000000_InitialCreate_migration.js
|
|
67
|
+
import masterrecord from 'masterrecord';
|
|
68
|
+
|
|
69
|
+
class InitialCreate extends masterrecord.schema {
|
|
70
|
+
constructor(context) {
|
|
71
|
+
super(context);
|
|
72
|
+
}
|
|
67
73
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
74
|
+
async up(table) {
|
|
75
|
+
await this.init(table);
|
|
76
|
+
await this.createTable(table.User);
|
|
77
|
+
await this.createTable(table.Post);
|
|
71
78
|
}
|
|
72
|
-
|
|
79
|
+
|
|
80
|
+
async down(table) {
|
|
81
|
+
await this.init(table);
|
|
82
|
+
await this.dropTable(table.Post);
|
|
83
|
+
await this.dropTable(table.User);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default InitialCreate;
|
|
73
88
|
```
|
|
74
89
|
|
|
75
|
-
###
|
|
90
|
+
### 5. Apply migrations
|
|
76
91
|
|
|
77
92
|
```bash
|
|
78
|
-
masterrecord
|
|
93
|
+
masterrecord update-database AppContext
|
|
79
94
|
```
|
|
80
95
|
|
|
81
96
|
## Database Support
|
|
82
97
|
|
|
83
|
-
### PostgreSQL
|
|
84
|
-
|
|
85
|
-
-
|
|
86
|
-
-
|
|
87
|
-
-
|
|
88
|
-
- UUID support
|
|
89
|
-
- Parameterized queries ($1, $2, $3...)
|
|
90
|
-
- ON CONFLICT DO NOTHING for idempotent seeds
|
|
98
|
+
### PostgreSQL
|
|
99
|
+
- SERIAL / BIGSERIAL auto-increment
|
|
100
|
+
- Native BOOLEAN, UUID, JSONB
|
|
101
|
+
- Parameterized queries (`$1`, `$2`, …)
|
|
102
|
+
- `ON CONFLICT DO NOTHING` for idempotent seeds
|
|
91
103
|
|
|
92
104
|
### MySQL
|
|
93
|
-
|
|
94
|
-
- AUTO_INCREMENT
|
|
105
|
+
- `AUTO_INCREMENT` primary keys
|
|
95
106
|
- TINYINT for booleans (0/1)
|
|
96
|
-
- JSON
|
|
97
|
-
- INSERT IGNORE for idempotent seeds
|
|
107
|
+
- JSON column type
|
|
108
|
+
- `INSERT IGNORE` for idempotent seeds
|
|
98
109
|
|
|
99
110
|
### SQLite
|
|
100
|
-
|
|
101
|
-
- AUTOINCREMENT
|
|
111
|
+
- `AUTOINCREMENT` primary keys
|
|
102
112
|
- INTEGER for booleans (0/1)
|
|
103
|
-
- INSERT OR IGNORE for idempotent seeds
|
|
113
|
+
- `INSERT OR IGNORE` for idempotent seeds
|
|
104
114
|
|
|
105
115
|
## Creating Migrations
|
|
106
116
|
|
|
107
|
-
|
|
117
|
+
Migration files are classes that extend `masterrecord.schema`. They must implement `async up(table)` and `async down(table)` methods. Always call `await this.init(table)` first inside each method.
|
|
118
|
+
|
|
119
|
+
### Creating a table
|
|
108
120
|
|
|
109
121
|
```javascript
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
down: function(table, schema) {
|
|
117
|
-
schema.dropTable(table.User);
|
|
122
|
+
import masterrecord from 'masterrecord';
|
|
123
|
+
|
|
124
|
+
class CreateUsers extends masterrecord.schema {
|
|
125
|
+
async up(table) {
|
|
126
|
+
await this.init(table);
|
|
127
|
+
await this.createTable(table.User);
|
|
118
128
|
}
|
|
119
|
-
|
|
129
|
+
|
|
130
|
+
async down(table) {
|
|
131
|
+
await this.init(table);
|
|
132
|
+
await this.dropTable(table.User);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export default CreateUsers;
|
|
120
137
|
```
|
|
121
138
|
|
|
122
|
-
### Adding
|
|
139
|
+
### Adding columns
|
|
123
140
|
|
|
124
141
|
```javascript
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
142
|
+
import masterrecord from 'masterrecord';
|
|
143
|
+
|
|
144
|
+
class AddPhoneToUser extends masterrecord.schema {
|
|
145
|
+
async up(table) {
|
|
146
|
+
await this.init(table);
|
|
147
|
+
await this.addColumn({
|
|
128
148
|
tableName: 'User',
|
|
129
149
|
name: 'phone_number',
|
|
130
|
-
type: 'string'
|
|
150
|
+
type: 'string',
|
|
131
151
|
});
|
|
132
|
-
}
|
|
152
|
+
}
|
|
133
153
|
|
|
134
|
-
down
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
name: 'phone_number'
|
|
138
|
-
});
|
|
154
|
+
async down(table) {
|
|
155
|
+
await this.init(table);
|
|
156
|
+
await this.dropColumn({ tableName: 'User', name: 'phone_number' });
|
|
139
157
|
}
|
|
140
|
-
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export default AddPhoneToUser;
|
|
141
161
|
```
|
|
142
162
|
|
|
143
|
-
### Modifying
|
|
163
|
+
### Modifying columns
|
|
144
164
|
|
|
145
165
|
```javascript
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
166
|
+
import masterrecord from 'masterrecord';
|
|
167
|
+
|
|
168
|
+
class MakeAgeRequired extends masterrecord.schema {
|
|
169
|
+
async up(table) {
|
|
170
|
+
await this.init(table);
|
|
171
|
+
await this.alterColumn({
|
|
149
172
|
tableName: 'User',
|
|
150
|
-
table: {
|
|
151
|
-
name: 'age',
|
|
152
|
-
type: 'integer',
|
|
153
|
-
nullable: false,
|
|
154
|
-
default: 0
|
|
155
|
-
}
|
|
173
|
+
table: { name: 'age', type: 'integer', nullable: false, default: 0 },
|
|
156
174
|
});
|
|
157
|
-
}
|
|
175
|
+
}
|
|
158
176
|
|
|
159
|
-
down
|
|
160
|
-
|
|
177
|
+
async down(table) {
|
|
178
|
+
await this.init(table);
|
|
179
|
+
await this.alterColumn({
|
|
161
180
|
tableName: 'User',
|
|
162
|
-
table: {
|
|
163
|
-
name: 'age',
|
|
164
|
-
type: 'integer',
|
|
165
|
-
nullable: true
|
|
166
|
-
}
|
|
181
|
+
table: { name: 'age', type: 'integer', nullable: true },
|
|
167
182
|
});
|
|
168
183
|
}
|
|
169
|
-
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export default MakeAgeRequired;
|
|
170
187
|
```
|
|
171
188
|
|
|
172
|
-
### Renaming
|
|
189
|
+
### Renaming columns
|
|
173
190
|
|
|
174
191
|
```javascript
|
|
175
|
-
|
|
176
|
-
up: function(table, schema) {
|
|
177
|
-
schema.renameColumn({
|
|
178
|
-
tableName: 'User',
|
|
179
|
-
name: 'username',
|
|
180
|
-
newName: 'user_name'
|
|
181
|
-
});
|
|
182
|
-
},
|
|
192
|
+
import masterrecord from 'masterrecord';
|
|
183
193
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
194
|
+
class RenameUsernameColumn extends masterrecord.schema {
|
|
195
|
+
async up(table) {
|
|
196
|
+
await this.init(table);
|
|
197
|
+
await this.renameColumn({ tableName: 'User', name: 'username', newName: 'user_name' });
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async down(table) {
|
|
201
|
+
await this.init(table);
|
|
202
|
+
await this.renameColumn({ tableName: 'User', name: 'user_name', newName: 'username' });
|
|
190
203
|
}
|
|
191
|
-
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export default RenameUsernameColumn;
|
|
192
207
|
```
|
|
193
208
|
|
|
194
209
|
## Seed Data
|
|
195
210
|
|
|
196
|
-
###
|
|
211
|
+
### Single-row seeding
|
|
197
212
|
|
|
198
213
|
```javascript
|
|
199
|
-
|
|
200
|
-
up: function(table, schema) {
|
|
201
|
-
schema.createTable(table.Role);
|
|
214
|
+
import masterrecord from 'masterrecord';
|
|
202
215
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
});
|
|
216
|
+
class SeedRoles extends masterrecord.schema {
|
|
217
|
+
async up(table) {
|
|
218
|
+
await this.init(table);
|
|
219
|
+
await this.createTable(table.Role);
|
|
208
220
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
});
|
|
213
|
-
},
|
|
221
|
+
this.seed('Role', { name: 'Admin', description: 'Administrator role' });
|
|
222
|
+
this.seed('Role', { name: 'User', description: 'Regular user role' });
|
|
223
|
+
}
|
|
214
224
|
|
|
215
|
-
down
|
|
216
|
-
|
|
225
|
+
async down(table) {
|
|
226
|
+
await this.init(table);
|
|
227
|
+
await this.dropTable(table.Role);
|
|
217
228
|
}
|
|
218
|
-
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export default SeedRoles;
|
|
219
232
|
```
|
|
220
233
|
|
|
221
|
-
### Bulk
|
|
234
|
+
### Bulk seeding (preferred)
|
|
222
235
|
|
|
223
236
|
```javascript
|
|
224
|
-
|
|
225
|
-
up: function(table, schema) {
|
|
226
|
-
schema.createTable(table.User);
|
|
237
|
+
import masterrecord from 'masterrecord';
|
|
227
238
|
|
|
228
|
-
|
|
229
|
-
|
|
239
|
+
class SeedUsers extends masterrecord.schema {
|
|
240
|
+
async up(table) {
|
|
241
|
+
await this.init(table);
|
|
242
|
+
await this.createTable(table.User);
|
|
243
|
+
|
|
244
|
+
this.bulkSeed('User', [
|
|
230
245
|
{ name: 'Alice', email: 'alice@example.com', age: 25 },
|
|
231
246
|
{ name: 'Bob', email: 'bob@example.com', age: 30 },
|
|
232
247
|
{ name: 'Charlie', email: 'charlie@example.com', age: 35 },
|
|
233
|
-
{ name: 'Diana', email: 'diana@example.com', age: 28 }
|
|
234
248
|
]);
|
|
235
|
-
}
|
|
249
|
+
}
|
|
236
250
|
|
|
237
|
-
down
|
|
238
|
-
|
|
251
|
+
async down(table) {
|
|
252
|
+
await this.init(table);
|
|
253
|
+
await this.dropTable(table.User);
|
|
239
254
|
}
|
|
240
|
-
}
|
|
241
|
-
```
|
|
255
|
+
}
|
|
242
256
|
|
|
243
|
-
|
|
257
|
+
export default SeedUsers;
|
|
258
|
+
```
|
|
244
259
|
|
|
245
|
-
Seeds are
|
|
260
|
+
Seeds are idempotent — re-running a migration won't create duplicate rows, because MasterRecord emits the database-native "insert or ignore" form (`INSERT OR IGNORE`, `INSERT IGNORE`, or `ON CONFLICT DO NOTHING`). This requires a unique constraint or primary key on the target table.
|
|
246
261
|
|
|
247
|
-
|
|
248
|
-
**MySQL**: Uses `INSERT IGNORE`
|
|
249
|
-
**PostgreSQL**: Uses `INSERT ... ON CONFLICT DO NOTHING`
|
|
262
|
+
### Conditional seeding by database
|
|
250
263
|
|
|
251
|
-
|
|
264
|
+
```javascript
|
|
265
|
+
import masterrecord from 'masterrecord';
|
|
252
266
|
|
|
253
|
-
|
|
267
|
+
class SeedSettings extends masterrecord.schema {
|
|
268
|
+
async up(table) {
|
|
269
|
+
await this.init(table);
|
|
270
|
+
await this.createTable(table.Setting);
|
|
254
271
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
up: function(table, schema) {
|
|
258
|
-
schema.createTable(table.Setting);
|
|
259
|
-
|
|
260
|
-
// Only seed if running on specific database
|
|
261
|
-
if(schema.context.isPostgres){
|
|
262
|
-
schema.seed('Setting', {
|
|
263
|
-
key: 'postgres_feature',
|
|
264
|
-
value: 'enabled'
|
|
265
|
-
});
|
|
272
|
+
if (this.context.isPostgres) {
|
|
273
|
+
this.seed('Setting', { key: 'postgres_feature', value: 'enabled' });
|
|
266
274
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
schema.seed('Setting', {
|
|
270
|
-
key: 'mysql_feature',
|
|
271
|
-
value: 'enabled'
|
|
272
|
-
});
|
|
275
|
+
if (this.context.isMySQL) {
|
|
276
|
+
this.seed('Setting', { key: 'mysql_feature', value: 'enabled' });
|
|
273
277
|
}
|
|
274
|
-
}
|
|
278
|
+
}
|
|
275
279
|
|
|
276
|
-
down
|
|
277
|
-
|
|
280
|
+
async down(table) {
|
|
281
|
+
await this.init(table);
|
|
282
|
+
await this.dropTable(table.Setting);
|
|
278
283
|
}
|
|
279
|
-
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export default SeedSettings;
|
|
280
287
|
```
|
|
281
288
|
|
|
282
289
|
## Migration Commands
|
|
283
290
|
|
|
284
|
-
### Create a New Migration
|
|
285
|
-
|
|
286
291
|
```bash
|
|
287
|
-
|
|
288
|
-
|
|
292
|
+
# Enable migrations for a context (creates snapshot)
|
|
293
|
+
masterrecord enable-migrations <ContextName>
|
|
289
294
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
masterrecord add-migration AddUserProfile context
|
|
293
|
-
```
|
|
295
|
+
# Generate a new migration from a diff of current entities vs snapshot
|
|
296
|
+
masterrecord add-migration <MigrationName> <ContextName>
|
|
294
297
|
|
|
295
|
-
|
|
298
|
+
# Apply all pending migrations
|
|
299
|
+
masterrecord update-database <ContextName>
|
|
296
300
|
|
|
297
|
-
|
|
298
|
-
masterrecord
|
|
301
|
+
# Operate on every context in the project at once
|
|
302
|
+
masterrecord enable-migrations-all
|
|
303
|
+
masterrecord add-migration-all <MigrationName>
|
|
304
|
+
masterrecord update-database-all
|
|
299
305
|
```
|
|
300
306
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
Migrations are tracked in the snapshot file:
|
|
304
|
-
```
|
|
305
|
-
app/models/db/migrations/context_contextSnapShot.json
|
|
306
|
-
```
|
|
307
|
+
Migration state is tracked in `<contextname>_contextSnapShot.json` alongside the migration files.
|
|
307
308
|
|
|
308
309
|
## Examples
|
|
309
310
|
|
|
310
|
-
### Example
|
|
311
|
+
### Example: E-commerce schema
|
|
311
312
|
|
|
312
313
|
```javascript
|
|
313
|
-
// migrations/
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
314
|
+
// app/models/db/migrations/1700000000000_CreateECommerce_migration.js
|
|
315
|
+
import masterrecord from 'masterrecord';
|
|
316
|
+
|
|
317
|
+
class CreateECommerce extends masterrecord.schema {
|
|
318
|
+
async up(table) {
|
|
319
|
+
await this.init(table);
|
|
320
|
+
|
|
321
|
+
await this.createTable(table.User);
|
|
322
|
+
await this.createTable(table.Category);
|
|
323
|
+
await this.createTable(table.Product);
|
|
324
|
+
await this.createTable(table.Order);
|
|
325
|
+
await this.createTable(table.OrderItem);
|
|
326
|
+
|
|
327
|
+
this.bulkSeed('Category', [
|
|
324
328
|
{ name: 'Electronics', slug: 'electronics' },
|
|
325
329
|
{ name: 'Clothing', slug: 'clothing' },
|
|
326
|
-
{ name: 'Books', slug: 'books' }
|
|
330
|
+
{ name: 'Books', slug: 'books' },
|
|
327
331
|
]);
|
|
328
332
|
|
|
329
|
-
|
|
330
|
-
schema.seed('User', {
|
|
333
|
+
this.seed('User', {
|
|
331
334
|
name: 'Admin',
|
|
332
335
|
email: 'admin@shop.com',
|
|
333
336
|
role: 'admin',
|
|
334
|
-
created_at: new Date()
|
|
335
337
|
});
|
|
336
338
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
{
|
|
340
|
-
name: 'Laptop',
|
|
341
|
-
price: 999.99,
|
|
342
|
-
category_id: 1,
|
|
343
|
-
stock: 50
|
|
344
|
-
},
|
|
345
|
-
{
|
|
346
|
-
name: 'T-Shirt',
|
|
347
|
-
price: 19.99,
|
|
348
|
-
category_id: 2,
|
|
349
|
-
stock: 200
|
|
350
|
-
}
|
|
339
|
+
this.bulkSeed('Product', [
|
|
340
|
+
{ name: 'Laptop', price: 999.99, category_id: 1, stock: 50 },
|
|
341
|
+
{ name: 'T-Shirt', price: 19.99, category_id: 2, stock: 200 },
|
|
351
342
|
]);
|
|
352
|
-
},
|
|
353
|
-
|
|
354
|
-
down: function(table, schema) {
|
|
355
|
-
schema.dropTable(table.OrderItem);
|
|
356
|
-
schema.dropTable(table.Order);
|
|
357
|
-
schema.dropTable(table.Product);
|
|
358
|
-
schema.dropTable(table.Category);
|
|
359
|
-
schema.dropTable(table.User);
|
|
360
343
|
}
|
|
361
|
-
};
|
|
362
|
-
```
|
|
363
|
-
|
|
364
|
-
### Example 2: Blog with PostgreSQL-Specific Features
|
|
365
|
-
|
|
366
|
-
```javascript
|
|
367
|
-
// migrations/20250111_CreateBlog.js
|
|
368
|
-
module.exports = {
|
|
369
|
-
up: function(table, schema) {
|
|
370
|
-
schema.createTable(table.Author);
|
|
371
|
-
schema.createTable(table.Post);
|
|
372
|
-
schema.createTable(table.Comment);
|
|
373
|
-
|
|
374
|
-
// PostgreSQL-specific: JSON metadata
|
|
375
|
-
if(schema.context.isPostgres){
|
|
376
|
-
schema.addColumn({
|
|
377
|
-
tableName: 'Post',
|
|
378
|
-
name: 'metadata',
|
|
379
|
-
type: 'jsonb' // PostgreSQL binary JSON
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
schema.addColumn({
|
|
383
|
-
tableName: 'Post',
|
|
384
|
-
name: 'post_id',
|
|
385
|
-
type: 'uuid' // PostgreSQL native UUID
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// Seed authors
|
|
390
|
-
schema.bulkSeed('Author', [
|
|
391
|
-
{ name: 'John Doe', email: 'john@blog.com', bio: 'Tech blogger' },
|
|
392
|
-
{ name: 'Jane Smith', email: 'jane@blog.com', bio: 'Travel writer' }
|
|
393
|
-
]);
|
|
394
|
-
|
|
395
|
-
// Seed initial posts
|
|
396
|
-
schema.seed('Post', {
|
|
397
|
-
title: 'Welcome to Our Blog',
|
|
398
|
-
content: 'This is our first post!',
|
|
399
|
-
author_id: 1,
|
|
400
|
-
published: true,
|
|
401
|
-
created_at: new Date()
|
|
402
|
-
});
|
|
403
|
-
},
|
|
404
344
|
|
|
405
|
-
down
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
345
|
+
async down(table) {
|
|
346
|
+
await this.init(table);
|
|
347
|
+
await this.dropTable(table.OrderItem);
|
|
348
|
+
await this.dropTable(table.Order);
|
|
349
|
+
await this.dropTable(table.Product);
|
|
350
|
+
await this.dropTable(table.Category);
|
|
351
|
+
await this.dropTable(table.User);
|
|
409
352
|
}
|
|
410
|
-
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export default CreateECommerce;
|
|
411
356
|
```
|
|
412
357
|
|
|
413
|
-
### Example
|
|
358
|
+
### Example: Multi-database-aware migration
|
|
414
359
|
|
|
415
360
|
```javascript
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
361
|
+
import masterrecord from 'masterrecord';
|
|
362
|
+
|
|
363
|
+
class AddUserPreferences extends masterrecord.schema {
|
|
364
|
+
async up(table) {
|
|
365
|
+
await this.init(table);
|
|
366
|
+
|
|
419
367
|
// Works across all databases
|
|
420
|
-
|
|
368
|
+
await this.addColumn({
|
|
421
369
|
tableName: 'User',
|
|
422
370
|
name: 'preferences',
|
|
423
|
-
type: 'text'
|
|
371
|
+
type: 'text',
|
|
424
372
|
});
|
|
425
373
|
|
|
426
|
-
//
|
|
427
|
-
if(
|
|
428
|
-
|
|
429
|
-
schema.alterColumn({
|
|
374
|
+
// PostgreSQL: upgrade to native JSONB
|
|
375
|
+
if (this.context.isPostgres) {
|
|
376
|
+
await this.alterColumn({
|
|
430
377
|
tableName: 'User',
|
|
431
|
-
table: {
|
|
432
|
-
name: 'preferences',
|
|
433
|
-
type: 'jsonb'
|
|
434
|
-
}
|
|
378
|
+
table: { name: 'preferences', type: 'jsonb' },
|
|
435
379
|
});
|
|
436
380
|
}
|
|
437
381
|
|
|
438
|
-
|
|
439
|
-
const defaultPrefs = JSON.stringify({
|
|
440
|
-
theme: 'light',
|
|
441
|
-
notifications: true,
|
|
442
|
-
language: 'en'
|
|
443
|
-
});
|
|
444
|
-
|
|
445
|
-
schema.seed('User', {
|
|
382
|
+
this.seed('User', {
|
|
446
383
|
name: 'Demo User',
|
|
447
384
|
email: 'demo@example.com',
|
|
448
|
-
preferences:
|
|
449
|
-
});
|
|
450
|
-
},
|
|
451
|
-
|
|
452
|
-
down: function(table, schema) {
|
|
453
|
-
schema.dropColumn({
|
|
454
|
-
tableName: 'User',
|
|
455
|
-
name: 'preferences'
|
|
385
|
+
preferences: JSON.stringify({ theme: 'light', notifications: true, language: 'en' }),
|
|
456
386
|
});
|
|
457
387
|
}
|
|
458
|
-
};
|
|
459
|
-
```
|
|
460
|
-
|
|
461
|
-
## Database-Specific Features
|
|
462
|
-
|
|
463
|
-
### PostgreSQL Features
|
|
464
|
-
|
|
465
|
-
```javascript
|
|
466
|
-
module.exports = {
|
|
467
|
-
up: function(table, schema) {
|
|
468
|
-
if(schema.context.isPostgres){
|
|
469
|
-
// JSONB for better JSON performance
|
|
470
|
-
schema.addColumn({
|
|
471
|
-
tableName: 'User',
|
|
472
|
-
name: 'settings',
|
|
473
|
-
type: 'jsonb'
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
// Native UUID support
|
|
477
|
-
schema.addColumn({
|
|
478
|
-
tableName: 'User',
|
|
479
|
-
name: 'uuid',
|
|
480
|
-
type: 'uuid'
|
|
481
|
-
});
|
|
482
388
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
name: 'active',
|
|
487
|
-
type: 'boolean',
|
|
488
|
-
default: true
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
// BYTEA for binary data
|
|
492
|
-
schema.addColumn({
|
|
493
|
-
tableName: 'User',
|
|
494
|
-
name: 'avatar',
|
|
495
|
-
type: 'binary' // Maps to BYTEA in PostgreSQL
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
},
|
|
499
|
-
|
|
500
|
-
down: function(table, schema) {
|
|
501
|
-
// Cleanup
|
|
502
|
-
}
|
|
503
|
-
};
|
|
504
|
-
```
|
|
505
|
-
|
|
506
|
-
### MySQL Features
|
|
507
|
-
|
|
508
|
-
```javascript
|
|
509
|
-
module.exports = {
|
|
510
|
-
up: function(table, schema) {
|
|
511
|
-
if(schema.context.isMySQL){
|
|
512
|
-
// JSON type
|
|
513
|
-
schema.addColumn({
|
|
514
|
-
tableName: 'User',
|
|
515
|
-
name: 'settings',
|
|
516
|
-
type: 'json'
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
// TINYINT for booleans (0/1)
|
|
520
|
-
schema.addColumn({
|
|
521
|
-
tableName: 'User',
|
|
522
|
-
name: 'active',
|
|
523
|
-
type: 'boolean' // Maps to TINYINT
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
// AUTO_INCREMENT
|
|
527
|
-
// (handled automatically for primary keys)
|
|
528
|
-
}
|
|
529
|
-
},
|
|
530
|
-
|
|
531
|
-
down: function(table, schema) {
|
|
532
|
-
// Cleanup
|
|
533
|
-
}
|
|
534
|
-
};
|
|
535
|
-
```
|
|
536
|
-
|
|
537
|
-
### SQLite Features
|
|
538
|
-
|
|
539
|
-
```javascript
|
|
540
|
-
module.exports = {
|
|
541
|
-
up: function(table, schema) {
|
|
542
|
-
if(schema.context.isSQLite){
|
|
543
|
-
// TEXT for everything
|
|
544
|
-
// INTEGER for booleans (0/1)
|
|
545
|
-
// AUTOINCREMENT for primary keys
|
|
546
|
-
|
|
547
|
-
// SQLite requires table rebuild for certain alterations
|
|
548
|
-
// MasterRecord handles this automatically
|
|
549
|
-
}
|
|
550
|
-
},
|
|
551
|
-
|
|
552
|
-
down: function(table, schema) {
|
|
553
|
-
// Cleanup
|
|
554
|
-
}
|
|
555
|
-
};
|
|
556
|
-
```
|
|
557
|
-
|
|
558
|
-
## Best Practices
|
|
559
|
-
|
|
560
|
-
### 1. Always Provide Down Migrations
|
|
561
|
-
|
|
562
|
-
```javascript
|
|
563
|
-
// ✅ GOOD
|
|
564
|
-
module.exports = {
|
|
565
|
-
up: function(table, schema) {
|
|
566
|
-
schema.createTable(table.User);
|
|
567
|
-
},
|
|
568
|
-
down: function(table, schema) {
|
|
569
|
-
schema.dropTable(table.User);
|
|
570
|
-
}
|
|
571
|
-
};
|
|
572
|
-
|
|
573
|
-
// ❌ BAD
|
|
574
|
-
module.exports = {
|
|
575
|
-
up: function(table, schema) {
|
|
576
|
-
schema.createTable(table.User);
|
|
577
|
-
},
|
|
578
|
-
down: function(table, schema) {
|
|
579
|
-
// Empty - can't rollback!
|
|
389
|
+
async down(table) {
|
|
390
|
+
await this.init(table);
|
|
391
|
+
await this.dropColumn({ tableName: 'User', name: 'preferences' });
|
|
580
392
|
}
|
|
581
|
-
}
|
|
582
|
-
```
|
|
583
|
-
|
|
584
|
-
### 2. Use Bulk Seeding for Multiple Records
|
|
585
|
-
|
|
586
|
-
```javascript
|
|
587
|
-
// ✅ GOOD - Single query
|
|
588
|
-
schema.bulkSeed('User', [
|
|
589
|
-
{ name: 'Alice', email: 'alice@example.com' },
|
|
590
|
-
{ name: 'Bob', email: 'bob@example.com' }
|
|
591
|
-
]);
|
|
592
|
-
|
|
593
|
-
// ❌ BAD - Multiple queries
|
|
594
|
-
schema.seed('User', { name: 'Alice', email: 'alice@example.com' });
|
|
595
|
-
schema.seed('User', { name: 'Bob', email: 'bob@example.com' });
|
|
596
|
-
```
|
|
597
|
-
|
|
598
|
-
### 3. Test Migrations on All Databases
|
|
599
|
-
|
|
600
|
-
If your app supports multiple databases, test migrations on each:
|
|
601
|
-
|
|
602
|
-
```bash
|
|
603
|
-
# Test on SQLite
|
|
604
|
-
DATABASE_TYPE=sqlite masterrecord migrate context
|
|
605
|
-
|
|
606
|
-
# Test on MySQL
|
|
607
|
-
DATABASE_TYPE=mysql masterrecord migrate context
|
|
608
|
-
|
|
609
|
-
# Test on PostgreSQL
|
|
610
|
-
DATABASE_TYPE=postgres masterrecord migrate context
|
|
611
|
-
```
|
|
612
|
-
|
|
613
|
-
### 4. Keep Migrations Small and Focused
|
|
614
|
-
|
|
615
|
-
```javascript
|
|
616
|
-
// ✅ GOOD - One logical change
|
|
617
|
-
// Migration: AddUserPhoneNumber
|
|
618
|
-
schema.addColumn({
|
|
619
|
-
tableName: 'User',
|
|
620
|
-
name: 'phone',
|
|
621
|
-
type: 'string'
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
// ❌ BAD - Too many unrelated changes
|
|
625
|
-
schema.addColumn({ tableName: 'User', name: 'phone', type: 'string' });
|
|
626
|
-
schema.addColumn({ tableName: 'Post', name: 'views', type: 'integer' });
|
|
627
|
-
schema.createTable(table.Comment);
|
|
628
|
-
schema.seed('Setting', { key: 'version', value: '2.0' });
|
|
629
|
-
```
|
|
630
|
-
|
|
631
|
-
### 5. Use Timestamps for Migration Names
|
|
632
|
-
|
|
633
|
-
MasterRecord automatically adds timestamps to migration files:
|
|
634
|
-
```
|
|
635
|
-
20250111_143052_CreateUserTable.js
|
|
636
|
-
20250111_150023_AddUserEmail.js
|
|
637
|
-
```
|
|
638
|
-
|
|
639
|
-
This ensures migrations run in the correct order.
|
|
640
|
-
|
|
641
|
-
## Troubleshooting
|
|
642
|
-
|
|
643
|
-
### Migration Not Found
|
|
644
|
-
|
|
645
|
-
**Error**: "Cannot find migration file"
|
|
646
|
-
|
|
647
|
-
**Solution**: Ensure migration is in the correct directory:
|
|
648
|
-
```
|
|
649
|
-
app/models/db/migrations/
|
|
650
|
-
```
|
|
651
|
-
|
|
652
|
-
### Context Not Found
|
|
653
|
-
|
|
654
|
-
**Error**: "Cannot find context"
|
|
655
|
-
|
|
656
|
-
**Solution**: Provide full path or context name:
|
|
657
|
-
```bash
|
|
658
|
-
masterrecord migrate context
|
|
659
|
-
# or
|
|
660
|
-
masterrecord migrate ./app/models/context.js
|
|
661
|
-
```
|
|
662
|
-
|
|
663
|
-
### Seed Data Not Idempotent
|
|
664
|
-
|
|
665
|
-
**Error**: Duplicate key violations on repeated migrations
|
|
666
|
-
|
|
667
|
-
**Solution**: Ensure tables have primary keys or unique constraints:
|
|
668
|
-
```javascript
|
|
669
|
-
schema.createTable(table.User); // User entity must have primary key
|
|
670
|
-
schema.seed('User', { id: 1, name: 'Admin' }); // Include PK in seed
|
|
671
|
-
```
|
|
672
|
-
|
|
673
|
-
### PostgreSQL Permission Errors
|
|
674
|
-
|
|
675
|
-
**Error**: "permission denied for table"
|
|
393
|
+
}
|
|
676
394
|
|
|
677
|
-
|
|
678
|
-
```sql
|
|
679
|
-
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
|
|
680
|
-
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
|
|
395
|
+
export default AddUserPreferences;
|
|
681
396
|
```
|
|
682
397
|
|
|
683
|
-
##
|
|
684
|
-
|
|
685
|
-
- **MasterRecord**: 0.3.0+
|
|
686
|
-
- **PostgreSQL**: 9.6+ (tested with 12+, 13+, 14+, 15+, 16+)
|
|
687
|
-
- **MySQL**: 5.7+ (tested with 8.0+)
|
|
688
|
-
- **SQLite**: 3.x (any recent version)
|
|
689
|
-
- **Node.js**: 14+ (async/await support required)
|
|
690
|
-
|
|
691
|
-
## Additional Resources
|
|
692
|
-
|
|
693
|
-
- [PostgreSQL Setup Guide](./POSTGRESQL_SETUP.md)
|
|
694
|
-
- [Entity Definitions](./ENTITIES.md)
|
|
695
|
-
- [MasterRecord API Reference](./API_REFERENCE.md)
|
|
696
|
-
|
|
697
|
-
## License
|
|
398
|
+
## Best practices
|
|
698
399
|
|
|
699
|
-
|
|
400
|
+
1. **Always provide a `down` migration.** Even if it just reverses `createTable` with `dropTable`. It pays off the first time a migration needs to be rolled back.
|
|
401
|
+
2. **Prefer `bulkSeed` over multiple `seed` calls.** It's one INSERT instead of N.
|
|
402
|
+
3. **Test migrations on every target database.** If your app supports MySQL and Postgres, run both sets of migrations in CI.
|
|
403
|
+
4. **Keep migrations small and focused.** One logical change per migration file.
|
|
404
|
+
5. **Never edit an already-applied migration.** Create a new migration that corrects or rolls back the earlier one.
|