From 5a670e8beaf55a86c5575db00fccbc9040dae693 Mon Sep 17 00:00:00 2001 From: Jari Bakken Date: Mon, 20 Oct 2014 18:22:29 +0200 Subject: [PATCH] Add a script that generates a liquibase changelog. Should be internal. --- unleash-server/README.md | 10 +++- unleash-server/package.json | 3 +- .../scripts/generate-liquibase-artifact.js | 51 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100755 unleash-server/scripts/generate-liquibase-artifact.js diff --git a/unleash-server/README.md b/unleash-server/README.md index fb6ed82f9a..220746b841 100644 --- a/unleash-server/README.md +++ b/unleash-server/README.md @@ -1,7 +1,7 @@ # unleash-server unleash-server is a place to ask for the status of features. -# Important commands: +## Important commands: ``` // Set up DB @@ -20,3 +20,11 @@ http://localhost:4242/features // Execute tests: npm test ``` + +## Making a schema change + +1. Create `migrations/sql/NNN-your-migration-name.up.sql` with your change in SQL. +2. Create `migrations/sql/NNN-your-migration-name.down.sql` with the rollback of your change in SQL. +3. Run `db-migrate create your-migration-name` and edit the generated file to run the above SQL files. +4. Run `npm run db-migrate-up`. +5. Generate LB artifact using `scripts/generate-liquibase-artifact` (TODO: make this internal) diff --git a/unleash-server/package.json b/unleash-server/package.json index 6ab6b697ec..fb9644a376 100644 --- a/unleash-server/package.json +++ b/unleash-server/package.json @@ -45,6 +45,7 @@ "jshint": "2.5.2", "mocha": "1.20.1", "supertest": "0.13.0", - "supervisor": "~0.6.0" + "supervisor": "~0.6.0", + "xmlbuilder": "^2.4.4" } } diff --git a/unleash-server/scripts/generate-liquibase-artifact.js b/unleash-server/scripts/generate-liquibase-artifact.js new file mode 100755 index 0000000000..ff56885795 --- /dev/null +++ b/unleash-server/scripts/generate-liquibase-artifact.js @@ -0,0 +1,51 @@ +#!/usr/bin/env node + +// TODO: move this to somewhere internal + +var builder = require('xmlbuilder'); +var util = require('util'); +var path = require('path'); +var fs = require('fs'); +var sqlRoot = path.resolve(__dirname, '../migrations/sql'); +var encoding = 'UTF-8'; + +var changeLog = builder.create('databaseChangeLog').dec('1.0', encoding); +changeLog.att('xmlns', 'http://www.liquibase.org/xml/ns/dbchangelog'); +changeLog.att('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); +changeLog.att('xmlns:ext', 'http://www.liquibase.org/xml/ns/dbchangelog-ext'); +changeLog.att('xsi:schemaLocation', + 'http://www.liquibase.org/xml/ns/dbchangelog ' + + 'http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd ' + + 'http://www.liquibase.org/xml/ns/dbchangelog-ext ' + + 'http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd'); + +fs.readdir(sqlRoot, function (err, files) { + if (err) { throw err; } + + var changes = {}; + + files.forEach(function (sqlFile) { + var match = sqlFile.match(/(.+?)\.(up|down)\.sql/); + + if (!match) { + throw util.format('invalid sql file name, missing up|down: %s', sqlFile); + } + + var name = match[1]; + var direction = match[2]; + + changes[name] = changes[name] || {}; + changes[name][direction] = fs.readFileSync(path.resolve(sqlRoot, sqlFile), {encoding: encoding}); + }); + + Object.keys(changes).forEach(function (name) { + var change = changes[name]; + + var el = changeLog.ele('changeSet', {id: name}); + el.ele('sql', {}, change.up); + el.ele('rollback', {}, change.down); + }); + + util.puts(changeLog.end({pretty: true})); +}); +