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
Separate migration generator #2786
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import mkdirp from 'mkdirp'; | ||
import Promise from 'bluebird'; | ||
import { template } from 'lodash'; | ||
import { getMergedConfig } from './Migrator'; | ||
|
||
export default class MigrationGenerator { | ||
constructor(migrationConfig) { | ||
this.config = getMergedConfig(migrationConfig); | ||
} | ||
|
||
// Creates a new migration, with a given name. | ||
make(name, config) { | ||
this.config = getMergedConfig(config, this.config); | ||
if (!name) { | ||
return Promise.reject( | ||
new Error('A name must be specified for the generated migration') | ||
); | ||
} | ||
|
||
return this._ensureFolder(config) | ||
.then((val) => this._generateStubTemplate(val)) | ||
.then((val) => this._writeNewMigration(name, val)); | ||
} | ||
|
||
// Ensures a folder for the migrations exist, dependent on the migration | ||
// config settings. | ||
_ensureFolder() { | ||
const dirs = this._absoluteConfigDirs(); | ||
|
||
const promises = dirs.map((dir) => { | ||
return Promise.promisify(fs.stat, { context: fs })(dir).catch(() => | ||
Promise.promisify(mkdirp)(dir) | ||
); | ||
}); | ||
|
||
return Promise.all(promises); | ||
} | ||
|
||
// Generates the stub template for the current migration, returning a compiled | ||
// template. | ||
_generateStubTemplate() { | ||
const stubPath = | ||
this.config.stub || | ||
path.join(__dirname, 'stub', this.config.extension + '.stub'); | ||
|
||
return Promise.promisify(fs.readFile, { context: fs })(stubPath).then( | ||
(stub) => template(stub.toString(), { variable: 'd' }) | ||
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. Can this be rewritten using string template? |
||
); | ||
} | ||
|
||
// Write a new migration to disk, using the config and generated filename, | ||
// passing any `variables` given in the config to the template. | ||
_writeNewMigration(name, tmpl) { | ||
const { config } = this; | ||
const dirs = this._absoluteConfigDirs(); | ||
const dir = dirs.slice(-1)[0]; // Get last specified directory | ||
|
||
if (name[0] === '-') name = name.slice(1); | ||
const filename = yyyymmddhhmmss() + '_' + name + '.' + config.extension; | ||
|
||
return Promise.promisify(fs.writeFile, { context: fs })( | ||
path.join(dir, filename), | ||
tmpl(config.variables || {}) | ||
).return(path.join(dir, filename)); | ||
} | ||
|
||
_absoluteConfigDirs() { | ||
const directories = Array.isArray(this.config.directory) | ||
? this.config.directory | ||
: [this.config.directory]; | ||
return directories.map((directory) => { | ||
return path.resolve(process.cwd(), directory); | ||
}); | ||
} | ||
} | ||
|
||
// Ensure that we have 2 places for each of the date segments. | ||
function padDate(segment) { | ||
segment = segment.toString(); | ||
return segment[1] ? segment : `0${segment}`; | ||
} | ||
|
||
// Get a date object in the correct format, without requiring a full out library | ||
// like "moment.js". | ||
function yyyymmddhhmmss() { | ||
const d = new Date(); | ||
return ( | ||
d.getFullYear().toString() + | ||
padDate(d.getMonth() + 1) + | ||
padDate(d.getDate()) + | ||
padDate(d.getHours()) + | ||
padDate(d.getMinutes()) + | ||
padDate(d.getSeconds()) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this be replaced with native Promise?
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.
When I did that, linting failed. Thoughts?
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.
There should be no need to focus on using native promises since knex already depends heavily on bluebird.
There's been suggestions before to not have this dependency - or at the very least the option to supply promise lib/native - but I believe until such a decision is made, it's fine to keep using bluebird.
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.
@wubzz
#1588 -> there is very clearly communicated intent to eventually go away off using bluebird, hence I see why not to avert using it in newly touched code.
@JakeGinnivan What was the linting error?
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.
@JakeGinnivan Ah, most likely linting error was due to use of Promise.promisify -> you can't just remove reference to Promise here, instead import promisify explicitly.
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.
@kibertoad Afaik that comment is strictly in regards to the "utility" functions exposed by knex (such as
.map
etc) and TS typings, not removing bluebird as a whole. Especially not as a first step.In any case it's not on the board for the next version of knex and certainly not set in stone. Contributors should not have to take this potential future change into consideration when developing features in present time, imho.
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.
Linting error was Promise is not defined