{"id":25566522,"url":"https://github.com/pdmlab/migrate-semver","last_synced_at":"2025-07-04T17:09:20.437Z","repository":{"id":57296974,"uuid":"66307368","full_name":"PDMLab/migrate-semver","owner":"PDMLab","description":"Database agnostic migrations based on SemVer using Node.js","archived":false,"fork":false,"pushed_at":"2018-03-17T17:04:23.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-02T02:04:59.712Z","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/PDMLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-22T20:54:29.000Z","updated_at":"2018-03-17T17:04:24.000Z","dependencies_parsed_at":"2022-09-01T13:01:56.183Z","dependency_job_id":null,"html_url":"https://github.com/PDMLab/migrate-semver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmigrate-semver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmigrate-semver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmigrate-semver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmigrate-semver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PDMLab","download_url":"https://codeload.github.com/PDMLab/migrate-semver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239927244,"owners_count":19719827,"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":"2025-02-20T22:33:01.079Z","updated_at":"2025-02-20T22:33:01.543Z","avatar_url":"https://github.com/PDMLab.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Database agnostic migrations based on SemVer using Node.js\n\n`migrate-semver` is a small library to allow you do to migrations based on [SemVer](http://semver.org/) in a database agnostic way using Node.js.\n\n## Installation\n\n```\nnpm install --save migrate-semver\n```\n\n## Usage\n\n`migrate-semver` just handles the heavy lifting of finding the viable migrations to run based on a SemVer compliant version string passed into it.\n\nThe following example runs a migration for version `0.3.0`.\n\n```js\nconst version = '0.3.0';\nconst migrationsDirectory = path.join(__dirname, 'migrations');\nconst migrateSemVer = new SemVerMigration({ migrationsDirectory }, plugin());\n\nmigrateSemVer.connect({}, err =\u003e {\n  migrateSemVer.up({ version }, err =\u003e {\n    assert.equal(migrations[2].version, version);\n    done();\n  });\n});\n```\n\n`migrate-semver` handles several scenarios for you based on the example above: \n\n* If your current database has no migrations at all (so it might be empty at all), `migrate-semver` runs all available migrations until `0.3.0` (including `0.3.0`).\n* If your current database has version `0.2.0` applied and `0.3.0` is the next available migration, `0.3.0` is just applied.\n* If your current database has version `0.1.0` applied and `0.2.0` is also available, both `0.2.0` and `0.3.0` wil be applied.\n\n`migrate-semver` allows you to specify the base directory which contains all your migration folders and files.\nJust pass the `migrationsDirectory` option to the `SemVerMigration` ctor function as shown above.\n\nThe file and folder structure has to follow this convention (the `index-up.js` can be renamed if you implement your own plugin):\n\n\u003cpre\u003e\n- \u0026lt;migrationsDirectory\u003e    \n  - 0.1.0\n    - index-up.js\n  - 0.2.0\n    - index-up.js\n  - 0.2.1\n    - index-up.js\n  - 0.3.0 \n    - index-up.js\n\u003c/pre\u003e\n\n### canMigrate\n\nIf you want to automate your migrations, your code triggering the migrations can check whether an migration for a particular version can be done using the `migrateSemVer.canMigrate({ version })` function:\n\n```js\nconst version = '0.3.0';\n\nmigrateSemVer.connect({}, err =\u003e {\n  migrateSemVer.canMigrate({ version }, (err, canMigrate) =\u003e {\n    if (canMigrate) {\n      migrateSemVer.up({ version }, err =\u003e {\n        assert.equal(migrations[2].version, version);\n        done();\n      });\n    }\n  });\n});\n```\n\nIf you don't do this sane check and the migration file or directory won't exist, you'll receive an error during when trying to run the migration.\n\n### hasMigration\n\nThe `hasMigration` function allows you to check up front, if `migrate-semver` has applied a particular migration already:\n\n```js\nmigrateSemVer.hasMigration({ version, direction: 'up' }, (err, hasMigration) =\u003e { \n  assert.equal(hasMigration, true);\n  done();\n});\n```\n \n### customOptions\n\nYou can pass custom options to the `up` function as child object of the `options` parameter.\nThese `customOptions` will be passed into the plugin and if the plugin handles them appropriately, they'll be passed into every migration (the 'how' depends on the particular plugin).\n \n```js\nmigrateSemVer.up({ version, customOptions }, err =\u003e {\n  assert.equal(migrations[2].version, version);\n  done();\n});\n```\n\n## Plugins\n\nAs said, `migrate-semver` is database agnostic, so the concrete implementation for a particular database has to be done by a plugin which has to be passed to the `SemVerMigration` ctor function as well.\n\nCurrently there is one plugin implementation available for MongoDb using `mongoose`.\n\nFor more details on `migrate-semver-mongoose` visit the [GitHub-Repository](https://github.com/PDMLab/migrate-semver-mongoose).\n\nIf you want to implement your own `migrate-semver` plugin, just implement a Node.js module exporting the following functions:\n\n```js\nconst plugin = function(options, callback) {\n\n  const hasMigrationsTable = function (options, callback) {\n    callback(null, hasTable);\n  };\n\n  const createMigrationsTable = function (options, callback) {\n    callback();\n  };\n\n  /**\n  * @param {Object} options\n  * @param {String} options.version\n  * @param {String} options.direction - may be up or down\n  */\n  const hasMigration = function (options, callback) {\n    callback(null, hasMigrationApplied);\n  };\n\n  const getLatestAppliedMigration = function (callback) {\n    return callback(null, latestMigration); // e.g. '0.2.0'\n  };\n\n  /**\n  * @param {Object} options\n  * @param {String} options.version\n  * @param {String} options.direction - may be up or down\n  */\n  const addMigrationToMigrationsTable = function (options, callback) {\n    callback();\n  };\n\n  /**\n  * @param {Object} options\n  * @param {String} options.version\n  * @param {String} options.migrationsDirectory\n  * @param {Object} [options.customOptions]\n  */\n  const up = function (options, continueWith) {\n    const migrationsDirectory = options.migrationsDirectory;\n    const migrationPath = path.join(migrationsDirectory, options.version, 'index-up');\n    const migration = require(migrationPath); // eslint-disable-line\n    migration.up(options, callback);\n  };\n\nreturn {\n    connect,\n    hasMigrationsTable,\n    createMigrationsTable,\n    hasMigration,\n    getLatestAppliedMigration,\n    addMigrationToMigrationsTable,\n    up\n  };\n}\n\nmodule.exports = plugin;\n```\n\nAfter this, just `require` your plugin and pass it to the `SemVerMigration` ctor function.\n\n## Running the tests\n\n```\nnpm test\n```\n\n## Want to help?\n\nThis project is just getting off the ground and could use some help with cleaning things up and refactoring.\n\nIf you want to contribute - we'd love it! Just open an issue to work against so you get full credit for your fork. You can open the issue first so we can discuss and you can work your fork as we go along.\n\nIf you see a bug, please be so kind as to show how it's failing, and we'll do our best to get it fixed quickly.\n\nBefore sending a PR, please [create an issue](https://github.com/PDMLab/composefile/issues/new) to introduce your idea and have a reference for your PR.\n\nAlso please add tests and make sure to run `npm run eslint`.\n\n## License\n\nMIT License\n\nCopyright (c) 2016 PDMLab\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fmigrate-semver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdmlab%2Fmigrate-semver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fmigrate-semver/lists"}