Skip to content

Commit d9e8af4

Browse files
author
Theo Gravity
authored
Merge pull request #28 from kriasoft/fix-migration-bug
Fix migration bug where using -- as an actual string value gets lost
2 parents d83e0ad + 93b86e5 commit d9e8af4

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

migrations/003-test-cert.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Up
2+
CREATE TABLE whatever ( certificate TEXT );
3+
INSERT INTO whatever ( certificate ) VALUES (
4+
'-----BEGIN CERTIFICATE-----
5+
some contents
6+
-----END CERTIFICATE-----');
7+
8+
-- Down
9+
DROP TABLE whatever;

src/Database.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ class Database {
167167
reject(new Error(message));
168168
} else {
169169
/* eslint-disable no-param-reassign */
170-
migration.up = up.replace(/^--.*?$/gm, '').trim(); // Remove comments
171-
migration.down = down.replace(/^--.*?$/gm, '').trim(); // and trim whitespaces
170+
migration.up = up.replace(/^-- .*?$/gm, '').trim();// Remove comments
171+
migration.down = down.trim(); // and trim whitespaces
172172
/* eslint-enable no-param-reassign */
173173
resolve();
174174
}

test/spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,16 @@ it('Should migrate the database', (done) => {
149149
let p = db.open(':memory:');
150150
p = p.then(() => db.migrate());
151151
p = p.then(() => db.all('SELECT id, name FROM migrations').then((result) => {
152-
expect(result).to.be.deep.equal([{ id: 1, name: 'initial' }, { id: 2, name: 'some-feature' }]);
152+
expect(result).to.be.deep.equal([{ id: 1, name: 'initial' }, { id: 2, name: 'some-feature' }, { id: 3, name: 'test-cert' }]);
153153
}));
154154
p = p.then(() => db.all('SELECT * FROM Category').then((result) => {
155155
expect(result).to.be.deep.equal([{ id: 1, name: 'Test' }]);
156156
}));
157+
158+
p = p.then(() => db.all('SELECT certificate from whatever').then((result) => {
159+
expect(result[0].certificate).to.be.equal('-----BEGIN CERTIFICATE-----\nsome contents\n-----END CERTIFICATE-----');
160+
}));
161+
157162
p = p.then(() => db.close());
158163
p.then(done, done);
159164
});

0 commit comments

Comments
 (0)