Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cypress] PostgreSQL Connection pool for System Tests #43924

Merged
merged 12 commits into from
Aug 29, 2024
35 changes: 21 additions & 14 deletions tests/System/plugins/db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import postgres from 'postgres';
// Items cache which are added by an insert statement
let insertedItems = [];

// Use of the PostgreSQL connection pool to limit the number of sessions, see
// https://github.com/porsager/postgres?tab=readme-ov-file#connection-details
let postgresConnectionPool = null;

/**
* Does run the given query against the database from the configuration. It caches all inserted items.
*
Expand All @@ -29,17 +33,20 @@ function queryTestDB(joomlaQuery, config) {
insertedItems.push(insertItem);
}

// Check if the DB is from postgres
// Do we use PostgreSQL?
if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') {
const connection = postgres({
host: config.env.db_host,
port: config.env.db_port,
database: config.env.db_name,
username: config.env.db_user,
password: config.env.db_password,
idle_timeout: 1,
max_lifetime: 1,
});

if (postgresConnectionPool === null) {
// Initialisation on the first call
postgresConnectionPool = postgres({
host: config.env.db_host,
port: config.env.db_port,
database: config.env.db_name,
username: config.env.db_user,
password: config.env.db_password,
max: 10, // Use only this (unchanged default) maximum number of connections in the pool
});
}

// Postgres delivers the data direct as result of the insert query
if (insertItem) {
Expand All @@ -49,7 +56,7 @@ function queryTestDB(joomlaQuery, config) {
// Postgres needs double quotes
query = query.replaceAll('`', '"');

return connection.unsafe(query).then((result) => {
return postgresConnectionPool.unsafe(query).then((result) => {
// Select query should always return an array
if (query.indexOf('SELECT') === 0 && !Array.isArray(result)) {
return [result];
Expand All @@ -64,12 +71,12 @@ function queryTestDB(joomlaQuery, config) {
insertItem.rows.push(result[0].id);
}

// Normalize the object
// Normalize the object and return from PostgreSQL
return { insertId: result[0].id };
});
}

// Return a promise which runs the query
// Return a promise which runs the query for MariaDB / MySQL
return new Promise((resolve, reject) => {
// Create the connection and connect
const connection = mysql.createConnection({
Expand All @@ -94,7 +101,7 @@ function queryTestDB(joomlaQuery, config) {
insertItem.rows.push(results.insertId);
}

// Resolve the result
// Resolve the result from MariaDB / MySQL
return resolve(results);
});
});
Expand Down