Coder Social home page Coder Social logo

merkel's Introduction

merkel

Handles your database migration crisis

npm downloads linux build windows build codecov dependencies Status node code style: prettier semantic-release license chat: on gitter

merkel is a framework-agnostic database migration tool designed to autonomously run in Continuous Deployment, with rollbacks in mind.

Installation

npm install --global merkel or npm install --save-dev merkel

Run merkel init to initialize a .merkelrc.json and install a git hook

Is the .merkelrc.json required?
No, but it holds the migration directory, and if you use it, you could change it later because the migration directory at any time is known through git.

Is the git hook required?
No, but it helps you type less. Read on to learn more.

Workflow

Make changes to your model files

Let's say you made some changes to your model files that require a database migration.

Generate a migration file

Before you commit, create a new migration file by running merkel generate. This will generate a new migration file inside your migration directory (default ./migrations). If a tsconfig.json was detected, the migration file will be in TypeScript. You can change the migration directory with --migration-dir and provide a custom template with --template. Like all options, they can also be set in .merkelrc.json or passed through environment variables. The name of the migration file can be set with --name. By default, a UUID is used.

Why UUIDs?
In opposite to sequential IDs or timestamps, UUIDs allow separate developers to write migration files without any conflicts. There can be migration files introduced in separate git branches or commits with migration files even cherry-picked across repositories, they will not create merge conflicts. This works well with the distributed nature of git. Providing a custom, unique name is equally good.

Write your migration file

You migration file exports two functions: up and down. The up function is expected to make all necessary database changes compared to the previous commit. The down function should try to reverse as good as possible. Both functions should return a Promise. Your migration file can use any dependency you want to execute this task, import a database connection, use a low-level driver or high-level ORM, use the AWS SDK, spawn child processes...

Example:

const db = require('../db')

exports.up = function up() {
  return new Promise((resolve, reject) => db.connect(err => (err ? reject(err) : resolve()))).then(() =>
    db.query('ALTER TABLE order_details RENAME COLUMN notes TO order_notes')
  )
}

exports.down = function down() {
  return new Promise((resolve, reject) => db.connect(err => (err ? reject(err) : resolve()))).then(() =>
    db.query('ALTER TABLE order_details RENAME COLUMN order_notes TO notes')
  )
}

Where db.js could look like this:

const pg = require('pg')

module.exports = new pg.Client(process.env.DB)

Commit

Add your model changes and the migration file, and run git commit. If you installed the git hook, you will see that merkel detected that you added a migration file and included a command like this in your commit message:

[merkel up d12f99e4-710d-4d4a-94f8-13d9d121bac5]

This command will later be parsed by merkel migrate.

Migrate

After checking out the new commit in Continuous Deployment or on a coworker's machine, run merkel migrate. merkel will query the database's merkel_meta table for the last migration run, and what the HEAD was when that migration was run.

To be able to do this, you must provide merkel with a database connection URI. You can do this either through the --db option or through the MERKEL_DB environment variable. It is not recommended to save this in the .merkelrc.json file, as connection data differs across environments.

To query the database, merkel needs a database driver. The driver is detected through the protocol part of the connection URI. In order to allow many dialects, it is not a dependency of merkel, but instead required from the current working directory, which means you need one installed in your project (you probably already have). See supported dialects.

merkel then asks git which commits were made since then the last migration. It then scans the new commits for the merkel commands like you saw in the example above.

Pending migrations:

The confirmation prompt will only show up if run in a TTY context and can be disabled with --confirm=false. To get only status output, run merkel status.

merkel will then execute the migrations in that exact order, and log these in the database as they happen.

But...

What if a migration fails?

If a migration fails (throws an exception / returns a rejected promise), the schema your source files expect doesn't match your database schema anymore. You now have two options:

  • Quickly fix the migration file in a separate commit. That commit message should not include any merkel command. The next merkel migrate execution will then start where it migration chain broke and will still run the migration files in the order they were specified in the commit messages, but with the newest version of the migration file.
  • Completely revert the deployment to the previous state, see reverting migrations

What if I need to revert a deployment?

Reverting a deployment with git revert

When you do a git revert, normally git will create a commit that is the exact inverse of the commits you want to invert. This means, if one of the commits added a migration file, it will now be deleted. This is not desirable. Instead of deleting the migration files, you want to keep them, but let merkel migrate them down.

