Created
June 20, 2017 20:20
-
-
Save sgress454/dcd3e2f1584335be2d619fcc4f7d32d7 to your computer and use it in GitHub Desktop.
Pre-commit hook to block commits that change models without adding a migration script
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
#!/Users/scottmac/.nvm/v6.3.0/bin/node | |
/** | |
* Pre-commit hook to prevent commits that change models unless they | |
* also include a migration file. | |
* | |
* To skip, use the `--no-verify` option when committing. | |
* (useful if you just added a comment to a model) | |
*/ | |
var exec = require('child_process').exec; | |
var _ = require('@sailshq/lodash'); | |
// Get the current Git status (in shorthand mode) | |
exec('git status -s', function(err, stdout, stderr) { | |
if (err) { process.exit(0); } | |
// Split into individual lines. | |
var lines = stdout.split('\n'); | |
// Flag indicating that a model file has been modified. | |
var modifiedModel = false; | |
// Flag indicating that a migration file has been added. | |
var addedMigration = false; | |
// Loop through each line in the output, checking for entries about model files or migrations. | |
_.each(lines, function(line) { | |
if (line.match(/^[MAD]\s+api\/models\//)) { modifiedModel = true; } | |
if (line.match(/^A\s+db\/migrations\//)) { addedMigration = true; } | |
}); | |
// If a model was modified, but no migration was added, bail. | |
if (modifiedModel && !addedMigration) { | |
console.error('Attempted to commit a new/modified/deleted model without a migration file!'); | |
process.exit(1); | |
} | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment