Skip to content

Commit

Permalink
Merge pull request #28 from kriasoft/fix-migration-bug
Browse files Browse the repository at this point in the history
Fix migration bug where using -- as an actual string value gets lost
  • Loading branch information
Theo Gravity authored Apr 30, 2017
2 parents d83e0ad + 93b86e5 commit d9e8af4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 9 additions & 0 deletions migrations/003-test-cert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Up
CREATE TABLE whatever ( certificate TEXT );
INSERT INTO whatever ( certificate ) VALUES (
'-----BEGIN CERTIFICATE-----
some contents
-----END CERTIFICATE-----');

-- Down
DROP TABLE whatever;
4 changes: 2 additions & 2 deletions src/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ class Database {
reject(new Error(message));
} else {
/* eslint-disable no-param-reassign */
migration.up = up.replace(/^--.*?$/gm, '').trim(); // Remove comments
migration.down = down.replace(/^--.*?$/gm, '').trim(); // and trim whitespaces
migration.up = up.replace(/^-- .*?$/gm, '').trim();// Remove comments
migration.down = down.trim(); // and trim whitespaces
/* eslint-enable no-param-reassign */
resolve();
}
Expand Down
7 changes: 6 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,16 @@ it('Should migrate the database', (done) => {
let p = db.open(':memory:');
p = p.then(() => db.migrate());
p = p.then(() => db.all('SELECT id, name FROM migrations').then((result) => {
expect(result).to.be.deep.equal([{ id: 1, name: 'initial' }, { id: 2, name: 'some-feature' }]);
expect(result).to.be.deep.equal([{ id: 1, name: 'initial' }, { id: 2, name: 'some-feature' }, { id: 3, name: 'test-cert' }]);
}));
p = p.then(() => db.all('SELECT * FROM Category').then((result) => {
expect(result).to.be.deep.equal([{ id: 1, name: 'Test' }]);
}));

p = p.then(() => db.all('SELECT certificate from whatever').then((result) => {
expect(result[0].certificate).to.be.equal('-----BEGIN CERTIFICATE-----\nsome contents\n-----END CERTIFICATE-----');
}));

p = p.then(() => db.close());
p.then(done, done);
});

0 comments on commit d9e8af4

Please sign in to comment.