Skip to content

Commit

Permalink
[cypress] Database Unix Sockets for System Tests (#44092)
Browse files Browse the repository at this point in the history
* [cypress] Enable database usage with Unix Sockets

Check if db_host is a Unix socket by verifying the "unix:/" prefix.
JavaScript drivers does not handle this prefix, it needs to be stripped.
This approach maintains compatibility with the PHP drivers and allows
to have Cypress.config.mjs working for installation step (PHP driver)
and custom database commands (JavaScript driver).

* Fixing lint errors

* Rewrote a comment and used C-style formatting.

* Rewrote comments and used C-style formatting.
  • Loading branch information
muhme authored Oct 24, 2024
1 parent 2e4cdc1 commit 3b358b8
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions tests/System/plugins/db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,20 @@ function queryTestDB(joomlaQuery, config) {
// Do we use PostgreSQL?
if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') {
if (postgresConnectionPool === null) {
let hostOrUnixPath = config.env.db_host;

/* Verify if the connection is a Unix socket by checking for the "unix:/" prefix.
* PostgreSQL JS driver does not support this prefix, so it must be removed.
* We standardise the use of this prefix with the PHP driver by handling it here.
*/
if (hostOrUnixPath.startsWith('unix:/')) {
// e.g. 'unix:/var/run/postgresql' -> '/var/run/postgresql'
hostOrUnixPath = hostOrUnixPath.replace('unix:', '');
}

// Initialisation on the first call
postgresConnectionPool = new Pool({
host: config.env.db_host,
host: hostOrUnixPath,
port: config.env.db_port,
database: config.env.db_name,
user: config.env.db_user,
Expand Down Expand Up @@ -82,13 +93,33 @@ function queryTestDB(joomlaQuery, config) {
// Return a promise which runs the query for MariaDB / MySQL
return new Promise((resolve, reject) => {
// Create the connection and connect
const connection = mysql.createConnection({
host: config.env.db_host,
port: config.env.db_port,
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name,
});
let connectionConfig;
/* Verify if the connection is a Unix socket by checking for the "unix:/" prefix.
* MariaDB and MySQL JS drivers do not support this prefix, so it must be removed.
* We standardise the use of this prefix with the PHP driver by handling it here.
*/
if (config.env.db_host.startsWith('unix:/')) {
// If the host is a Unix socket, extract the socket path
connectionConfig = {
// e.g. 'unix:/var/run/mysqld/mysqld.sock' -> '/var/run/mysqld/mysqld.sock'
socketPath: config.env.db_host.replace('unix:', ''),
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name,
};
} else {
// Otherwise, use regular TCP host connection settings
connectionConfig = {
host: config.env.db_host,
port: config.env.db_port,
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name,
};
}

// Create the MySQL/MariaDB connection
const connection = mysql.createConnection(connectionConfig);

// Perform the query
connection.query(query, (error, results) => {
Expand Down

0 comments on commit 3b358b8

Please # to comment.