Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·240 lines (220 loc) · 6.25 KB
/
Copy pathcli.js
File metadata and controls
executable file
·240 lines (220 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env node
import { Command } from 'commander';
import {
displayInfo,
fastForward,
generateMigration,
getLatestIntrospection,
introspectDatabase,
migrate,
rollback,
saveIntrospection
} from './index.js';
import { loadConfig } from './lib/configuration.js';
import { getLogger } from './lib/logger.js';
import { connectToDatabase } from './lib/surrealdb.js';
import packageData from './package.json' with { type: 'json' };
const logger = getLogger('SurrealDB');
/**
* Main command line interface
*/
const program = new Command();
program
.name('surrealigrate')
.description(
'SurrealDB migration CLI tool for managing database schema changes'
)
.version(packageData.version)
.option('-c, --config <path>', 'path to YAML configuration file')
.option('-d, --dir <path>', 'directory containing migration files')
.addHelpText(
'after',
`
Example calls:
$ npm run migrate
$ npm run migrate --to 5
$ npm run rollback
$ npm run rollback --to 3
$ npm run info
$ npm run extract
$ npm run generate -- --name "migration-title"
$ npm run generate -- -d ./migrations/pending -n "migration-title"
Configuration:
This tool can be configured using a YAML file, environment variables, or a combination of both.
Priority order: Environment Variables > YAML Config > Default Config
Environment Variables:
SURREAL_URL SurrealDB connection URL
SURREAL_USER Database user
SURREAL_PASS Database password
SURREAL_NAMESPACE Database namespace
SURREAL_DATABASE Database name
SURREAL_MIGRATIONS_DIGITS Number of digits to use for migration version
SURREAL_MIGRATIONS_FOLDER Folder to store migration files
For more information on each command, use: npm run help:[command]
`
);
/**
* Apply pending migrations to the database
*/
program
.command('migrate')
.description('Apply pending migrations to the database')
.option('--to <version>', 'migrate to a specific version')
.addHelpText(
'after',
`
Examples:
$ npm run migrate
$ npm run migrate --to 5
$ npm run migrate -d ./custom-migrations
This command will apply all pending migrations or migrate to a specific version if --to is specified.
Migration files should be named in the format: <version>.<do|undo>.<title>.surql
`
)
.action(async (options) => {
if (program.opts().config) {
await loadConfig(program.opts().config);
}
await migrate(program.opts().dir, options.to);
});
/**
* Fast forward to generated migrations
*/
program
.command('fastforward')
.description('Fast forward to generated migrations')
.addHelpText(
'after',
`
Examples:
$ npm run fastforward
$ npm run fastforward -d ./custom-migrations
This command will fast forward the migration state to the last generated migration.
`
)
.action(async () => {
if (program.opts().config) {
await loadConfig(program.opts().config);
}
await fastForward(program.opts().dir);
});
/**
* Rollback applied migrations
*/
program
.command('rollback')
.description('Rollback applied migrations')
.option('--to <version>', 'rollback to a specific version')
.addHelpText(
'after',
`
Examples:
$ npm run rollback
$ npm run rollback --to 3
$ npm run rollback -d ./custom-migrations
This command will rollback the last applied migration or rollback to a specific version if --to is specified.
`
)
.action(async (options) => {
if (program.opts().config) {
await loadConfig(program.opts().config);
}
await rollback(program.opts().dir, options.to);
});
/**
* Display information about the current migration status
*/
program
.command('info')
.description('Display information about the current migration status')
.addHelpText(
'after',
`
Example:
$ npm run info
This command will display:
- Current version applied to the database
- Latest available migration version
- List of pending migrations (if any)
`
)
.action(async () => {
if (program.opts().config) {
await loadConfig(program.opts().config);
}
await displayInfo(program.opts().dir);
});
/**
* Extract the current database structure and store schema introspection data
*/
program
.command('extract')
.description(
'Inspect the current database structure and store schema introspection data'
)
.addHelpText(
'after',
`
Example:
$ npm run extract
This command will:
- Inspect the current database structure
- Store schema introspection data in the database
`
)
.action(async () => {
if (program.opts().config) {
await loadConfig(program.opts().config);
}
await connectToDatabase();
const introspectionData = await introspectDatabase();
await saveIntrospection(introspectionData.introspection);
});
/**
* Generate migration files
* - Must be run after extract.
*/
program
.command('generate')
.description(
'Analyze an updated database state, generate migration files, update migration status, and update stored introspection data. Must be run after extract.'
)
.option('-n, --name <title>', 'title of the migration')
.addHelpText(
'after',
`
Example:
$ npm run generate
$ npm run generate -- --name "migration-title"
$ npm run generate -- -d ./migrations/pending -n "migration-title"
This command will:
- Analyze the updated database state
- Generate migration files
- Store introspection data in the database
`
)
.action(async (options) => {
if (program.opts().config) {
await loadConfig(program.opts().config);
}
await connectToDatabase();
const latestIntrospection = await getLatestIntrospection();
if (!latestIntrospection?.data) {
logger.warn(
'The generate command does not make any database changes and therefore cannot initiate a stored intropection.'
);
logger.info(
'Please run extract command first, this initializes the stored introspection data, and provides comparison data for the generate command.\n'
);
return;
}
const currentIntrospection = await introspectDatabase();
await generateMigration(
latestIntrospection.data,
currentIntrospection,
options.name
);
//await saveIntrospection(currentIntrospection);
});
await program.parseAsync(process.argv);
process.exit(0);