Skip to content

Commit

Permalink
implement command update
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodenadai committed Apr 25, 2017
1 parent 94c16bc commit c5d154c
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 40 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
@@ -1,6 +1,11 @@
language: node_js
node_js:
- '6'
- '4'
after_script:
- 'cat coverage/lcov.info | ./node_modules/.bin/coveralls'
addons:
apt:
packages:
- oracle-java8-set-default
services:
- postgresql
8 changes: 0 additions & 8 deletions .yo-rc.json

This file was deleted.

71 changes: 64 additions & 7 deletions index.js
@@ -1,10 +1,67 @@
'use strict';
module.exports = (input, opts) => {
if (typeof input !== 'string') {
throw new TypeError(`Expected a string, got ${typeof input}`);
const childProcess = require('child_process');

class Liquibase {
constructor(params = {}) {
this.liquibase = params.liquibase || process.env.NODE_LIQUIBASE || 'lib/liquibase-core-3.5.3.jar';
this.driver = params.driver || process.env.NODE_LIQUIBASE_DRIVER || 'org.postgresql.Driver';
this.classpath = params.classpath || process.env.NODE_LIQUIBASE_CLASSPATH || 'lib/postgresql-9.4-1201.jdbc4.jar';

this.changeLogFile = params.changeLogFile || process.env.NODE_LIQUIBASE_CHANGE_LOG_FILE;
this.url = params.url || process.env.NODE_LIQUIBASE_URL;
this.username = params.username || process.env.NODE_LIQUIBASE_USERNAME;
this.password = params.password || process.env.NODE_LIQUIBASE_PASSWORD;

this.defaultsFile = params.defaultsFile || process.env.NODE_LIQUIBASE_DEFAULTS_FILE;

if (!this.defaultsFile) {
if (!this.changeLogFile) {
throw new Error('changeLogFile is undefined');
}
if (!this.url) {
throw new Error('url is undefined');
}
if (!this.username) {
throw new Error('username is undefined');
}
}
}

opts = opts || {};
get command() {
const cmd = `java -jar ${this.liquibase}`;

if (this.defaultsFile) {
return `${cmd} --defaultsFile=${this.defaultsFile}`;
}

return `${cmd}\
--driver=${this.driver}\
--classpath=${this.classpath}\
--changeLogFile=${this.changeLogFile}\
--url="${this.url}"\
--username=${this.username}\
--password=${this.password}\
`;
}

exec(command, options = {}) {
let child;
let promise = new Promise((resolve, reject) => {
child = childProcess
.exec(command, options, (error, stdout, stderr) => {
if (error) {
error.stderr = stderr;
return reject(error);
}
resolve({stdout: stdout});
});
});
promise.child = child;
return promise;
}

update() {
return this.exec(`${this.command} update`);
}
}

return input + ' & ' + (opts.postfix || 'rainbows');
};
module.exports = (params = {}) => new Liquibase(params);
Binary file added lib/liquibase-core-3.5.3.jar
Binary file not shown.
Binary file added lib/postgresql-9.4-1201.jdbc4.jar
Binary file not shown.
17 changes: 10 additions & 7 deletions package.json
@@ -1,25 +1,28 @@
{
"name": "liquibase",
"version": "0.0.0",
"description": "My stellar module",
"version": "0.0.2",
"description": "Node.js wrap for Liquibase",
"license": "MIT",
"repository": "pablodenadai/liquibase",
"repository": "pablodenadai/node-liquibase",
"author": {
"name": "Pablo De Nadai",
"email": "pablodenadai@gmail.com",
"url": "localhost"
"url": "https://github.com/pablodenadai/node-liquibase"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava"
"test": "xo && nyc ava -s"
},
"files": [
"index.js"
],
"keywords": [
""
"liquibase",
"sql",
"postgresql",
"migration"
],
"dependencies": {},
"devDependencies": {
Expand Down
75 changes: 62 additions & 13 deletions readme.md
@@ -1,6 +1,6 @@
# liquibase [![Build Status](https://travis-ci.org/pablodenadai/liquibase.svg?branch=master)](https://travis-ci.org/pablodenadai/liquibase) [![Coverage Status](https://coveralls.io/repos/github/pablodenadai/liquibase/badge.svg?branch=master)](https://coveralls.io/github/pablodenadai/liquibase?branch=master)
# Node Liquibase [![Build Status](https://travis-ci.org/pablodenadai/node-liquibase.svg?branch=master)](https://travis-ci.org/pablodenadai/node-liquibase) [![Coverage Status](https://coveralls.io/repos/github/pablodenadai/node-liquibase/badge.svg?branch=master&cache-buster=1)](https://coveralls.io/github/pablodenadai/node-liquibase?branch=master)

> My stellar module
> Node.js wrap for Liquibase

## Install
Expand All @@ -15,31 +15,80 @@ $ npm install --save liquibase
```js
const liquibase = require('liquibase');

liquibase('unicorns');
//=> 'unicorns & rainbows'
liquibase({
defaultsFile: 'resources/liquibase/liquibase.properties'

// liquibase: 'lib/liquibase-core-3.5.3.jar',
// driver: 'org.postgresql.Driver',
// classpath: 'lib/postgresql-9.4-1201.jdbc4.jar',
// changeLogFile: 'resources/liquibase/db.changelog.xml',
// url: 'jdbc:postgresql://localhost:5432/postgres',
// username: 'postgres',
// password: 'admin'
})
.update()
.then(() => console.log('success'))
.catch((err) => console.log('fail', err));
```


## API

### liquibase(input, [options])
### liquibase(params)

#### input
#### params

##### liquibase

Type: `string`

Lorem ipsum.
Path to core Liquibase JAR file. Defaults to `process.env.NODE_LIQUIBASE` or `'lib/liquibase-core-3.5.3.jar'`.

##### driver

Type: `string`

#### options
JDBC driver's name. Defaults to `process.env.NODE_LIQUIBASE_DRIVER` or `'org.postgresql.Driver'`.

##### classpath

Type: `string`

##### foo
Path to JDBC driver JAR file. Defaults to `process.env.NODE_LIQUIBASE_CLASSPATH` or `'lib/postgresql-9.4-1201.jdbc4.jar'`.

Type: `boolean`<br>
Default: `false`
##### changeLogFile

Type: `string`

Path to Liquibase changelog entry file. Defaults to `process.env.NODE_LIQUIBASE_CHANGE_LOG_FILE`.

##### url

Type: `string`

Database URL. Defaults to `process.env.NODE_LIQUIBASE_URL`.

##### username

Type: `string`

Database username. Defaults to `process.env.NODE_LIQUIBASE_USERNAME`.

##### password

Type: `string`

Database password. Defaults to `process.env.NODE_LIQUIBASE_PASSWORD`.

##### defaultsFile

Type: `string`

Lorem ipsum.
Path to Liquibase properties file. If provided, it will take priority over other properties. Defaults to `process.env.NODE_LIQUIBASE_DEFAULTS_FILE`.

## Todo
- Implement other [Liquibase commands](http://www.liquibase.org/documentation/command_line.html).

## License

MIT © [Pablo De Nadai](http://localhost)
MIT © [Pablo De Nadai](https://github.com/pablodenadai/node-liquibase)
11 changes: 11 additions & 0 deletions resources/liquibase/db.changelog.release_1_0_0.xml
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">

<include file="resources/liquibase/release_1_0_0/001_create_test_table.sql"/>
<include file="resources/liquibase/release_1_0_0/002_create_test_view.sql"/>
</databaseChangeLog>
10 changes: 10 additions & 0 deletions resources/liquibase/db.changelog.release_2_0_0.xml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">

<include file="resources/liquibase/release_2_0_0/001_create_test_index.sql"/>
</databaseChangeLog>
10 changes: 10 additions & 0 deletions resources/liquibase/db.changelog.xml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">

<include file="resources/liquibase/db.changelog.release_1_0_0.xml"/>
<include file="resources/liquibase/db.changelog.release_2_0_0.xml"/>
</databaseChangeLog>
6 changes: 6 additions & 0 deletions resources/liquibase/liquibase.properties
@@ -0,0 +1,6 @@
driver: org.postgresql.Driver
classpath: lib/postgresql-9.4-1201.jdbc4.jar
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: admin
changeLogFile: resources/liquibase/db.changelog.xml
8 changes: 8 additions & 0 deletions resources/liquibase/release_1_0_0/001_create_test_table.sql
@@ -0,0 +1,8 @@
--liquibase formatted sql
--changeset liquibase-demo:release_1_0_0.001_create_test_table.sql

CREATE TABLE liquibase_test (
key VARCHAR(64),
value VARCHAR(255),
PRIMARY KEY(key)
);
5 changes: 5 additions & 0 deletions resources/liquibase/release_1_0_0/002_create_test_view.sql
@@ -0,0 +1,5 @@
--liquibase formatted sql
--changeset liquibase-demo:release_2_0_0.002_create_test_view.sql

CREATE OR REPLACE VIEW
liquibase_test_view AS select * from liquibase_test;
5 changes: 5 additions & 0 deletions resources/liquibase/release_2_0_0/001_create_test_index.sql
@@ -0,0 +1,5 @@
--liquibase formatted sql
--changeset liquibase-demo:release_2_0_0.01_create_test_view.sql

CREATE OR REPLACE VIEW
liquibase_test_view AS select * from liquibase_test;
61 changes: 57 additions & 4 deletions test.js
@@ -1,9 +1,62 @@
import test from 'ava';
import fn from '.';

test('title', t => {
const err = t.throws(() => fn(123), TypeError);
t.is(err.message, 'Expected a string, got number');
test('constructor fail - no changeLogFile param or env variable', t => {
const error = t.throws(fn);

t.is(fn('unicorns'), 'unicorns & rainbows');
t.is(error.message, 'changeLogFile is undefined');
});

test('constructor fail - no url param or env variable', t => {
const error = t.throws(() => {
fn({
changeLogFile: 'resources/liquibase/db.changelog.xml'
});
});

t.is(error.message, 'url is undefined');
});

test('constructor fail - no username param or env variable', t => {
const error = t.throws(() => {
fn({
changeLogFile: 'resources/liquibase/db.changelog.xml',
url: 'jdbc:postgresql://localhost:5432/postgres'
});
});

t.is(error.message, 'username is undefined');
});

test('update success - params', async t => {
await fn({
changeLogFile: 'resources/liquibase/db.changelog.xml',
url: 'jdbc:postgresql://localhost:5432/postgres',
username: 'postgres',
password: 'admin'
})
.update()
.then(data => t.is(data.stdout, ''))
.catch(() => t.fail());
});

test('update success - defaults file', async t => {
await fn({
defaultsFile: 'resources/liquibase/liquibase.properties'
})
.update()
.then(data => t.is(data.stdout, ''))
.catch(() => t.fail());
});

test('update fail - params with wrong credentials', async t => {
await fn({
changeLogFile: 'resources/liquibase/db.changelog.xml',
url: 'jdbc:postgresql://localhost:5432/postgres',
username: 'postgres!123',
password: 'admin!123'
})
.update()
.then(() => t.fail())
.catch(() => t.pass());
});

0 comments on commit c5d154c

Please sign in to comment.