From caf4a2341f554b28e3918c53e7e897a3ca47bf8b Mon Sep 17 00:00:00 2001 From: Corey Date: Sun, 2 Jan 2022 09:25:43 -0500 Subject: [PATCH] feat: support `postgresql` protocol in database URI (#7757) --- spec/PostgresInitOptions.spec.js | 92 ++++++++++++----------------- spec/PostgresStorageAdapter.spec.js | 11 +++- src/Controllers/index.js | 1 + 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/spec/PostgresInitOptions.spec.js b/spec/PostgresInitOptions.spec.js index 069d5a6437..760240734d 100644 --- a/spec/PostgresInitOptions.spec.js +++ b/spec/PostgresInitOptions.spec.js @@ -4,8 +4,7 @@ const PostgresStorageAdapter = require('../lib/Adapters/Storage/Postgres/Postgre const postgresURI = process.env.PARSE_SERVER_TEST_DATABASE_URI || 'postgres://localhost:5432/parse_server_postgres_adapter_test_database'; -const ParseServer = require('../lib/index'); -const express = require('express'); + //public schema const databaseOptions1 = { initOptions: { @@ -24,72 +23,57 @@ const GameScore = Parse.Object.extend({ className: 'GameScore', }); -function createParseServer(options) { - return new Promise((resolve, reject) => { - const parseServer = new ParseServer.default( - Object.assign({}, defaultConfiguration, options, { - serverURL: 'http://localhost:12668/parse', - serverStartComplete: error => { - if (error) { - reject(error); - } else { - expect(Parse.applicationId).toEqual('test'); - const app = express(); - app.use('/parse', parseServer.app); - - const server = app.listen(12668); - Parse.serverURL = 'http://localhost:12668/parse'; - resolve(server); - } - }, - }) - ); - }); -} - describe_only_db('postgres')('Postgres database init options', () => { - let server; - - afterAll(done => { - if (server) { - Parse.serverURL = 'http://localhost:8378/1'; - server.close(done); - } - }); - it('should create server with public schema databaseOptions', done => { + it('should create server with public schema databaseOptions', async () => { const adapter = new PostgresStorageAdapter({ uri: postgresURI, collectionPrefix: 'test_', databaseOptions: databaseOptions1, }); + await reconfigureServer({ + databaseAdapter: adapter, + }); + const score = new GameScore({ + score: 1337, + playerName: 'Sean Plott', + cheatMode: false, + }); + await score.save(); + }); - createParseServer({ databaseAdapter: adapter }) - .then(newServer => { - server = newServer; - const score = new GameScore({ - score: 1337, - playerName: 'Sean Plott', - cheatMode: false, - }); - return score.save(); - }) - .then(async () => { - await reconfigureServer(); - done(); - }, done.fail); + it('should create server using postgresql uri with public schema databaseOptions', async () => { + const postgresURI2 = new URL(postgresURI); + postgresURI2.protocol = 'postgresql:'; + const adapter = new PostgresStorageAdapter({ + uri: postgresURI2.toString(), + collectionPrefix: 'test_', + databaseOptions: databaseOptions1, + }); + await reconfigureServer({ + databaseAdapter: adapter, + }); + const score = new GameScore({ + score: 1337, + playerName: 'Sean Plott', + cheatMode: false, + }); + await score.save(); }); - it('should fail to create server if schema databaseOptions does not exist', done => { + it('should fail to create server if schema databaseOptions does not exist', async () => { const adapter = new PostgresStorageAdapter({ uri: postgresURI, collectionPrefix: 'test_', databaseOptions: databaseOptions2, }); - - createParseServer({ databaseAdapter: adapter }).then(done.fail, async () => { - await reconfigureServer(); - done(); - }); + try { + await reconfigureServer({ + databaseAdapter: adapter, + }); + fail("Should have thrown error"); + } catch(error) { + expect(error).toBeDefined(); + } }); }); diff --git a/spec/PostgresStorageAdapter.spec.js b/spec/PostgresStorageAdapter.spec.js index dfb6bf4100..769aad74c5 100644 --- a/spec/PostgresStorageAdapter.spec.js +++ b/spec/PostgresStorageAdapter.spec.js @@ -555,7 +555,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => { }, classLevelPermissions: undefined, }); - await new Promise(resolve => setTimeout(resolve, 500)); + await new Promise(resolve => setTimeout(resolve, 2000)); expect(adapter._onchange).toHaveBeenCalled(); }); }); @@ -567,4 +567,13 @@ describe_only_db('postgres')('PostgresStorageAdapter shutdown', () => { adapter.handleShutdown(); expect(adapter._client.$pool.ending).toEqual(true); }); + + it('handleShutdown, close connection of postgresql uri', () => { + const databaseURI2 = new URL(databaseURI); + databaseURI2.protocol = 'postgresql:'; + const adapter = new PostgresStorageAdapter({ uri: databaseURI2.toString() }); + expect(adapter._client.$pool.ending).toEqual(false); + adapter.handleShutdown(); + expect(adapter._client.$pool.ending).toEqual(true); + }); }); diff --git a/src/Controllers/index.js b/src/Controllers/index.js index 89dc79c232..67f90a8edf 100644 --- a/src/Controllers/index.js +++ b/src/Controllers/index.js @@ -227,6 +227,7 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption } switch (protocol) { case 'postgres:': + case 'postgresql:': return new PostgresStorageAdapter({ uri: databaseURI, collectionPrefix,