-
-
Notifications
You must be signed in to change notification settings - Fork 129
beforeMigration option #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
20ab166
2f2ae82
4cb3204
90da4f5
eb4666e
4e6b01e
8bb6d7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -131,6 +131,46 @@ const store = new Conf({ | |||||
|
||||||
> Note: The version the migrations use refers to the **project version** by default. If you want to change this behavior, specify the [`projectVersion`](#projectVersion) option. | ||||||
|
||||||
### beforeMigration | ||||||
|
||||||
Type: `Function`\ | ||||||
Default: `undefined` | ||||||
|
||||||
You can use `beforeMigration` callback for logging purposes, preparing migration data, etc. | ||||||
|
||||||
`beforeMigration` callback will be called before the migration is executed. | ||||||
|
||||||
It could be useful when u have multiple configs and you want to log the migration data for each config. | ||||||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Example: | ||||||
|
||||||
```js | ||||||
const Conf = require('conf'); | ||||||
|
||||||
console.log = someLogger.log; | ||||||
|
||||||
const mainConfig = new Conf({ | ||||||
beforeMigration: (store, currentVersion, nextVersion) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if it were like this.
Suggested change
That way we could easily add more metadata properties in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the properties maybe be named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could maybe also add the package version and all the migration versions. In case the user wants to know where in the migration process they are. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to keep them top-level in |
||||||
console.log(`[main-config] migrate from ${currentVersion} -> ${nextVersion}`); | ||||||
}, | ||||||
migrations: { | ||||||
'0.4.0': store => { | ||||||
store.set('debugPhase', true); | ||||||
}, | ||||||
} | ||||||
}); | ||||||
Ciberusps marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const secondConfig = new Conf({ | ||||||
beforeMigration: (store, currentVersion, nextVersion) => { | ||||||
console.log(`[second-config] migrate from ${currentVersion} -> ${nextVersion}`); | ||||||
}, | ||||||
migrations: { | ||||||
'1.0.1': store => { | ||||||
store.set('debugPhase', true); | ||||||
}, | ||||||
} | ||||||
}); | ||||||
``` | ||||||
|
||||||
#### configName | ||||||
|
||||||
Type: `string`\ | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -103,6 +103,16 @@ export interface Options<T extends Record<string, any>> { | |||||||
*/ | ||||||||
migrations?: Migrations<T>; | ||||||||
|
||||||||
/** | ||||||||
You can use `beforeMigration` callback for logging purposes, preparing migration data, etc. | ||||||||
|
||||||||
`beforeMigration` callback will be called before the migration is executed. | ||||||||
|
||||||||
It could be useful when u have multiple configs and you want to log the migration data for each config. | ||||||||
|
||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
beforeMigration?: BeforeMigrationCallback<T>; | ||||||||
|
||||||||
/** | ||||||||
__You most likely don't need this. Please don't use it unless you really have to.__ | ||||||||
|
||||||||
|
@@ -226,6 +236,7 @@ export interface Options<T extends Record<string, any>> { | |||||||
} | ||||||||
|
||||||||
export type Migrations<T extends Record<string, any>> = Record<string, (store: Conf<T>) => void>; | ||||||||
export type BeforeMigrationCallback<T extends Record<string, any>> = (store: Conf<T>, currentVersion: string, previousVersion: string) => void; | ||||||||
|
||||||||
export type Schema<T> = {[Property in keyof T]: ValueSchema}; | ||||||||
export type ValueSchema = TypedJSONSchema; | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1203,3 +1203,22 @@ test('__internal__ keys - should only match specific "__internal__" entry', t => | |||||
conf.set('__internal__foo.you-shall', 'not-pass'); | ||||||
}); | ||||||
}); | ||||||
|
||||||
test('beforeMigration - should be called before every migration', t => { | ||||||
const conf = new Conf({ | ||||||
cwd: tempy.directory(), | ||||||
projectVersion: '2.0.0', | ||||||
beforeMigration: (store, currentVersion, nextVersion) => { | ||||||
store.set(`beforeMigration ${currentVersion} -> ${nextVersion}`, true); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}, | ||||||
migrations: { | ||||||
'1.0.0': () => {}, | ||||||
'1.0.1': () => {}, | ||||||
'2.0.1': () => {} | ||||||
} | ||||||
}); | ||||||
|
||||||
t.is(conf.get('beforeMigration 0.0.0 -> 1.0.0'), true); | ||||||
t.is(conf.get('beforeMigration 1.0.0 -> 1.0.1'), true); | ||||||
t.false(conf.has('beforeMigration 1.0.1 -> 2.0.1')); | ||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ambiguous. Before the whole process or before each step.