@2012mjm/sails-db-migrate 中文文档教程
sails-db-migrate
db-migrate 集成 < href="http://sailsjs.org/">Sails.js。 这是一个相当简单的包装器, 它提供用于运行和创建迁移的 grunt 任务。 它也是 从 Sails 配置中提取数据库配置,因此您不必 在 database.json
文件中复制您的配置。
支持 Sails 0.10.x。
Setup
安装非常典型。
$ npm install --save sails-db-migrate
您可能还必须显式安装数据库驱动程序。 通常是 安装在 sails-{postgresql,mysql}
下,但在外部找不到 那个包裹。
$ npm install --save pg # or mysql
您需要设置 config/migrations.js
来命名您要连接的连接 用于运行迁移。
// config/migrations.js
module.exports.migrations = {
// connection name matches a field from config/connections.js
connection: 'somePostgresqlServer' // or MySQL
};
或者,您可以在配置文件中指定数据库表的名称 用于跟踪迁移(默认为 migrations
),用于跟踪的目录 迁移(默认为 migrations
),以及是否创建 coffeescript 用于迁移的文件而不是 javascript 文件(默认为 false
)。
// config/migrations.js
module.exports.migrations = {
// connection name matches a field from config/connections.js
connection: 'somePostgresqlServer', // or MySQL
table: 'sails_migrations',
migrationsDir: 'sails_migrations',
coffeeFile: true
};
您还需要设置 tasks/register/dbMigrate.js
以添加 db:migrate
咕噜咕噜的任务。
// tasks/register/dbMigrate.js
module.exports = require('sails-db-migrate').gruntTasks;
Usage
Help
可以通过运行 grunt db:migrate
找到帮助。
$ grunt db:migrate
Creating migrations
您使用 grunt db:migrate:create
创建迁移
$ grunt db:migrate:create --name add-some-fooz
+ db-migrate create add-some-fooz
[INFO] Created migration at /Users/dlee/prj/test/migrations/20140829025723-add-some-fooz.js
Done, without errors.
您可以编辑新的迁移文件。 这在 db-migrate 文档。
Running migrations
迁移可以向上或向下运行。 要仅运行一定数量的迁移, 指定 --count
标志
$ grunt db:migrate:up
Running "db:migrate:up" (db:migrate) task
+ db-migrate up
[INFO] Processed migration 20140829025723-add-some-fooz
[INFO] Done
Done, without errors.
$ grunt db:migrate:down --count 2
Running "db:migrate:down" (db:migrate) task
+ db-migrate down --count 2
[INFO] Processed migration 20140829025723-add-some-fooz
[INFO] Processed migration 20140829025008-init
[INFO] Done
Done, without errors.
。要指定您自己的迁移目录,请使用 --migrations-dir
。
$ grunt db:migrate:up --migrations-dir=migrations-special
Debugging
通常,迁移加载日志级别为 silent
的 Sails,因为您通常 不需要它。 然而,有时 Sails 会加载失败,一些调试 输出会很好。
您可以设置 LOG_LEVEL
环境变量以打开 sails 日志记录 迁移
$ LOG_LEVEL=debug grunt db:migrate:up
Patches Welcome
如果您想为 sails-db-migrate 做出贡献,请先查看一些 问题跟踪器中的未解决问题。
sails-db-migrate
db-migrate integration for Sails.js. This is a fairly simple wrapper, which provides grunt tasks for running and creating migrations. It also extracts the database configuration from the Sails config, so you don't have to duplicate you config in a database.json
file.
Supports Sails 0.10.x.
Setup
Installation is very typical.
$ npm install --save sails-db-migrate
You may also have to explicitly install your database driver. Normallys it's installed under sails-{postgresql,mysql}
, but that won't be found outside of that package.
$ npm install --save pg # or mysql
You need to setup config/migrations.js
to name the connection which you will use to run migrations.
// config/migrations.js
module.exports.migrations = {
// connection name matches a field from config/connections.js
connection: 'somePostgresqlServer' // or MySQL
};
Optionally, you can specify in the config file the name of the database table to be used to track migrations (defaults to migrations
), the directory to use for migrations (defaults to migrations
), and whether to create a coffeescript file for the migrations instead of javascript file (defaults to false
).
// config/migrations.js
module.exports.migrations = {
// connection name matches a field from config/connections.js
connection: 'somePostgresqlServer', // or MySQL
table: 'sails_migrations',
migrationsDir: 'sails_migrations',
coffeeFile: true
};
You'll also need to setup tasks/register/dbMigrate.js
to add the db:migrate
tasks to grunt.
// tasks/register/dbMigrate.js
module.exports = require('sails-db-migrate').gruntTasks;
Usage
Help
Help can be found by running grunt db:migrate
.
$ grunt db:migrate
Creating migrations
You create migrations using the grunt db:migrate:create
$ grunt db:migrate:create --name add-some-fooz
+ db-migrate create add-some-fooz
[INFO] Created migration at /Users/dlee/prj/test/migrations/20140829025723-add-some-fooz.js
Done, without errors.
You can edit your new migration file. This is fully documented in the db-migrate docs.
Running migrations
Migrations can be run up or down. To run only a certain number of migrations, specify the --count
flag.`
$ grunt db:migrate:up
Running "db:migrate:up" (db:migrate) task
+ db-migrate up
[INFO] Processed migration 20140829025723-add-some-fooz
[INFO] Done
Done, without errors.
$ grunt db:migrate:down --count 2
Running "db:migrate:down" (db:migrate) task
+ db-migrate down --count 2
[INFO] Processed migration 20140829025723-add-some-fooz
[INFO] Processed migration 20140829025008-init
[INFO] Done
Done, without errors.
To specify your own migrations directory, use --migrations-dir
.
$ grunt db:migrate:up --migrations-dir=migrations-special
Debugging
Normally, migrations load Sails with a log level of silent
, since you usually don't need it. Sometimes, however, Sails will fail to load, and some debug output would be nice.
You can set the LOG_LEVEL
environment variable to turn up sails logging for the migrations
$ LOG_LEVEL=debug grunt db:migrate:up
Patches Welcome
If you'd like to contribute to sails-db-migrate, start by looking at some of the open issues in the issue tracker.