-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow 'Down' migrations in .sql files (#530)
* Allow 'Down' migrations in .sql files This commit allows SQL files to have down migrations via comments. Anything below `-- Up migration` will be treated as `up`, anything below a `-- Down migration` will be treated as `down`. A migration file can have either or both. * Fix lint errors * Use Title Case in SQL template file * Make migrateSqlFile synchronous * Review comments * Fix regexp * Rewritten, tests
- Loading branch information
Showing
6 changed files
with
174 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import fs from 'fs' | ||
import { promisify } from 'util' | ||
import { MigrationBuilderActions } from './types' | ||
|
||
const readFile = promisify(fs.readFile) // eslint-disable-line security/detect-non-literal-fs-filename | ||
|
||
const createMigrationCommentRegex = (direction: 'up' | 'down') => | ||
new RegExp(`^\\s*--[\\s-]*${direction}\\s+migration`, 'im') // eslint-disable-line security/detect-non-literal-regexp | ||
|
||
export const getActions = (content: string): MigrationBuilderActions => { | ||
const upMigrationCommentRegex = createMigrationCommentRegex('up') | ||
const downMigrationCommentRegex = createMigrationCommentRegex('down') | ||
|
||
const upMigrationStart = content.search(upMigrationCommentRegex) | ||
const downMigrationStart = content.search(downMigrationCommentRegex) | ||
|
||
const upSql = | ||
upMigrationStart >= 0 | ||
? content.substr(upMigrationStart, downMigrationStart < upMigrationStart ? undefined : downMigrationStart) | ||
: content | ||
const downSql = | ||
downMigrationStart >= 0 | ||
? content.substr(downMigrationStart, upMigrationStart < downMigrationStart ? undefined : upMigrationStart) | ||
: undefined | ||
|
||
return { | ||
up: pgm => pgm.sql(upSql), | ||
down: downSql === undefined ? false : pgm => pgm.sql(downSql), | ||
} | ||
} | ||
|
||
export default async (sqlPath: string) => { | ||
const content = await readFile(sqlPath, 'utf-8') | ||
return getActions(content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
-- up migration | ||
-- Up Migration | ||
|
||
-- Down Migration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import sinon from 'sinon' | ||
import { expect } from 'chai' | ||
import { getActions } from '../src/sqlMigration' | ||
|
||
/* eslint-disable no-unused-expressions */ | ||
|
||
describe('lib/sqlMigration', () => { | ||
describe('getActions', () => { | ||
it('without comments', () => { | ||
const content = 'SELECT 1 FROM something' | ||
const { up, down } = getActions(content) | ||
expect(up).to.exist | ||
expect(down).to.be.false | ||
|
||
const sql = sinon.spy() | ||
expect(up({ sql })).to.not.exist | ||
expect(sql.called).to.be.true | ||
expect(sql.lastCall.args[0].trim()).to.eql(content.trim()) | ||
}) | ||
|
||
it('with up comment', () => { | ||
const content = ` | ||
-- Up Migration | ||
SELECT 1 FROM something | ||
` | ||
const { up, down } = getActions(content) | ||
expect(up).to.exist | ||
expect(down).to.be.false | ||
|
||
const sql = sinon.spy() | ||
expect(up({ sql })).to.not.exist | ||
expect(sql.called).to.be.true | ||
expect(sql.lastCall.args[0].trim()).to.eql(content.trim()) | ||
}) | ||
|
||
it('with both comments', () => { | ||
const upMigration = ` | ||
-- Up Migration | ||
SELECT 1 FROM something` | ||
const downMigration = ` | ||
-- Down Migration | ||
SELECT 2 FROM something` | ||
const content = `${upMigration}${downMigration}` | ||
const { up, down } = getActions(content) | ||
expect(up).to.exist | ||
expect(down).to.exist | ||
|
||
const upSql = sinon.spy() | ||
expect(up({ sql: upSql })).to.not.exist | ||
expect(upSql.called).to.be.true | ||
expect(upSql.lastCall.args[0].trim()).to.eql(upMigration.trim()) | ||
|
||
const downSql = sinon.spy() | ||
expect(down({ sql: downSql })).to.not.exist | ||
expect(downSql.called).to.be.true | ||
expect(downSql.lastCall.args[0].trim()).to.eql(downMigration.trim()) | ||
}) | ||
|
||
it('with both comments in reverse order', () => { | ||
const upMigration = ` | ||
-- Up Migration | ||
SELECT 1 FROM something` | ||
const downMigration = ` | ||
-- Down Migration | ||
SELECT 2 FROM something` | ||
const content = `${downMigration}${upMigration}` | ||
const { up, down } = getActions(content) | ||
expect(up).to.exist | ||
expect(down).to.exist | ||
|
||
const upSql = sinon.spy() | ||
expect(up({ sql: upSql })).to.not.exist | ||
expect(upSql.called).to.be.true | ||
expect(upSql.lastCall.args[0].trim()).to.eql(upMigration.trim()) | ||
|
||
const downSql = sinon.spy() | ||
expect(down({ sql: downSql })).to.not.exist | ||
expect(downSql.called).to.be.true | ||
expect(downSql.lastCall.args[0].trim()).to.eql(downMigration.trim()) | ||
}) | ||
|
||
it('with both comments with some chars added', () => { | ||
const upMigration = ` | ||
-- - up Migration to do Up migration | ||
SELECT 1 FROM something` | ||
const downMigration = ` | ||
-- -- -- Down migration to bring DB down | ||
SELECT 2 FROM something` | ||
const content = `${upMigration}${downMigration}` | ||
const { up, down } = getActions(content) | ||
expect(up).to.exist | ||
expect(down).to.exist | ||
|
||
const upSql = sinon.spy() | ||
expect(up({ sql: upSql })).to.not.exist | ||
expect(upSql.called).to.be.true | ||
expect(upSql.lastCall.args[0].trim()).to.eql(upMigration.trim()) | ||
|
||
const downSql = sinon.spy() | ||
expect(down({ sql: downSql })).to.not.exist | ||
expect(downSql.called).to.be.true | ||
expect(downSql.lastCall.args[0].trim()).to.eql(downMigration.trim()) | ||
}) | ||
}) | ||
}) |