node-pg-migrate
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -15,2 +15,3 @@ /* | ||
var _ = require('lodash'); | ||
var utils = require('./utils'); | ||
@@ -29,5 +30,11 @@ var ops = { | ||
// by default, all migrations are wrapped in a transaction | ||
this.use_transaction = true; | ||
this.enableReverseMode = function(){ | ||
REVERSE_MODE = true; | ||
} | ||
this.noTransaction = function(){ | ||
self.use_transaction = false; | ||
} | ||
@@ -41,2 +48,5 @@ this.getSql = function(){ | ||
} | ||
this.getSqlSteps = function(){ | ||
return REVERSE_MODE ? steps.reverse() : steps; | ||
} | ||
@@ -91,4 +101,10 @@ // this function wraps each operation within a function that either | ||
this.sql = wrap( ops.other.sql ); | ||
// Other utilities which may be useful | ||
// .func creates a string which will not be escaped | ||
// common uses are for PG functions, ex: { ... default: pgm.func('NOW()') } | ||
this.func = utils.PgLiteral.create; | ||
} | ||
module.exports = MigrationBuilder; |
@@ -1,9 +0,9 @@ | ||
exports.up = function(pgm, run){ | ||
exports.up = function(pgm, run) { | ||
run(); | ||
} | ||
}; | ||
exports.down = function(pgm, run){ | ||
exports.down = function(pgm, run) { | ||
run(); | ||
} | ||
}; |
@@ -14,4 +14,6 @@ /* | ||
var util = require('util'); | ||
var utils = require('./utils'); | ||
var async = require('async'); | ||
var _ = require('lodash'); | ||
var utils = require('./utils'); | ||
var MigrationBuilder = require('./migration-builder'); | ||
@@ -31,16 +33,21 @@ var db = require('./db'); | ||
self.up(pgm, function(){ | ||
var sql = 'BEGIN; '+"\n"; | ||
sql += pgm.getSql(); | ||
sql += utils.t("INSERT INTO pgmigrations (name, run_on) VALUES ('{name}', NOW());\n", { name: self.name }); | ||
sql += 'COMMIT;'; | ||
console.log('### MIGRATION '+self.name+' (up) ###') | ||
console.log( sql ); | ||
db.query(sql, function(err, result){ | ||
if (err) return done(err); | ||
var sql_steps = pgm.getSqlSteps(); | ||
sql_steps.push( utils.t("INSERT INTO pgmigrations (name, run_on) VALUES ('{name}', NOW());\n", { name: self.name }) ); | ||
done(); | ||
}); | ||
if (pgm.use_transaction){ | ||
// wrap in a transaction, combine into one sql statement | ||
sql_steps.unshift('BEGIN;'); | ||
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( sql_steps.join("\n") ); | ||
async.eachSeries(sql_steps, function(sql, next_step){ | ||
db.query(sql, next_step); | ||
}, done); | ||
}) | ||
@@ -47,0 +54,0 @@ } |
@@ -35,2 +35,3 @@ var utils = require('../utils'); | ||
options = options || {}; | ||
if (_.isArray(columns)) columns = columns.join(', '); | ||
var index_name = generateIndexName( table_name, columns, options ); | ||
@@ -37,0 +38,0 @@ return utils.t('DROP INDEX {index};', { index: index_name } ); |
@@ -24,7 +24,7 @@ var utils = require('../utils'); | ||
drop: function ( table_name ) { | ||
return utils.t('DROP TABLE {table_name};', { table_name: table_name } ); | ||
return utils.t('DROP TABLE \"{table_name}\";', { table_name: table_name } ); | ||
}, | ||
addColumns: function ( table_name, columns ) { | ||
return utils.t("ALTER TABLE {table_name} \n{actions};", { | ||
return utils.t("ALTER TABLE \"{table_name}\" \n{actions};", { | ||
table_name: table_name, | ||
@@ -39,3 +39,3 @@ actions: parseColumns( columns ).replace(/^/gm, ' ADD '), | ||
} | ||
return utils.t("ALTER TABLE {table_name} \n{actions};", { | ||
return utils.t("ALTER TABLE \"{table_name}\" \n{actions};", { | ||
table_name: table_name, | ||
@@ -51,3 +51,3 @@ actions: columns.join(",\n").replace(/^/gm, ' DROP '), | ||
} else if ( options.default !== undefined ){ | ||
actions.push('SET DEFAULT '+escapeValue(options.default)); | ||
actions.push('SET DEFAULT '+utils.escapeValue(options.default)); | ||
} | ||
@@ -63,3 +63,3 @@ if ( options.type ){ | ||
return utils.t("ALTER TABLE {table_name} \n{actions};", { | ||
return utils.t("ALTER TABLE \"{table_name}\" \n{actions};", { | ||
table_name: table_name, | ||
@@ -73,3 +73,3 @@ actions: actions.join(",\n").replace(/^/gm, ' ALTER '+column_name+' '), | ||
renameTable: function( table_name, new_name ){ | ||
return utils.t("ALTER TABLE {table_name} RENAME TO {new_name};", { | ||
return utils.t("ALTER TABLE \"{table_name}\" RENAME TO {new_name};", { | ||
table_name: table_name, | ||
@@ -83,3 +83,3 @@ new_name: new_name | ||
renameColumn: function( table_name, column_name, new_name ){ | ||
return utils.t("ALTER TABLE {table_name} RENAME {column} TO {new_name};", { | ||
return utils.t("ALTER TABLE \"{table_name}\" RENAME {column} TO {new_name};", { | ||
table_name: table_name, | ||
@@ -96,3 +96,3 @@ column: column_name, | ||
addConstraint: function( table_name, constraint_name, expression ){ | ||
return utils.t("ALTER TABLE {table_name} ADD{constraint_name} {constraint};", { | ||
return utils.t("ALTER TABLE \"{table_name}\" ADD{constraint_name} {constraint};", { | ||
table_name: table_name, | ||
@@ -104,3 +104,3 @@ constraint_name: constraint_name ? ' CONSTRAINT '+constraint_name : '', | ||
dropConstraint: function( table_name, constraint_name, expression ){ | ||
return utils.t("ALTER TABLE {table_name} DROP CONSTRAINT {constraint_name};", { | ||
return utils.t("ALTER TABLE \"{table_name}\" DROP CONSTRAINT {constraint_name};", { | ||
table_name: table_name, | ||
@@ -185,3 +185,3 @@ constraint_name: constraint_name, | ||
type: type, | ||
default: options.default !== undefined ? ' DEFAULT '+escapeValue(options.default) : '', | ||
default: options.default !== undefined ? ' DEFAULT '+utils.escapeValue(options.default) : '', | ||
constraints: constraints.length ? ' '+constraints.join(' ') : '' | ||
@@ -191,9 +191,2 @@ }); | ||
}).join(", \n"); | ||
} | ||
function escapeValue ( val ){ | ||
// TODO: figure out a solution for unescaping functions -- ex: NOW() | ||
if ( val === null ) return 'NULL'; | ||
if ( typeof val === 'string' ) return "'"+escape(val)+"'"; | ||
if ( typeof val === 'number' ) return val; | ||
} |
@@ -9,3 +9,3 @@ var async = require('async'); | ||
module.exports = function(options){ | ||
module.exports = function MigrationRunner(options){ | ||
var self = this; | ||
@@ -12,0 +12,0 @@ |
@@ -8,3 +8,23 @@ module.exports = { | ||
return s; | ||
}, | ||
escapeValue: function ( val ){ | ||
// TODO: figure out a solution for unescaping functions -- ex: NOW() | ||
if ( val === null ) return 'NULL'; | ||
if ( typeof val === 'boolean' ) return val.toString(); | ||
if ( typeof val === 'string' ) return "'"+escape(val)+"'"; | ||
if ( typeof val === 'number' ) return val; | ||
if ( val instanceof PgLiteral ) return val.toString(); | ||
}, | ||
PgLiteral: PgLiteral | ||
} | ||
// This is used to create unescaped strings | ||
// exposed in the migrations via pgm.func | ||
function PgLiteral(str){ | ||
this.toString = function(){ | ||
return str; | ||
} | ||
} | ||
PgLiteral.create = function(str){ | ||
return new PgLiteral(str); | ||
} |
@@ -16,3 +16,3 @@ { | ||
], | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"engines": { | ||
@@ -41,4 +41,4 @@ "node": ">=0.6.0" | ||
"scripts": { | ||
} | ||
} |
@@ -19,3 +19,3 @@ # pg-migrate | ||
- `pg-migrate create {migration-name}` - creates a new migration file with the name you give it. Spaces and underscores will be replaced by dashes and a timestamp is prepended to your file name. | ||
- `pg-migrate create {migration-name}` - creates a new migration file with the name you give it. Spaces and underscores will be replaced by dashes and a timestamp is prepended to your file name. | ||
- `pg-migrate up` - run all up migrations from the current state | ||
@@ -42,3 +42,3 @@ - `pg-migrate up {N}` - run N up migrations from the current position | ||
**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. | ||
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. | ||
@@ -55,2 +55,5 @@ #### Automatic Down Migrations | ||
By default, each migration will be run in a transaction. To disable transactions for a specific migration, call `pgm.noTransaction()` | ||
This is required for some SQL operations that cannot be run within a transaction. It should be used carefully. | ||
### Creating & Altering Tables / Columns | ||
@@ -67,3 +70,3 @@ | ||
- `inherits` _[string]_ - table to inherit from | ||
**Reverse Operation:** `dropTable` | ||
@@ -102,3 +105,3 @@ | ||
**Aliases:** `addColumn` | ||
**Aliases:** `addColumn` | ||
**Reverse Operation:** `dropColumns` | ||
@@ -154,5 +157,5 @@ | ||
- `constraint_name` _[string]_ - name for the constraint | ||
- `expression` _[string]_ - constraint expression (raw sql) | ||
- `expression` _[string]_ - constraint expression (raw sql) | ||
**Aliases:** `createConstraint` | ||
**Aliases:** `createConstraint` | ||
**Reverse Operation:** `dropConstraint` | ||
@@ -186,3 +189,3 @@ | ||
**Aliases:** `addIndex` | ||
**Aliases:** `addIndex` | ||
**Reverse Operation:** `dropIndex` | ||
@@ -214,3 +217,3 @@ | ||
**Aliases:** `addExtension` | ||
**Aliases:** `addExtension` | ||
**Reverse Operation:** `dropExtension` | ||
@@ -227,3 +230,3 @@ | ||
**Aliases:** `addExtension` | ||
**Aliases:** `addExtension` | ||
**Reverse Operation:** `dropExtension` | ||
@@ -250,3 +253,3 @@ | ||
**Aliases:** `addType` | ||
**Aliases:** `addType` | ||
**Reverse Operation:** `dropType` | ||
@@ -267,3 +270,3 @@ | ||
The `createTable` and `addColumns` methods both take a `columns` argument that specifies column names and options. It is a object (key/value) where each key is the name of the column, and the value is another object that defines the options for the column. | ||
The `createTable` and `addColumns` methods both take a `columns` argument that specifies column names and options. It is a object (key/value) where each key is the name of the column, and the value is another object that defines the options for the column. | ||
@@ -280,13 +283,13 @@ - `type` _[string]_ - data type (use normal postgres types) | ||
**There are some aliases on types to make things more foolproof:** | ||
**There are some aliases on types to make things more foolproof:** | ||
_(int, string, float, double, datetime, bool)_ | ||
**There is a shorthand to pass only the type instead of an options object:** | ||
`pgm.addColumns('myTable', { age: 'integer' });` | ||
is equivalent to | ||
**There is a shorthand to pass only the type instead of an options object:** | ||
`pgm.addColumns('myTable', { age: 'integer' });` | ||
is equivalent to | ||
`pgm.addColumns('myTable', { age: { type: 'integer' } });` | ||
**There is a shorthand for normal auto-increment IDs:** | ||
`pgm.addColumns('myTable', { id: 'id' });` | ||
is equivalent to | ||
**There is a shorthand for normal auto-increment IDs:** | ||
`pgm.addColumns('myTable', { id: 'id' });` | ||
is equivalent to | ||
`pgm.addColumns('myTable', { id: { type: 'serial', primaryKey: true } });` | ||
@@ -293,0 +296,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
39021
577
296