db-migrate
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -56,1 +56,6 @@ ## 0.1.0 | ||
- Added compatibility for mysql-2.0.0-alpha driver #25 | ||
## 0.2.2 | ||
Fix: | ||
- Fix default value error when using Postgres #43 |
@@ -134,3 +134,3 @@ var util = require('util'); | ||
} else { | ||
defaultValue = spec.defaultValue; | ||
defaultValue = columnSpec.defaultValue; | ||
} | ||
@@ -137,0 +137,0 @@ sql = util.format("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s", tableName, columnName, defaultValue); |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"engines": { | ||
@@ -17,0 +17,0 @@ "node": ">=0.6.0" |
220
README.md
@@ -7,3 +7,3 @@ # db-migrate | ||
$ npm install -g db-migrate | ||
$ npm install db-migrate | ||
@@ -33,9 +33,11 @@ ## Supported Databases | ||
exports.up = function(db, callback){ | ||
callback(); | ||
}; | ||
```javascript | ||
exports.up = function (db, callback) { | ||
callback(); | ||
}; | ||
exports.down = function(callback){ | ||
callback(); | ||
}; | ||
exports.down = function (callback) { | ||
callback(); | ||
}; | ||
``` | ||
@@ -51,73 +53,81 @@ All you have to do is populate these, invoking `callback()` when complete, and you are ready to migrate! | ||
exports.up = function(db, callback){ | ||
db.createTable('pets', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, callback); | ||
}; | ||
```javascript | ||
exports.up = function (db, callback) { | ||
db.createTable('pets', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, callback); | ||
}; | ||
exports.down = function(db, callback){ | ||
db.dropTable('pets', callback); | ||
}; | ||
exports.down = function (db, callback) { | ||
db.dropTable('pets', callback); | ||
}; | ||
``` | ||
The second creates `./migrations/20111219120005-add-owners.js`, which we can populate: | ||
exports.up = function(db, callback){ | ||
db.createTable('owners', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, callback); | ||
}; | ||
```javascript | ||
exports.up = function (db, callback) { | ||
db.createTable('owners', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, callback); | ||
}; | ||
exports.down = function(db, callback){ | ||
db.dropTable('owners', callback); | ||
}; | ||
exports.down = function (db, callback) { | ||
db.dropTable('owners', callback); | ||
}; | ||
``` | ||
Executing multiple statements against the database within a single migration requires a bit more care. You can either nest the migrations like: | ||
exports.up = function(db, callback){ | ||
db.createTable('pets', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, createOwners); | ||
```javascript | ||
exports.up = function (db, callback) { | ||
db.createTable('pets', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, createOwners); | ||
function createOwners(err) { | ||
if (err) { callback(err); return; } | ||
db.createTable('owners', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, callback); | ||
} | ||
}; | ||
function createOwners(err) { | ||
if (err) { callback(err); return; } | ||
db.createTable('owners', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}, callback); | ||
} | ||
}; | ||
exports.down = function(db, callback){ | ||
db.dropTable('pets', function(err) { | ||
if (err) { callback(err); return; } | ||
db.dropTable('owners', callback); | ||
}) | ||
}; | ||
exports.down = function (db, callback) { | ||
db.dropTable('pets', function(err) { | ||
if (err) { callback(err); return; } | ||
db.dropTable('owners', callback); | ||
}); | ||
}; | ||
``` | ||
or use the async library to simplify things a bit, such as: | ||
var async = require('async'); | ||
```javascript | ||
var async = require('async'); | ||
exports.up = function(db, callback){ | ||
async.series([ | ||
db.createTable.bind(db, 'pets', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}), | ||
db.createTable.bind(db, 'owners', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}) | ||
], callback); | ||
}; | ||
exports.up = function (db, callback) { | ||
async.series([ | ||
db.createTable.bind(db, 'pets', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}), | ||
db.createTable.bind(db, 'owners', { | ||
id: { type: 'int', primaryKey: true }, | ||
name: 'string' | ||
}); | ||
], callback); | ||
}; | ||
exports.down = function(db, callback){ | ||
async.series([ | ||
db.dropTable.bind(db, 'pets'), | ||
db.dropTable.bind(db, 'owners') | ||
], callback); | ||
}; | ||
exports.down = function (db, callback) { | ||
async.series([ | ||
db.dropTable.bind(db, 'pets'), | ||
db.dropTable.bind(db, 'owners') | ||
], callback); | ||
}; | ||
``` | ||
@@ -165,27 +175,29 @@ | ||
{ | ||
"dev": { | ||
"driver": "sqlite3", | ||
"filename": "~/dev.db" | ||
}, | ||
```javascript | ||
{ | ||
"dev": { | ||
"driver": "sqlite3", | ||
"filename": "~/dev.db" | ||
}, | ||
"test": { | ||
"driver": "sqlite3", | ||
"filename": ":memory:" | ||
}, | ||
"test": { | ||
"driver": "sqlite3", | ||
"filename": ":memory:" | ||
}, | ||
"prod": { | ||
"driver": "mysql", | ||
"user": "root", | ||
"password": "root" | ||
}, | ||
"pg": { | ||
"driver": "pg", | ||
"user": "test", | ||
"password": "test", | ||
"host": "localhost", | ||
"database": "mydb" | ||
} | ||
} | ||
"prod": { | ||
"driver": "mysql", | ||
"user": "root", | ||
"password": "root" | ||
}, | ||
"pg": { | ||
"driver": "pg", | ||
"user": "test", | ||
"password": "test", | ||
"host": "localhost", | ||
"database": "mydb" | ||
} | ||
} | ||
``` | ||
@@ -216,20 +228,22 @@ You can pass the -e or --env option to db-migrate to select the environment you want to run migrations against. The --config option can be used to specify the path to your database.json file if it's not in the current working directory. | ||
// with no table options | ||
exports.up = function(db, callback) { | ||
db.createTable('pets', { | ||
id: { type: 'int', primaryKey: true, autoIncrement: true }, | ||
name: 'string' // shorthand notation | ||
}, callback); | ||
} | ||
```javascript | ||
// with no table options | ||
exports.up = function (db, callback) { | ||
db.createTable('pets', { | ||
id: { type: 'int', primaryKey: true, autoIncrement: true }, | ||
name: 'string' // shorthand notation | ||
}, callback); | ||
} | ||
// with table options | ||
exports.up = function(db, callback) { | ||
db.createTable('pets', { | ||
columns: { | ||
id: { type: 'int', primaryKey: true, autoIncrement: true }, | ||
name: 'string' // shorthand notation | ||
}, | ||
ifNotExists: true | ||
}, callback); | ||
} | ||
// with table options | ||
exports.up = function (db, callback) { | ||
db.createTable('pets', { | ||
columns: { | ||
id: { type: 'int', primaryKey: true, autoIncrement: true }, | ||
name: 'string' // shorthand notation | ||
}, | ||
ifNotExists: true | ||
}, callback); | ||
} | ||
``` | ||
@@ -236,0 +250,0 @@ __Column Specs__ |
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
399
0
93433