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

(occ db:convert-type) Add support for UNIX sockets #39242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joshtrichards
Copy link
Member

Adds support to occ db:convert-type to support UNIX socket connections via MySQL/MariaDB:

Summary

TODO

  • ...

Checklist

@solracsf solracsf added the 3. to review Waiting for reviews label Jul 9, 2023
@solracsf solracsf added this to the Nextcloud 28 milestone Jul 9, 2023
@@ -254,6 +257,16 @@
if ($input->getOption('port')) {
$connectionParams['port'] = $input->getOption('port');
}
if (strpos($input->getOption('hostname'), ':') !== false) {
// Host variable may carry a socket (or port)
[$host, $portOrSocket] = explode(':', $input->getOption('hostname'), 2);

Check notice

Code scanning / Psalm

PossiblyUndefinedArrayOffset Note

Possibly undefined array key
@szaimen szaimen requested review from a team, ArtificialOwl, icewind1991 and Fenn-CS and removed request for a team July 13, 2023 16:16
Comment on lines +260 to +269
if (strpos($input->getOption('hostname'), ':') !== false) {
// Host variable may carry a socket (or port)
[$host, $portOrSocket] = explode(':', $input->getOption('hostname'), 2);
if (ctype_digit($portOrSocket)) {
$connectionParams['port'] = $portOrSocket; // to be consistent with how we handle this elsewhere we accept this too but don't document it
} else {
$connectionParams['unix_socket'] = $portOrSocket;
}
$connectionParams['host'] = $host;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (strpos($input->getOption('hostname'), ':') !== false) {
// Host variable may carry a socket (or port)
[$host, $portOrSocket] = explode(':', $input->getOption('hostname'), 2);
if (ctype_digit($portOrSocket)) {
$connectionParams['port'] = $portOrSocket; // to be consistent with how we handle this elsewhere we accept this too but don't document it
} else {
$connectionParams['unix_socket'] = $portOrSocket;
}
$connectionParams['host'] = $host;
}
if (preg_match('/^(.+)(:(\d+|[^:]+))?$/',$input->getOption('hostname'), $matches)) {
$connectionParams['host'] = $matches[1];
if (isset($matches[3])) {
if (is_numeric($matches[3])) {
$connectionParams['port'] = $matches[3];
} else {
$connectionParams['unix_socket'] = $matches[3];
}
}
}

Suggestion :

Using regex is most likely better than explode. /^(.+):(\d+|[^:]+)$/ matches a string that starts with one or more characters ((.+)) followed by a colon (:) and then either a numeric value (\d+) or one or more non-colon characters ([^:]+) until the end of the string ($).

The regular expression approach allows you to extract the host name and port number or socket path in a single step, which is more concise than using explode() and ctype_digit(). Additionally, it allows for more flexibility in the format of the option value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not copy the comments but they are relevant.

Copy link
Contributor

@Fenn-CS Fenn-CS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay though I left an inline suggestion. Do you mind using regex instead?

Fixes nextcloud#31998

Adds support to `occ db:convert-type` to support UNIX socket connections via MySQL/MariaDB. Uses same `dbhost` / `hostname` parameter parsing logic (adapted) as used elsewhere (at least the relevant parts) for consistency.

Signed-off-by: Josh Richards <josh.t.richards@gmail.com>
@AndyScherzinger AndyScherzinger force-pushed the db-convert-type-mysql-socket-no-pw branch from 864c17c to 1ac39e7 Compare February 27, 2024 13:25
This was referenced Mar 12, 2024
This was referenced Mar 20, 2024
@skjnldsv skjnldsv mentioned this pull request Mar 28, 2024
81 tasks
@skjnldsv skjnldsv modified the milestones: Nextcloud 29, Nextcloud 30 Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review Waiting for reviews enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants