From 4a67a59827e02cfefceb25cb532606a5d2246d19 Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 9 Apr 2020 13:26:57 -0700 Subject: [PATCH 1/5] made ensureSchema a callback for createPool --- cloud-sql/mysql/mysql/server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloud-sql/mysql/mysql/server.js b/cloud-sql/mysql/mysql/server.js index a7b9485ff0..5df1e4a4eb 100644 --- a/cloud-sql/mysql/mysql/server.js +++ b/cloud-sql/mysql/mysql/server.js @@ -86,7 +86,6 @@ const createPool = async () => { //[END_EXCLUDE] }); }; -createPool(); // [END cloud_sql_mysql_mysql_create] const ensureSchema = async () => { @@ -97,7 +96,8 @@ const ensureSchema = async () => { candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` ); }; -ensureSchema(); + +createPool().then(ensureSchema).catch((error)=>(console.log(error))); // Serve the index page, showing vote tallies. app.get('/', async (req, res) => { From 22cd870f9de0d0fdc4e141d403b8e7408466eaf5 Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 9 Apr 2020 14:36:56 -0700 Subject: [PATCH 2/5] fixed unhandled promise rejection in ensureSchema --- cloud-sql/mysql/mysql/server.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cloud-sql/mysql/mysql/server.js b/cloud-sql/mysql/mysql/server.js index 5df1e4a4eb..f21834ec2b 100644 --- a/cloud-sql/mysql/mysql/server.js +++ b/cloud-sql/mysql/mysql/server.js @@ -88,13 +88,16 @@ const createPool = async () => { }; // [END cloud_sql_mysql_mysql_create] -const ensureSchema = async () => { +const ensureSchema = () => { // Wait for tables to be created (if they don't already exist). - await pool.query( + pool.query( `CREATE TABLE IF NOT EXISTS votes ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` - ); + ) + .then(() => console.log(`Ensured that table 'votes' exists`)) + .catch((e) =>`Got error: ${e}` + ); }; createPool().then(ensureSchema).catch((error)=>(console.log(error))); From 9d4bd3f92f0bbd82f00ffe9faa994bd708b1736f Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 9 Apr 2020 14:56:27 -0700 Subject: [PATCH 3/5] stubbed table creation query in test --- cloud-sql/mysql/mysql/test/server.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cloud-sql/mysql/mysql/test/server.test.js b/cloud-sql/mysql/mysql/test/server.test.js index 23a9b6615f..11e3493980 100644 --- a/cloud-sql/mysql/mysql/test/server.test.js +++ b/cloud-sql/mysql/mysql/test/server.test.js @@ -22,6 +22,11 @@ const assert = require('assert'); const stubMysql = sinon.stub(require('promise-mysql')); const poolStub = sinon.stub(); const queryStub = sinon.stub(); +queryStub.withArgs(sinon.match( + `CREATE TABLE IF NOT EXISTS votes + ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, + candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` +)).resolves([]); queryStub.withArgs(sinon.match('SELECT COUNT(vote_id)')).resolves([{count: 1}]); queryStub.withArgs(sinon.match('SELECT candidate, time_cast')).resolves([]); poolStub['query'] = queryStub; From cea603b45768ef1671715bec62a5436dadf93204 Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 9 Apr 2020 15:47:09 -0700 Subject: [PATCH 4/5] refactor to use async/await instead of then in ensureSchema --- cloud-sql/mysql/mysql/server.js | 31 ++++++++++++++++------- cloud-sql/mysql/mysql/test/server.test.js | 5 ---- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/cloud-sql/mysql/mysql/server.js b/cloud-sql/mysql/mysql/server.js index f21834ec2b..4e00eb3f75 100644 --- a/cloud-sql/mysql/mysql/server.js +++ b/cloud-sql/mysql/mysql/server.js @@ -88,19 +88,32 @@ const createPool = async () => { }; // [END cloud_sql_mysql_mysql_create] -const ensureSchema = () => { +const ensureSchema = async () => { // Wait for tables to be created (if they don't already exist). - pool.query( - `CREATE TABLE IF NOT EXISTS votes - ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, - candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` - ) - .then(() => console.log(`Ensured that table 'votes' exists`)) - .catch((e) =>`Got error: ${e}` + try { + await pool.query( + `CREATE TABLE IF NOT EXISTS votes + ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, + candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` ); + console.log(`Ensured that table 'votes' exists`); + } catch (e) { + console.log(`Got error: ${e}`); + } +}; + +let schemaReady; + +createPool() + .then(() => (schemaReady = ensureSchema())) + .catch((error) => console.log(error)); + +const awaitSchema = async (req, res, next) => { + await schemaReady; + next(); }; -createPool().then(ensureSchema).catch((error)=>(console.log(error))); +app.use(awaitSchema); // Serve the index page, showing vote tallies. app.get('/', async (req, res) => { diff --git a/cloud-sql/mysql/mysql/test/server.test.js b/cloud-sql/mysql/mysql/test/server.test.js index 11e3493980..23a9b6615f 100644 --- a/cloud-sql/mysql/mysql/test/server.test.js +++ b/cloud-sql/mysql/mysql/test/server.test.js @@ -22,11 +22,6 @@ const assert = require('assert'); const stubMysql = sinon.stub(require('promise-mysql')); const poolStub = sinon.stub(); const queryStub = sinon.stub(); -queryStub.withArgs(sinon.match( - `CREATE TABLE IF NOT EXISTS votes - ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, - candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` -)).resolves([]); queryStub.withArgs(sinon.match('SELECT COUNT(vote_id)')).resolves([{count: 1}]); queryStub.withArgs(sinon.match('SELECT candidate, time_cast')).resolves([]); poolStub['query'] = queryStub; From a8b7e213c5f8f5b836d0f2da91e8c553d7ce0b8f Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Wed, 15 Apr 2020 14:42:50 -0700 Subject: [PATCH 5/5] incorporate PR feedback --- cloud-sql/mysql/mysql/server.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/cloud-sql/mysql/mysql/server.js b/cloud-sql/mysql/mysql/server.js index 4e00eb3f75..c4b7f9d152 100644 --- a/cloud-sql/mysql/mysql/server.js +++ b/cloud-sql/mysql/mysql/server.js @@ -90,23 +90,17 @@ const createPool = async () => { const ensureSchema = async () => { // Wait for tables to be created (if they don't already exist). - try { - await pool.query( - `CREATE TABLE IF NOT EXISTS votes - ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, - candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` - ); - console.log(`Ensured that table 'votes' exists`); - } catch (e) { - console.log(`Got error: ${e}`); - } + await pool.query( + `CREATE TABLE IF NOT EXISTS votes + ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, + candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );` + ); + console.log(`Ensured that table 'votes' exists`); }; let schemaReady; -createPool() - .then(() => (schemaReady = ensureSchema())) - .catch((error) => console.log(error)); +createPool().then(() => (schemaReady = ensureSchema())); const awaitSchema = async (req, res, next) => { await schemaReady;