Skip to content

Commit 3b358b8

Browse files
authored
[cypress] Database Unix Sockets for System Tests (joomla#44092)
* [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.
1 parent 2e4cdc1 commit 3b358b8

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

tests/System/plugins/db.mjs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,20 @@ function queryTestDB(joomlaQuery, config) {
3737
// Do we use PostgreSQL?
3838
if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') {
3939
if (postgresConnectionPool === null) {
40+
let hostOrUnixPath = config.env.db_host;
41+
42+
/* Verify if the connection is a Unix socket by checking for the "unix:/" prefix.
43+
* PostgreSQL JS driver does not support this prefix, so it must be removed.
44+
* We standardise the use of this prefix with the PHP driver by handling it here.
45+
*/
46+
if (hostOrUnixPath.startsWith('unix:/')) {
47+
// e.g. 'unix:/var/run/postgresql' -> '/var/run/postgresql'
48+
hostOrUnixPath = hostOrUnixPath.replace('unix:', '');
49+
}
50+
4051
// Initialisation on the first call
4152
postgresConnectionPool = new Pool({
42-
host: config.env.db_host,
53+
host: hostOrUnixPath,
4354
port: config.env.db_port,
4455
database: config.env.db_name,
4556
user: config.env.db_user,
@@ -82,13 +93,33 @@ function queryTestDB(joomlaQuery, config) {
8293
// Return a promise which runs the query for MariaDB / MySQL
8394
return new Promise((resolve, reject) => {
8495
// Create the connection and connect
85-
const connection = mysql.createConnection({
86-
host: config.env.db_host,
87-
port: config.env.db_port,
88-
user: config.env.db_user,
89-
password: config.env.db_password,
90-
database: config.env.db_name,
91-
});
96+
let connectionConfig;
97+
/* Verify if the connection is a Unix socket by checking for the "unix:/" prefix.
98+
* MariaDB and MySQL JS drivers do not support this prefix, so it must be removed.
99+
* We standardise the use of this prefix with the PHP driver by handling it here.
100+
*/
101+
if (config.env.db_host.startsWith('unix:/')) {
102+
// If the host is a Unix socket, extract the socket path
103+
connectionConfig = {
104+
// e.g. 'unix:/var/run/mysqld/mysqld.sock' -> '/var/run/mysqld/mysqld.sock'
105+
socketPath: config.env.db_host.replace('unix:', ''),
106+
user: config.env.db_user,
107+
password: config.env.db_password,
108+
database: config.env.db_name,
109+
};
110+
} else {
111+
// Otherwise, use regular TCP host connection settings
112+
connectionConfig = {
113+
host: config.env.db_host,
114+
port: config.env.db_port,
115+
user: config.env.db_user,
116+
password: config.env.db_password,
117+
database: config.env.db_name,
118+
};
119+
}
120+
121+
// Create the MySQL/MariaDB connection
122+
const connection = mysql.createConnection(connectionConfig);
92123

93124
// Perform the query
94125
connection.query(query, (error, results) => {

0 commit comments

Comments
 (0)