npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@viva-eng/mysql-migrate

v2.0.2

Published

A utility defining, modifying, and migrating MySQL databases

Downloads

1

Readme

A simple migration management tool for MySQL

Install

$ npm install -g @viva-eng/mysql-migrate

Usage

mym <command>

Commands:
  mym init [directory]  Create a new migration project
  mym bootstrap         Bootstraps a database
  mym create <name>     Creates a new migration
  mym list              Lists out all existing migrations
  mym migrate           Migrates the database up to the given version
  mym rollback          Rolls back the database to the given version
  mym history           Returns the recent migration history of the database
  mym completion        Outputs a bash completion script for mym

Options:
  --version      Show version number                                                                           [boolean]
  -v, --verbose  Enables verbose logging                                                      [boolean] [default: false]
  --help         Show help                                                                                     [boolean]

Starting a new Project

When starting up a new database project, you'll first need to get your database configuration all set up.

$ mym init ./my-new-project

This will create a new directory for you called my-new-project, and create your .mym.json file with the basic starting structure for your configuration.

{
  "environments": {
    "default": {
      "host": "localhost",
      "port": 3306,
      "user": "",
      "password": "",
      "database": ""
    }
  }
}

After you fill in all the fields with your database configuration, you'll need to bootstrap the project. This will create the history table in your database where we keep track of the current status of the database. In your project directory:

$ mym bootstrap

Creating new database migrations

Creating a new migration is as simple as calling mym create

$ mym create my-new-migration

This will create a new directory inside your project like 0000-my-new-migration which will contain a number of files.

Note: The name of the directory is important, as the migrations must be stored in sorted order, and so are prefixed with a timestamp. Renaming the directory can lead to unintended side-effects.

migrate.sql
migrate-hooks.js
rollback.sql
rollback-hooks.js

The migrate.sql file contains the query (or queries) that will run when the migration is executed; Likewise, the rollback.sql file contains any queries to run when rolling back the migration. The migrate-hooks.js and rollback-hooks.js files allow you to write JavaScript functions that will be run before and/or after then migration and rollback are run.

Hooks

The hooks files alongside your migration and rollback scripts allow you define functions to execute before and after the scripts execute. However, they also have one other function. The before hooks also enable to make modifications to the queries before they are run, such as populating variables, or even replacing the query completely. If your before hook returns a string (or returns a promise that resolves to a string), the string will be treated as a query to be exectued in place of the script contents.

migrate-hooks.js
exports.before = async (params) => {
  // This tells you what type of action is being performed
  // ("migrate" or "rollback")
  console.log(params.action);

  // This tells you migration version of the script
  // being run (the name of the directory the script is in)
  console.log(params.version);

  // This is the SQL from the script file
  console.log(params.sql);
};

exports.after = async (error, params) => {
  // If an error occured, it will be passed in here
  if (error) {
    console.log(error);
    return;
  }

  // This tells you what type of action is being performed
  // ("migrate" or "rollback")
  console.log(params.action);

  // This tells you migration version of the script
  // being run (the name of the directory the script is in)
  console.log(params.version);

  // This is the SQL from the script file
  console.log(params.sql);

  // This is the SQL that was actually executed (different
  // from the file contents if it was modified by your
  // before hook)
  console.log(params.finalSql);

  // This is the result from the MySQL query call
  console.log(params.result);
};