{"id":13517373,"url":"https://github.com/tj/node-migrate","last_synced_at":"2025-05-15T10:05:43.162Z","repository":{"id":44839914,"uuid":"1657729","full_name":"tj/node-migrate","owner":"tj","description":"Abstract migration framework for node","archived":false,"fork":false,"pushed_at":"2024-08-22T13:50:51.000Z","size":261,"stargazers_count":1555,"open_issues_count":23,"forks_count":226,"subscribers_count":17,"default_branch":"main","last_synced_at":"2025-04-30T12:43:21.127Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tj.png","metadata":{"files":{"readme":"Readme.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-04-24T21:00:22.000Z","updated_at":"2025-04-05T13:27:58.000Z","dependencies_parsed_at":"2024-01-13T19:26:49.115Z","dependency_job_id":"3f919b42-f188-4141-9436-d728d37f6019","html_url":"https://github.com/tj/node-migrate","commit_stats":{"total_commits":194,"total_committers":43,"mean_commits":4.511627906976744,"dds":0.6804123711340206,"last_synced_commit":"6a8e5994018e6a9ab7da4079dbe20fdd3de68d68"},"previous_names":["visionmedia/node-migrate"],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fnode-migrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fnode-migrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fnode-migrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fnode-migrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tj","download_url":"https://codeload.github.com/tj/node-migrate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319718,"owners_count":22051072,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T05:01:33.083Z","updated_at":"2025-05-15T10:05:38.083Z","avatar_url":"https://github.com/tj.png","language":"JavaScript","readme":"# Migrate\n\n[![NPM Version](https://img.shields.io/npm/v/migrate.svg)](https://npmjs.org/package/migrate)\n[![NPM Downloads](https://img.shields.io/npm/dm/migrate.svg)](https://npmjs.org/package/migrate)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n[![Build Status](https://travis-ci.org/tj/node-migrate.svg?branch=master)](https://travis-ci.org/tj/node-migrate)\n\nAbstract migration framework for node.\n\n## Installation\n\n    $ npm install migrate\n\n## Usage\n\n```\nUsage: migrate [options] [command]\n\nOptions:\n\n  -V, --version  output the version number\n  -h, --help     output usage information\n\nCommands:\n\n  init           Initalize the migrations tool in a project\n  list           List migrations and their status\n  create \u003cname\u003e  Create a new migration\n  up [name]      Migrate up to a given migration\n  down [name]    Migrate down to a given migration\n  help [cmd]     display help for [cmd]\n```\n\nFor help with the individual commands, see `migrate help [cmd]`.  Each command has some helpful flags\nfor customising the behavior of the tool.\n\n## Programmatic usage\n\n```javascript\nvar migrate = require('migrate')\n\nmigrate.load({\n  stateStore: '.migrate'\n}, function (err, set) {\n  if (err) {\n    throw err\n  }\n  set.up(function (err) {\n    if (err) {\n      throw err\n    }\n    console.log('migrations successfully ran')\n  })\n})\n```\n\n## Creating Migrations\n\nTo create a migration, execute `migrate create \u003ctitle\u003e` with a title. By default, a file in `./migrations/` will be created with the following content:\n\n```javascript\n'use strict'\n\nmodule.exports.up = function (next) {\n  next()\n}\n\nmodule.exports.down = function (next) {\n  next()\n}\n```\n\nAll you have to do is populate these, invoking `next()` when complete (no need to call `next()` if up/down functions are `async`), and you are ready to migrate!\n\nFor example:\n\n```\n$ migrate create add-pets\n$ migrate create add-owners\n```\n\nThe first call creates `./migrations/{timestamp in milliseconds}-add-pets.js`, which we can populate:\n\n```javascript\n// db is just an object shared between the migrations\nvar db = require('./db');\n\nexports.up = function (next) {\n  db.pets = [];\n  db.pets.push('tobi')\n  db.pets.push('loki')\n  db.pets.push('jane')\n  next()\n}\n\nexports.down = function (next) {\n  db.pets.pop('pets')\n  db.pets.pop('pets')\n  db.pets.pop('pets')\n  delete db.pets\n  next()\n}\n```\n\nThe second creates `./migrations/{timestamp in milliseconds}-add-owners.js`, which we can populate:\n\n```javascript\nvar db = require('./db');\n\nexports.up = function (next) {\n  db.owners = [];\n  db.owners.push('taylor')\n  db.owners.push('tj', next)\n}\n\nexports.down = function (next) {\n  db.owners.pop()\n  db.owners.pop()\n  delete db.owners\n  next()\n}\n```\n\n### Advanced migration creation\n\nWhen creating migrations you have a bunch of other options to help you control how the migrations\nare created.  You can fully configure the way the migration is made with a `generator`, which is just a \nfunction exported as a node module.  A good example of a generator is the  default one [shipped with\nthis package](https://github.com/tj/node-migrate/blob/b282cacbb4c0e73631d651394da52396131dd5de/lib/template-generator.js).\n\nThe `create` command accepts a flag for pointing the tool at a generator, for example:\n\n```\n$ migrate create --generator ./my-migrate-generator.js\n```\n\nA more simple and common thing you might want is to just change the default template file which is created.  To do this, you\ncan simply pass the `template-file` flag:\n\n```\n$ migrate create --template-file ./my-migration-template.js\n```\n\nLastly, if you want to use newer ECMAscript features, or language addons like TypeScript, for your migrations, you can\nuse the `compiler` flag.  For example, to use babel with your migrations, you can do the following:\n\n```\n$ npm install --save babel-register\n$ migrate create --compiler=\"js:babel-register\" foo\n$ migrate up --compiler=\"js:babel-register\"\n```\n\n## Running Migrations\n\nWhen first running the migrations, all will be executed in sequence.\n\n```\n$ migrate\n  up : migrations/1316027432511-add-pets.js\n  up : migrations/1316027432512-add-jane.js\n  up : migrations/1316027432575-add-owners.js\n  up : migrations/1316027433425-coolest-pet.js\n  migration : complete\n```\n\nSubsequent attempts will simply output \"complete\", as they have already been executed. `migrate` knows this because it stores the current state in \n`./.migrate` which is typically a file that SCMs like GIT should ignore.\n\n```\n$ migrate\n  migration : complete\n```\n\nIf we were to create another migration using `migrate create`, and then execute migrations again, we would execute only those not previously executed:\n\n```\n$ migrate\n  up : migrates/1316027433455-coolest-owner.js\n```\n\nYou can also run migrations incrementally by specifying a migration.\n\n```\n$ migrate up 1316027433425-coolest-pet.js\n  up : migrations/1316027432511-add-pets.js\n  up : migrations/1316027432512-add-jane.js\n  up : migrations/1316027432575-add-owners.js\n  up : migrations/1316027433425-coolest-pet.js\n  migration : complete\n```\n\nThis will run up-migrations up to (and including) `1316027433425-coolest-pet.js`. Similarly you can run down-migrations up to (and including) a\nspecific migration, instead of migrating all the way down.\n\n```\n$ migrate down 1316027432512-add-jane.js\n  down : migrations/1316027432575-add-owners.js\n  down : migrations/1316027432512-add-jane.js\n  migration : complete\n```\n\nAny time you want to see the current state of the migrations, you can run `migrate list` to see an output like:\n\n```\n$ migrate list\n  1316027432511-add-pets.js [2017-09-23] : \u003cNo Description\u003e\n  1316027432512-add-jane.js [2017-09-23] : \u003cNo Description\u003e\n```\n\nThe description can be added by exporting a `description` field from the migration file.\n\n## Custom State Storage\n\nBy default, `migrate` stores the state of the migrations which have been run in a file (`.migrate`).  But you\ncan provide a custom storage engine if you would like to do something different, like storing them in your database of choice.\nA storage engine has a simple interface of `load(fn)` and `save(set, fn)`.  As long as what goes in as `set` comes out\nthe same on `load`, then you are good to go!\n\nIf you are using the provided cli, you can specify the store implementation with the `--store` flag, which should be a `require`-able node module.  For example:\n\n```\n$ migrate up --store=\"my-migration-store\"\n```\n\n## API\n\n### `migrate.load(opts, cb)`\n\nCalls the callback with a `Set` based on the options passed.  Options:\n\n- `set`: A set instance if you created your own\n- `stateStore`: A store instance to load and store migration state, or a string which is a path to the migration state file\n- `migrations`: An object where keys are migration names and values are migration objects\n- `migrationsDirectory`: The path to the migrations directory\n- `filterFunction`: A filter function which will be called for each file found in the migrations directory\n- `sortFunction`: A sort function to ensure migration order\n\n### `Set.up([migration, ]cb)`\n\nMigrates up to the specified `migration` or, if none is specified, to the latest\nmigration. Calls the callback `cb`, possibly with an error `err`, when done.\n\n### `Set.down([migration, ]cb)`\n\nMigrates down to the specified `migration` or, if none is specified, to the\nfirst migration. Calls the callback `cb`, possibly with an error `err`, when\ndone.\n","funding_links":[],"categories":["JavaScript","Web 后端","Migration"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftj%2Fnode-migrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftj%2Fnode-migrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftj%2Fnode-migrate/lists"}