node-pg-migrate
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -47,2 +47,3 @@ /* | ||
this.getSqlSteps = function(){ | ||
// in reverse mode, we flip the order of the statements | ||
return REVERSE_MODE ? steps.reverse() : steps; | ||
@@ -49,0 +50,0 @@ } |
@@ -1,9 +0,7 @@ | ||
exports.up = function(pgm, run) { | ||
exports.up = function(pgm) { | ||
run(); | ||
}; | ||
exports.down = function(pgm, run) { | ||
exports.down = function(pgm) { | ||
run(); | ||
}; |
@@ -21,3 +21,3 @@ /* | ||
var Migration = function(path, actions){ | ||
var Migration = function(path, actions, options){ | ||
var self = this; | ||
@@ -32,7 +32,8 @@ this.path = path; | ||
var pgm = new MigrationBuilder(); | ||
self.up(pgm, function(){ | ||
function upComplete(){ | ||
console.log('### MIGRATION '+self.name+' (up) ###') | ||
var sql_steps = pgm.getSqlSteps(); | ||
sql_steps.push( utils.t("INSERT INTO pgmigrations (name, run_on) VALUES ('{name}', NOW());\n", { name: self.name }) ); | ||
sql_steps.push( utils.t("INSERT INTO "+options.migrations_table+" (name, run_on) VALUES ('{name}', NOW());", { name: self.name }) ); | ||
@@ -43,8 +44,7 @@ if (pgm.use_transaction){ | ||
sql_steps.push('COMMIT;'); | ||
var sql_steps = [sql_steps.join("\n")]; | ||
} else { | ||
console.log('> WARNING: This migration is not wrapped in a transaction! <') | ||
console.log('#> WARNING: This migration is not wrapped in a transaction! <') | ||
} | ||
console.log( sql_steps.join("\n") ); | ||
console.log( sql_steps.join("\n")+"\n\n" ); | ||
@@ -58,3 +58,10 @@ if (!global.dryRun) { | ||
} | ||
}) | ||
} | ||
if (self.up.length == 2) { | ||
self.up(pgm, upComplete); | ||
} else { | ||
self.up(pgm); | ||
upComplete() | ||
} | ||
} | ||
@@ -72,20 +79,34 @@ self.applyDown = function(done){ | ||
self.down(pgm, function(){ | ||
var sql = 'BEGIN; '+"\n"; | ||
sql += pgm.getSql(); | ||
sql += utils.t("DELETE FROM pgmigrations WHERE name='{name}';\n", { name: self.name }); | ||
sql += 'COMMIT;'; | ||
function downComplete(){ | ||
console.log('### MIGRATION '+self.name+' (down) ###') | ||
console.log( sql ); | ||
var sql_steps = pgm.getSqlSteps(); | ||
sql_steps.push( utils.t("DELETE FROM "+options.migrations_table+" WHERE name='{name}';", { name: self.name }) ); | ||
if (pgm.use_transaction){ | ||
// wrap in a transaction, combine into one sql statement | ||
sql_steps.unshift('BEGIN;'); | ||
sql_steps.push('COMMIT;'); | ||
} else { | ||
console.log('#> WARNING: This migration is not wrapped in a transaction! <') | ||
} | ||
console.log( sql_steps.join("\n")+"\n\n" ); | ||
if (!global.dryRun) { | ||
db.query(sql, function(err, result){ | ||
if (err) return done(err); | ||
done(); | ||
}); | ||
async.eachSeries(sql_steps, function(sql, next_step){ | ||
db.query(sql, next_step); | ||
}, done); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
} | ||
if (self.down.length == 2) { | ||
self.down(pgm, downComplete); | ||
} else { | ||
self.down(pgm); | ||
downComplete() | ||
} | ||
} | ||
@@ -92,0 +113,0 @@ } |
@@ -108,3 +108,3 @@ var utils = require('../utils'); | ||
type_name: type_name, | ||
opts: options.join("', ") | ||
opts: options.join("', '") | ||
}); | ||
@@ -111,0 +111,0 @@ }, |
@@ -16,3 +16,3 @@ var async = require('async'); | ||
db.init(options.database_url); | ||
self.run = function(callback){ | ||
@@ -25,7 +25,7 @@ var current_index, migrate_to_index; | ||
function fetchCompletedMigrations(err){ | ||
db.query("SELECT * FROM pgmigrations ORDER BY run_on", this) | ||
db.query("SELECT * FROM "+options.migrations_table+" ORDER BY run_on", this) | ||
}, | ||
function determineMigrationsToRun(err, result){ | ||
if (err) return callback(err); | ||
if (!result || !result.rows) return callback(new Error('Unable to fetch migrations from pgmigrations table')); | ||
if (!result || !result.rows) return callback(new Error('Unable to fetch migrations from '+options.migrations_table+' table')); | ||
@@ -54,3 +54,3 @@ // TODO: handle merging of migrations that are out of order better | ||
// users can also specify the number of migrations to move up and down | ||
migrate_to_index = current_index + options.count * (options.direction=='up' ?1:-1); | ||
migrate_to_index = current_index + options.count * (options.direction=='up' ?1:-1); | ||
} | ||
@@ -106,3 +106,3 @@ } | ||
var actions = require( path.relative(__dirname, file_path) ); | ||
var migration = new Migration( file_path, actions ); | ||
var migration = new Migration( file_path, actions, options ); | ||
migrations.push(migration); | ||
@@ -114,21 +114,23 @@ } | ||
} | ||
function ensureMigrationTableExists(callback){ | ||
Step( | ||
function checkIfTableExists(){ | ||
db.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '"+options.migrations_table+"'", this); | ||
}, | ||
function createTableIfNecessary(err, result){ | ||
if (err) return callback(err); | ||
if (result && result.rows && result.rows.length == 1) return callback(); | ||
db.query('CREATE TABLE '+options.migrations_table+' ( id SERIAL, name varchar(255) NOT NULL, run_on timestamp NOT NULL)', this); | ||
}, | ||
function finish(err){ | ||
if (err) return callback(err); | ||
return callback(); | ||
} | ||
) | ||
} | ||
function ensureMigrationTableExists(callback){ | ||
Step( | ||
function checkIfTableExists(){ | ||
db.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'pgmigrations'", this); | ||
}, | ||
function createTableIfNecessary(err, result){ | ||
if (err) return callback(err); | ||
if (result && result.rows && result.rows.length == 1) return callback(); | ||
db.query('CREATE TABLE pgmigrations ( id SERIAL, name varchar(255) NOT NULL, run_on timestamp NOT NULL)', this); | ||
}, | ||
function finish(err){ | ||
if (err) return callback(err); | ||
return callback(); | ||
} | ||
) | ||
} | ||
@@ -18,3 +18,3 @@ { | ||
], | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"engines": { | ||
@@ -37,2 +37,3 @@ "node": ">=0.6.0" | ||
"optimist": "~0.3.7", | ||
"pg": "^3.4.1", | ||
"pkginfo": "~0.3.0", | ||
@@ -43,3 +44,2 @@ "step": "0.0.5" | ||
"mocha": "^2.2.1", | ||
"pg": "^3.4.1", | ||
"rewire": "^2.1.2", | ||
@@ -46,0 +46,0 @@ "sinon": "^1.11.1" |
@@ -31,7 +31,7 @@ # pg-migrate | ||
```javascript | ||
exports.up = function(pgm, run){ | ||
run(); | ||
exports.up = function(pgm){ | ||
} | ||
exports.down = function(pgm, run){ | ||
run(); | ||
exports.down = function(pgm){ | ||
} | ||
@@ -43,3 +43,3 @@ ``` | ||
**IMPORTANT** | ||
Generation of the up and down block is asynchronous, but each individal operation is not. Calling the migration functions on `pgm` doesn't actually migrate your database. These functions just add sql commands to a stack that is run after you call the callback. | ||
Calling the migration functions on `pgm` doesn't actually migrate your database. These functions just add sql commands to a stack that is is run. | ||
@@ -50,4 +50,16 @@ #### Automatic Down Migrations | ||
#### Async Migrations | ||
In some cases, you may want to perform some async operation during a migration, for example fetching some information from an external server, or inserting some data into the database. To make a migration block operate in async mode, just add another callback argument to the function signature. However, be aware that NONE of the pgm operations will be executed until `run()` is called. Here's an example: | ||
```javascript | ||
exports.up = function(pgm, run){ | ||
doSomethingAsync(function(){ | ||
run(); | ||
}); | ||
} | ||
``` | ||
## Migration methods | ||
@@ -54,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40416
3
16
600
309
8
+ Addedpg@^3.4.1
+ Addedbindings@1.2.1(transitive)
+ Addedbuffer-writer@1.0.0(transitive)
+ Addedgeneric-pool@2.1.1(transitive)
+ Addedjs-string-escape@1.0.1(transitive)
+ Addednan@1.3.0(transitive)
+ Addedpacket-reader@0.2.0(transitive)
+ Addedpg@3.6.4(transitive)
+ Addedpg-connection-string@0.1.3(transitive)
+ Addedpg-types@1.6.0(transitive)
+ Addedpgpass@0.0.3(transitive)
+ Addedsplit@0.3.3(transitive)
+ Addedthrough@2.3.8(transitive)