Skip to content

Commit 91541f3

Browse files
Tomas WinklerTomas Winkler
authored andcommitted
Explain order in the readme and introduce IRange interface
1 parent aca46f0 commit 91541f3

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ The supported commands are divided into groups according to their target, at thi
9696
* The file is stored in the `Migrations` directory within the root of your repository.
9797
* Add your migration script in the body of the `run` function using the [Kontent Management SDK](https://github.com/Kentico/kontent-management-sdk-js) that was injected via the `apiClient` parameter.
9898
* To choose between JavaScript and TypeScript when generating the script file, use the `--template-type` option, such as `--template-type "javascript"`.
99-
* The migration template contains an `order` property that is used to run a batch of migrations in the specified order.
99+
* The migration template contains an `order` property that is used to run a batch of migrations (range or all) in the specified order. The `order` must be a unique, positive integer or zero. There may be gaps between migrations, for example, the following sequence is perfectly fine 0,3,4,5,10
100100

101101
```typescript
102102
// Example migration template

src/cmds/migration/run.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { environmentConfigExists, getEnvironmentsConfig } from '../../utils/envi
66
import { createManagementClient } from '../../managementClientFactory';
77
import { loadMigrationsExecutionStatus } from '../../utils/statusManager';
88
import { IMigration } from '../../models/migration';
9+
import { IRange } from '../../models/range';
910

1011
const runMigrationCommand: yargs.CommandModule = {
1112
command: 'run',
@@ -182,15 +183,20 @@ const runMigrationCommand: yargs.CommandModule = {
182183
},
183184
};
184185

185-
export const getRange = (range: string): [number, number] | null => {
186+
export const getRange = (range: string): IRange | null => {
186187
const match = range.match(/^([0-9]+):([0-9]+)$/);
187188
if (!match) {
188189
return null;
189190
}
190191
const from = Number(match[1]);
191192
const to = Number(match[2]);
192193

193-
return from <= to ? [from, to] : null;
194+
return from <= to
195+
? {
196+
from,
197+
to,
198+
}
199+
: null;
194200
};
195201

196202
const checkForDuplicates = (migrationsToRun: IMigration[]): void => {
@@ -204,11 +210,11 @@ const checkForDuplicates = (migrationsToRun: IMigration[]): void => {
204210
}
205211
};
206212

207-
const getMigrationsByRange = (migrationsToRun: IMigration[], range: [number, number]): IMigration[] => {
213+
const getMigrationsByRange = (migrationsToRun: IMigration[], range: IRange): IMigration[] => {
208214
const migrations: IMigration[] = [];
209215

210216
for (const migration of migrationsToRun) {
211-
if (migration.module.order >= range[0] && migration.module.order <= range[1]) {
217+
if (migration.module.order >= range.from && migration.module.order <= range.to) {
212218
migrations.push(migration);
213219
}
214220
}

src/models/range.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IRange {
2+
from: number;
3+
to: number;
4+
}

src/tests/invalidOrderMigration.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ describe('Detection of invalid orders', () => {
5353
},
5454
{
5555
module: {
56-
order: 3,
56+
order: 6,
57+
name: 'test',
58+
},
59+
},
60+
{
61+
module: {
62+
order: 7,
5763
name: 'test',
5864
},
5965
},

0 commit comments

Comments
 (0)