From ac62c4b2c0d051226d511e2a67fc197971b749c0 Mon Sep 17 00:00:00 2001 From: theogravity Date: Mon, 10 Apr 2017 21:16:06 -0700 Subject: [PATCH 1/3] Fix migration bug where using -- as an actual string value gets lost --- migrations/003-test-cert.sql | 9 +++++++++ src/Database.js | 6 ++++-- test/spec.js | 7 ++++++- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 migrations/003-test-cert.sql diff --git a/migrations/003-test-cert.sql b/migrations/003-test-cert.sql new file mode 100644 index 0000000..e424640 --- /dev/null +++ b/migrations/003-test-cert.sql @@ -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; diff --git a/src/Database.js b/src/Database.js index 64c8c47..2a91b82 100644 --- a/src/Database.js +++ b/src/Database.js @@ -167,8 +167,10 @@ 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(/^-- Up.*?$/gm, '').trim(); // Remove comments + migration.up = migration.up.replace(/^-+$/gm, '').trim(); + migration.down = down.replace(/^-- Down.*?$/gm, '').trim(); // and trim whitespaces + migration.down = migration.down.replace(/^-+$/gm, '').trim(); // and trim whitespaces /* eslint-enable no-param-reassign */ resolve(); } diff --git a/test/spec.js b/test/spec.js index bab2573..96c02a0 100644 --- a/test/spec.js +++ b/test/spec.js @@ -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); }); From a573bd6f7df786c5e53a95c5e7b6449babbd35cc Mon Sep 17 00:00:00 2001 From: theogravity Date: Tue, 18 Apr 2017 15:59:01 -0700 Subject: [PATCH 2/3] Simplify trim fixes --- src/Database.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Database.js b/src/Database.js index 2a91b82..38d2f21 100644 --- a/src/Database.js +++ b/src/Database.js @@ -169,8 +169,7 @@ class Database { /* eslint-disable no-param-reassign */ migration.up = up.replace(/^-- Up.*?$/gm, '').trim(); // Remove comments migration.up = migration.up.replace(/^-+$/gm, '').trim(); - migration.down = down.replace(/^-- Down.*?$/gm, '').trim(); // and trim whitespaces - migration.down = migration.down.replace(/^-+$/gm, '').trim(); // and trim whitespaces + migration.down = down.trim(); // and trim whitespaces /* eslint-enable no-param-reassign */ resolve(); } From 93b86e51a4d751839d0fac3a0f1864cb1c1c8992 Mon Sep 17 00:00:00 2001 From: theogravity Date: Tue, 25 Apr 2017 11:25:05 -0700 Subject: [PATCH 3/3] Simplify removal of comments --- src/Database.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Database.js b/src/Database.js index 38d2f21..3d7e5a4 100644 --- a/src/Database.js +++ b/src/Database.js @@ -167,8 +167,7 @@ class Database { reject(new Error(message)); } else { /* eslint-disable no-param-reassign */ - migration.up = up.replace(/^-- Up.*?$/gm, '').trim(); // Remove comments - migration.up = migration.up.replace(/^-+$/gm, '').trim(); + migration.up = up.replace(/^-- .*?$/gm, '').trim();// Remove comments migration.down = down.trim(); // and trim whitespaces /* eslint-enable no-param-reassign */ resolve();