Add option 'filePrefix' to tighten which files are classed as patch files

This commit is contained in:
Andrew Chilton 2015-03-16 12:54:46 +13:00
Родитель 060ad14a61
Коммит 4a99cfd2f8
7 изменённых файлов: 49 добавлений и 12 удалений

Просмотреть файл

@ -19,6 +19,7 @@ var options = {
dir : path.join(__dirname, 'schema'),
patchKey : 'schema-patch-level',
patchLevel : 4,
filePrefix : 'patch',
metaTable : 'metadata',
mysql : mysql,
}
@ -56,6 +57,7 @@ Specific options for `mysql-patcher`:
* patchKey : string - the name of the row in the metaTable which stores the current patch
* createDatabase : true/false - tries to create the database if it doesn't exist (default: false)
* reversePatchAllowed : true/false - allow reverse patching to take place (default: false)
* filePrefix : string - the patchfile prefix to look for e.g. patch-001-002.sql (default: 'patch')
## Database Patch Files ##
@ -113,6 +115,7 @@ UPDATE metadata SET value = '1' WHERE name = 'schema-patch-level';
### Pending ###
* added option 'filePrefix' to tighten which files are classed as patch files
* fixed a test related to access for an unknown user
### v0.6.1 - 2015-02-02 ###

Просмотреть файл

@ -9,6 +9,7 @@ var path = require('path')
// npm
var async = require('async')
var clone = require('clone')
var glob = require('glob')
// globals for this package
var noop = Function.prototype // a No Op function
@ -40,6 +41,7 @@ function Patcher(options) {
this.options.reversePatchAllowed = this.options.reversePatchAllowed || false
this.options.patchKey = this.options.patchKey || 'patch'
this.options.createDatabase = this.options.createDatabase || false
this.options.filePrefix = this.options.filePrefix || 'patch'
// set this on the connection since we can have multiple statements
// in every patch
@ -191,33 +193,60 @@ Patcher.prototype.readDbPatchLevel = function readDbPatchLevel(callback) {
)
}
function extractBaseAndLevels(filename) {
var basename = path.basename(filename, '.sql')
var parts = basename.split('-')
if ( parts.length < 3 ) {
return false
}
if ( parts.length > 3 ) {
parts = [ parts.slice(0, parts.length - 2).join('-'), parts[parts.length-2], parts[parts.length-1] ]
}
// now parts is of length 3
return {
base : parts[0],
from : parseInt(parts[1], 10) || 0, // fixes parseInt('a', 10) => NaN
to : parseInt(parts[2], 10) || 0, // fixes parseInt('b', 10) => NaN
}
}
Patcher.prototype.readPatchFiles = function readPatchFiles(callback) {
this.patches = {}
fs.readdir(this.options.dir, (function(err, files) {
var globSpec = path.join(this.options.dir, this.options.filePrefix) + '-*-*.sql'
glob(globSpec, (function(err, files) {
if (err) return callback(err)
files = files.map((function(filename) {
return path.join(this.options.dir, filename)
}).bind(this))
async.eachLimit(
files,
10,
(function(filename, done) {
var m = filename.match(/-(\d+)-(\d+)\.sql$/)
if ( !m ) {
return done(new Error('Unknown file format: ' + filename))
var info = extractBaseAndLevels(filename)
if ( !info ) {
// don't look at this file since it doesn't match the spec
return done()
}
var from = parseInt(m[1], 10)
var to = parseInt(m[2], 10)
this.patches[from] = this.patches[from] || {}
// check the base is what we expect
if ( info.base !== this.options.filePrefix ) {
return done()
}
// check we have a 'from' and 'to'
if ( info.from === 0 && info.to === 0 ) {
return done()
}
this.patches[info.from] = this.patches[info.from] || {}
fs.readFile(filename, { encoding : 'utf8' }, (function(err, data) {
if (err) return done(err)
this.patches[from][to] = data
this.patches[info.from][info.to] = data
done()
}).bind(this))
}).bind(this),

Просмотреть файл

@ -23,6 +23,7 @@
"async": "^0.9.0",
"bluebird": "^2.3.0",
"clone": "^0.1.18",
"glob": "^5.0.3",
"xtend": "^4.0.0"
},
"devDependencies": {

1
test/patches/ignored.txt Normal file
Просмотреть файл

@ -0,0 +1 @@
This file is ignored, since we tell mysql-patcher to look for files with a particular filename.

Просмотреть файл

@ -0,0 +1 @@
This file is also ignored since it has the wrong extension (even though the basename look okay).

Просмотреть файл

@ -0,0 +1 @@
-- This file is ignored since the <from> and <to> parts of the filename are non-numeric.

Просмотреть файл

@ -0,0 +1 @@
-- This file is ignored since it does not begin with the required prefix. ie. <prefix>-<from>-<to>.sql.