To accomplish this, make sure to run git revert with the --no-commit/-n option. This will not make the commit immediately, but only stage the proposed changes, allowing you to edit them.

Run git status to see if any migration files got deleted. If yes, you can unstage the whole migration dir with

git reset HEAD migrations

And then bring them back with

git checkout -- migrations

Now we only need to tell merkel that we want to run down migrations. Run git commit, and in the commit message, add a merkel down command with all the migrations that need to be undone. The command can look like this:

[merkel down 6e28dcef-16f8-4a81-8783-aedc93043fa4]
[merkel down bab251c6-4aee-4137-8a83-6e6fcab29cdf]
[merkel down 43b15c65-2f6d-447d-bbcf-0efe3a34fd10]

Or multi-line like this:

[
  merkel down
  6e28dcef-16f8-4a81-8783-aedc93043fa4
  bab251c6-4aee-4137-8a83-6e6fcab29cdf
  43b15c65-2f6d-447d-bbcf-0efe3a34fd10
]

The order is important here!

Reverting a deployment with git reset

Let's imagine you are using a production branch which always points to a specific commit on master, and regularly gets updated with git reset to point to a new or older commit. After you git push --force, the current HEAD will suddenly be behind the last migration run in the database. merkel will detect this automatically, and run the migrations between HEAD and the last migrations down and in reverse order.

What if I checkout an older commit on my dev machine?

The same applies as for Reverting a deployment with git reset.

What if I want to change the migration directory?

If you are using a .merkelrc.json that has the migration dir specified, merkel will use git show to get the migration directory at the time the commit was made.

If not, then this is not possible without using the ability to run old migrations.

Programmatic usage

You can use merkel programmatically, for example in your favourite task runner. API documentation is available here.

TypeScript definitions are included.

Supported dialects

Currently only PostgreSQL. pg version ^6 must be installed in your project.


Wir schaffen das. – Angela Merkel

merkel's People

Contributors

eseliger avatar felixfbecker avatar greenkeeper[bot] avatar greenkeeperio-bot avatar maxjoehnk avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

loveconlee

merkel's Issues

An in-range update of @types/node is breaking the build 🚨

Version 8.0.58 of @types/node was just published.

Branch Build failing 🚨
Dependency @types/node
Current Version 8.0.57
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/appveyor/branch AppVeyor build failed Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build failed Details

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Stable release

I will bump version to v1.0.0 once I have this working in a project in production

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two-Factor Authentication, make configure the auth-only level is supported. semantic-release cannot publish with the default auth-and-writes level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml
  • The new Node.js version is in-range for the engines in 1 of your package.json files, so that was left alone

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected πŸ€–


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency @types/mkdirp to v1
  • chore(deps): update dependency @types/node to v20
  • chore(deps): update dependency chai to v5
  • chore(deps): update dependency del to v7
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency mocha to v10 (mocha, @types/mocha)
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency rimraf to v5
  • chore(deps): update dependency sinon to v17 (sinon, @types/sinon)
  • chore(deps): update dependency typescript to v5
  • fix(deps): update dependency chalk to v5
  • fix(deps): update dependency inquirer to v9 (inquirer, @types/inquirer)
  • fix(deps): update dependency mkdirp to v3
  • fix(deps): update dependency update-notifier to v7
  • fix(deps): update dependency uuid to v9 (uuid, @types/uuid)
  • fix(deps): update dependency yargs to v17 (yargs, @types/yargs)
  • πŸ” Create all rate-limited PRs at once πŸ”

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • chalk ^2.3.0
  • inquirer ^6.0.0
  • mkdirp ^0.5.1
  • mz ^2.4.0
  • sql-template-strings ^2.2.0
  • update-notifier ^2.3.0
  • uuid ^3.1.0
  • yargs ^12.0.0
  • @sourcegraph/prettierrc ^2.0.0
  • @sourcegraph/tslint-config ^12.0.0
  • @types/chai ^4.1.3
  • @types/chai-as-promised ^7.1.0
  • @types/del ^3.0.1
  • @types/globby ^6.1.0
  • @types/inquirer 0.0.42
  • @types/mkdirp ^0.5.1
  • @types/mocha ^5.0.0
  • @types/mz 0.0.32
  • @types/node ^9.6.14
  • @types/pg ^7.4.8
  • @types/sinon ^5.0.0
  • @types/uuid ^3.4.3
  • @types/yargs ^12.0.0
  • chai ^4.1.2
  • chai-as-promised ^7.1.1
  • cz-conventional-changelog ^2.0.0
  • del ^3.0.0
  • husky ^0.14.3
  • mocha ^5.1.1
  • nyc ^13.0.0
  • pg ^7.4.3
  • prettier 1.12.1
  • rimraf ^2.5.4
  • semantic-release ^15.4.0
  • sinon ^7.0.0
  • source-map-support ^0.5.5
  • tslint ^5.10.0
  • typedoc ^0.11.0
  • typescript ^3.0.0
  • validate-commit-msg ^2.14.0
  • node >=6
