Skip to content
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

launchfort/flock-mysql

Repository files navigation

Flock MySQL

Flock MySQL is a Flock plugin for MySQL.

Install

npm install launchfort/flock-mysql

Usage

// .flockrc.js
const { DefaultMigrator, NodeModuleMigrationProvider } = require('@launchfort/flock')
const { DataAccessProvider, TemplateProvider } = require('@launchfort/flock-mysql')

const migrationDir = 'migrations'
const migrationTableName = 'migration'
const connectionOptions = {
  host: 'localhost',
  database: process.env.MYSQL_DATABASE,
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASSWORD
}
const dap = new DataAccessProvider({ migrationTableName, connectionOptions })
const mp = new NodeModuleMigrationProvider({ migrationDir })

exports.migrator = new DefaultMigrator(mp, dap)
exports.migrationDir = migrationDir
exports.templateProvider = new TemplateProvider()

Migrations

When writing migrations that use flock-mysql then the QueryInterface#query method signature is identical to that of the pg's Client#query method.

Example:

exports.up = queryInterface => {
  const sql = 'SELECT * FROM user WHERE age = ?'
  const values = [ 1 ]
  return queryInterface.query({ sql, values })
}

The QueryInterface#query method accepts a query object with the following shape:

{
  sql: string,
  values?: any[],
  timeout?: number
}

API

Flock mysql exports implementations of Flock's DataAccessProvider and TemplateProvider as DataAccessProvider and TemplateProvider classes.

The DataAccessProvider class will connect to your MySQL DB by reading the properties from connectionOptions. This object is the same options object accepted by the mysql module's createConnection function.

NOTE: The only difference with the connection options is that flock-mysql defaults the timezone option to Z instead of local.

class DataAccessProvider implements Flock.DataAccessProvider {
  constructor ({
    migrationTableName = 'migration',
    acquireLock = true,
    connectionOptions = /* REQUIRED */ } = {})
}

Additionally, by default the DataAccessProvider will attempt to acquire an application lock immediately after connecting to the database. This behaviour can be overridden by setting the acquireLock option to false. Acquiring a lock helps to prevent concurrent migrations from occuring.

See: https://devcenter.heroku.com/articles/release-phase

About

Flock plugin for MySQL

Resources

Stars

Watchers

Forks

Packages

No packages published