postgrator
Advanced tools
Comparing version 3.7.0 to 3.8.0
# CHANGELOG | ||
## 3.8.0 | ||
### Feb 17 2019 | ||
- Add currentSchema option for Postgres | ||
## 3.7.0 | ||
@@ -4,0 +10,0 @@ |
@@ -120,2 +120,4 @@ const utils = require('./utils') | ||
schemaSql = `AND table_schema = '${schema[0]}'` | ||
} else if (config.currentSchema) { | ||
schemaSql = `AND table_schema = '${config.currentSchema}'` | ||
} | ||
@@ -273,2 +275,3 @@ | ||
) | ||
// pg 6.x does not return promise on connect() | ||
@@ -279,2 +282,11 @@ // This wrapper should allow 6.x and 7.x compatibility | ||
if (err) return reject(err) | ||
if (config.currentSchema) { | ||
return commonClient.dbConnection.query( | ||
`SET search_path = ${config.currentSchema}`, | ||
err => { | ||
if (err) return reject(err) | ||
return resolve() | ||
} | ||
) | ||
} | ||
return resolve() | ||
@@ -281,0 +293,0 @@ }) |
{ | ||
"name": "postgrator", | ||
"version": "3.7.0", | ||
"version": "3.8.0", | ||
"author": "Rick Bergfalk <rick.bergfalk@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "A SQL migration tool for SQL people", |
@@ -34,3 +34,2 @@ const fs = require('fs') | ||
const { migrationDirectory, migrationPattern, newline } = this.config | ||
this.migrations = [] | ||
return new Promise((resolve, reject) => { | ||
@@ -50,39 +49,54 @@ const loader = (err, files) => { | ||
} | ||
}).then(migrationFiles => { | ||
migrationFiles.forEach(file => { | ||
const m = file | ||
.split('/') | ||
.pop() | ||
.split('.') | ||
const name = m.length >= 3 ? m.slice(2, m.length - 1).join('.') : file | ||
const filename = migrationPattern | ||
? file | ||
: path.join(migrationDirectory, file) | ||
if (m[m.length - 1] === 'sql') { | ||
this.migrations.push({ | ||
version: Number(m[0]), | ||
action: m[1], | ||
filename: file, | ||
name: name, | ||
md5: fileChecksum(filename, newline), | ||
getSql: () => fs.readFileSync(filename, 'utf8') | ||
}) | ||
} else if (m[m.length - 1] === 'js') { | ||
const jsModule = require(filename) | ||
const sql = jsModule.generateSql() | ||
this.migrations.push({ | ||
version: Number(m[0]), | ||
action: m[1], | ||
filename: file, | ||
name: name, | ||
md5: checksum(sql, newline), | ||
getSql: () => sql | ||
}) | ||
} | ||
}) | ||
.then(migrationFiles => { | ||
return migrationFiles.map(file => { | ||
const basename = path.basename(file) | ||
const ext = path.extname(basename) | ||
const basenameNoExt = path.basename(file, ext) | ||
let [version, action, name = ''] = basenameNoExt.split('.') | ||
version = Number(version) | ||
const filename = migrationPattern | ||
? file | ||
: path.join(migrationDirectory, file) | ||
// TODO normalize filename on returned migration object | ||
// Today it is full path if glob is used, otherwise basename with extension | ||
// This is not persisted in the database, but this field might be a part of someone's workflow | ||
// Making this change will be a breaking fix | ||
if (ext === '.sql') { | ||
return { | ||
version, | ||
action, | ||
filename: file, | ||
name, | ||
md5: fileChecksum(filename, newline), | ||
getSql: () => fs.readFileSync(filename, 'utf8') | ||
} | ||
} | ||
if (ext === '.js') { | ||
const jsModule = require(filename) | ||
const sql = jsModule.generateSql() | ||
return { | ||
version, | ||
action, | ||
filename: file, | ||
name, | ||
md5: checksum(sql, newline), | ||
getSql: () => sql | ||
} | ||
} | ||
}) | ||
}) | ||
this.migrations = this.migrations.filter( | ||
migration => !isNaN(migration.version) | ||
.then(migrations => | ||
migrations.filter(migration => !isNaN(migration.version)) | ||
) | ||
return this.migrations | ||
}) | ||
.then(migrations => { | ||
this.migrations = migrations | ||
return migrations | ||
}) | ||
} | ||
@@ -89,0 +103,0 @@ |
@@ -141,3 +141,4 @@ # Postgrator 3 | ||
connectionString: 'tcp://username:password@hosturl/databasename', | ||
ssl: true | ||
ssl: true, | ||
currentSchema: 'my-schema-name' // migrations will only run against this schema | ||
}) | ||
@@ -144,0 +145,0 @@ ``` |
44762
920
311