travis
.travis.yml
  • node 8

  • Check this box to trigger a request for Renovate to run again on this repository

An in-range update of @types/mocha is breaking the build 🚨

Version 2.2.48 of @types/mocha was just published.

Branch Build failing 🚨
Dependency @types/mocha
Current Version 2.2.47
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

@types/mocha is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build failed Details
  • ❌ continuous-integration/appveyor/branch AppVeyor build failed Details

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of inquirer is breaking the build 🚨

Version 4.0.1 of inquirer was just published.

Branch Build failing 🚨
Dependency inquirer
Current Version 4.0.0
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

inquirer is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • ❌ continuous-integration/appveyor/branch Waiting for AppVeyor build to complete Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes v4.0.1
  • Update dependency to fix a LGPL license issue.
Commits

The new version differs by 3 commits.

  • 00f5050 4.0.1
  • 9895054 Bumped version to 4.1.0, bumped external-editor dependency to ^2.1.0 … (#616)
  • 62a72d4 Fix typo (#612)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/chai is breaking the build 🚨

Version 4.0.9 of @types/chai was just published.

Branch Build failing 🚨
Dependency @types/chai
Current Version 4.0.8
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

@types/chai is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/appveyor/branch AppVeyor build failed Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build failed Details

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/pg is breaking the build 🚨

Version 7.4.4 of @types/pg was just published.

Branch Build failing 🚨
Dependency @types/pg
Current Version 7.4.3
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

@types/pg is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • βœ… continuous-integration/appveyor/branch AppVeyor build succeeded Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build failed Details

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of sinon is breaking the build 🚨

Version 4.1.4 of sinon was just published.

Branch Build failing 🚨
Dependency sinon
Current Version 4.1.3
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

sinon is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • βœ… continuous-integration/appveyor/branch AppVeyor build succeeded Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes Minor fix for Symbol names and deprecation of spy.reset
  • Fix: assertion error messages did not handle Symbol names (#1640)
  • Deprecate spy.reset(), use spy.resetHistory() instead (#1446)
Commits

The new version differs by 36 commits.

  • 1ea2749 Update docs/changelog.md and set new release id in docs/_config.yml
  • 078c082 Add release documentation for v4.1.4
  • 571263e 4.1.4
  • f2ee9f1 Update History.md and AUTHORS for new release
  • a8262dd Assertion error messages handle symbolic method names
  • 8fa1e14 Merge pull request #1641 from mroderick/point-to-stack-overflow
  • 7c1ebd0 Update issue links to point to sinonjs/sinon
  • 93418f6 Update documentation to emphasize Stack Overflow
  • ca9e2fa Merge pull request #1636 from fearphage/fix-headless-chrome-in-circle
  • 39186f4 use google-chrome-unstable for tests
  • 6315621 invalidate cache
  • d078af9 try using default chrome install
  • 177c4b6 upgraded to the lastest official version of mochify
  • ecdc4e0 test with updated mochify
  • 360c2e7 added more details and notes

There are 36 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Dependency deprecation warning: validate-commit-msg (npm)

On registry https://registry.npmjs.org/, the "latest" version (v2.14.0) of dependency validate-commit-msg has the following deprecation notice:

Check out CommitLint which provides the same functionality with a more user-focused experience.

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Affected package file(s): package.json

If you don't care about this, you can close this issue and not be warned about validate-commit-msg's deprecation again. If you would like to completely disable all future deprecation warnings then add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

log command

Should show the migration log from the database, similar to git log

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.