From 365bc177a2e8152c096b7c3c11ecc11474a087a7 Mon Sep 17 00:00:00 2001 From: muhme Date: Mon, 16 Sep 2024 16:13:17 +0200 Subject: [PATCH 1/4] [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). --- tests/System/plugins/db.mjs | 46 ++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/tests/System/plugins/db.mjs b/tests/System/plugins/db.mjs index a92065c07e6a5..449a2ae26b433 100644 --- a/tests/System/plugins/db.mjs +++ b/tests/System/plugins/db.mjs @@ -36,9 +36,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 host_or_socket = config.env.db_host; + + // Check if db_host is a Unix socket by verifying the "unix:/" prefix. + // PostgreSQL does not handle this prefix, it needs to be stripped. + // This approach maintains compatibility with PHP driver and simplifies the overall configuration. + if (host_or_socket.startsWith("unix:/")) { + // e.g. 'unix:/var/run/postgresql' -> '/var/run/postgresql' + host_or_socket = host_or_socket.replace("unix:", ""); + } + // Initialisation on the first call postgresConnectionPool = postgres({ - host: config.env.db_host, + host: host_or_socket, port: config.env.db_port, database: config.env.db_name, username: config.env.db_user, @@ -78,13 +89,32 @@ 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; + // Check if db_host is a Unix socket by verifying the "unix:/" prefix. + // MariaDB and MySQL does not handle this prefix, it needs to be stripped. + // This approach maintains compatibility with PHP drivers and simplifies the overall configuration. + 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) => { From f95de5b9e1f885eba6fcad63b528096c54f7bf60 Mon Sep 17 00:00:00 2001 From: muhme Date: Tue, 17 Sep 2024 04:31:40 +0200 Subject: [PATCH 2/4] Fixing lint errors --- tests/System/plugins/db.mjs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/System/plugins/db.mjs b/tests/System/plugins/db.mjs index 449a2ae26b433..696e54d8e6461 100644 --- a/tests/System/plugins/db.mjs +++ b/tests/System/plugins/db.mjs @@ -36,20 +36,19 @@ function queryTestDB(joomlaQuery, config) { // Do we use PostgreSQL? if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') { if (postgresConnectionPool === null) { - - let host_or_socket = config.env.db_host; + let hostOrUnixPath = config.env.db_host; // Check if db_host is a Unix socket by verifying the "unix:/" prefix. // PostgreSQL does not handle this prefix, it needs to be stripped. // This approach maintains compatibility with PHP driver and simplifies the overall configuration. - if (host_or_socket.startsWith("unix:/")) { + if (hostOrUnixPath.startsWith('unix:/')) { // e.g. 'unix:/var/run/postgresql' -> '/var/run/postgresql' - host_or_socket = host_or_socket.replace("unix:", ""); + hostOrUnixPath = hostOrUnixPath.replace('unix:', ''); } // Initialisation on the first call postgresConnectionPool = postgres({ - host: host_or_socket, + host: hostOrUnixPath, port: config.env.db_port, database: config.env.db_name, username: config.env.db_user, @@ -93,11 +92,11 @@ function queryTestDB(joomlaQuery, config) { // Check if db_host is a Unix socket by verifying the "unix:/" prefix. // MariaDB and MySQL does not handle this prefix, it needs to be stripped. // This approach maintains compatibility with PHP drivers and simplifies the overall configuration. - if (config.env.db_host.startsWith("unix:/")) { + 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:", ""), + socketPath: config.env.db_host.replace('unix:', ''), user: config.env.db_user, password: config.env.db_password, database: config.env.db_name, From a417ea072939a6c412596b47e8432688320dcb53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Heiko=20L=C3=BCbbe?= Date: Thu, 24 Oct 2024 05:58:48 +0200 Subject: [PATCH 3/4] Rewrote a comment and used C-style formatting. --- tests/System/plugins/db.mjs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/System/plugins/db.mjs b/tests/System/plugins/db.mjs index 45bb13e92745a..d91494682304c 100644 --- a/tests/System/plugins/db.mjs +++ b/tests/System/plugins/db.mjs @@ -39,9 +39,10 @@ function queryTestDB(joomlaQuery, config) { if (postgresConnectionPool === null) { let hostOrUnixPath = config.env.db_host; - // Check if db_host is a Unix socket by verifying the "unix:/" prefix. - // PostgreSQL does not handle this prefix, it needs to be stripped. - // This approach maintains compatibility with PHP driver and simplifies the overall configuration. + /* Verify if the connection is a Unix socket by checking for the "unix:/" prefix. + * PostgreSQL does not support this prefix, so it must be removed. + * We standardise the use of this prefix across all databases and the PHP driver by handling it here. + */ if (hostOrUnixPath.startsWith('unix:/')) { // e.g. 'unix:/var/run/postgresql' -> '/var/run/postgresql' hostOrUnixPath = hostOrUnixPath.replace('unix:', ''); From 40f894ddda45e69d56d87a5ecbe7fdd0f11150cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Heiko=20L=C3=BCbbe?= Date: Thu, 24 Oct 2024 07:02:20 +0200 Subject: [PATCH 4/4] Rewrote comments and used C-style formatting. --- tests/System/plugins/db.mjs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/System/plugins/db.mjs b/tests/System/plugins/db.mjs index d91494682304c..6f1dcd5d56ad3 100644 --- a/tests/System/plugins/db.mjs +++ b/tests/System/plugins/db.mjs @@ -40,8 +40,8 @@ function queryTestDB(joomlaQuery, config) { let hostOrUnixPath = config.env.db_host; /* Verify if the connection is a Unix socket by checking for the "unix:/" prefix. - * PostgreSQL does not support this prefix, so it must be removed. - * We standardise the use of this prefix across all databases and the PHP driver by handling it here. + * 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' @@ -94,9 +94,10 @@ function queryTestDB(joomlaQuery, config) { return new Promise((resolve, reject) => { // Create the connection and connect let connectionConfig; - // Check if db_host is a Unix socket by verifying the "unix:/" prefix. - // MariaDB and MySQL does not handle this prefix, it needs to be stripped. - // This approach maintains compatibility with PHP drivers and simplifies the overall configuration. + /* 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 = {