Skip to content

Add context to yourls_get_db()#4020

Merged
ozh merged 15 commits intomasterfrom
db_context
Jan 7, 2026
Merged

Add context to yourls_get_db()#4020
ozh merged 15 commits intomasterfrom
db_context

Conversation

@ozh
Copy link
Copy Markdown
Member

@ozh ozh commented Nov 23, 2025

PR related to #3846

All yourls_get_db() calls in core are now passing an optional $context parameter, with the following naming convention:

/*
 * @param string $context Optional context. Default: ''.
 *   When provided, use a naming schema starting with a prefix describing the operation, followed
 *   by a short description
 *   - Prefix should be one of: "read-", "write-", or "other-".
 *        * "read-" for operations that only read from the DB (eg get_keyword_infos)
 *        * "write-" for operations that write to the DB (eg insert_link_in_db)
 *        * "other-" for operations that do not fit in the other categories because they are not SQL queries (eg
 *           when just storing info in memory, in the YDB object)
 *   - The description should be lowercase, words separated with underscores, eg "insert_link_in_db".
 *   Examples:
 *   - read-fetch_keyword
 *   - write-insert_link_in_db
 *   - write-update_url_title
 *   - read-get_options
 *   - other-get_debug_log

One practical use of such contextualized yourls_get_db() calls is to be able to use different DB connections in a Master/Slave setup. The following plugin should be a helping start : https://gist.github.com/ozh/553c2519878984271584939355e91b8e

@daynesuke Could you test this branch and the plugin for your use case ? Input would be much appreciated

@dgw dgw mentioned this pull request Dec 17, 2025
4 tasks
@dgw dgw self-assigned this Dec 17, 2025
Copy link
Copy Markdown
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

I admit I'm still not sold on this whole idea, especially since when to use other- seems entirely too ambiguous. Still, I've done my best to flag places that objectively don't make sense, such as use of read- context in a function that later performs a write operation.

@ozh
Copy link
Copy Markdown
Member Author

ozh commented Dec 29, 2025

All good points here @dgw , thanks a lot.

Suggestion for the read- / write- / other- situation : drop the other- and have just read- and write-. Other cases when it's not strictly a read or write statement can be finely handled by the slave DB if any.

ozh and others added 4 commits December 29, 2025 20:24
Co-authored-by: dgw <dgw@technobabbl.es>
Co-authored-by: dgw <dgw@technobabbl.es>
Co-authored-by: dgw <dgw@technobabbl.es>
Co-authored-by: dgw <dgw@technobabbl.es>
@dgw
Copy link
Copy Markdown
Member

dgw commented Dec 30, 2025

I guess that "slave DB" means read-? That's fine, as long as the other- it's replacing doesn't actually write to the DB. (Think I flagged some of those that you updated in the PR as-is, around plugin activation maybe?)

If the primary originator of $context parameters is us, i.e. core devs, we should be OK. :) I'll look for the patch that replaces other-s with one of the other two.

ozh added 2 commits December 30, 2025 23:18
- Replace 'other-' with 'read-' in contexts
- Validate context or raise debug message
- Add tests for context
- Add tests for all functions-debug.php
@ozh
Copy link
Copy Markdown
Member Author

ozh commented Dec 30, 2025

Calling yourls_get_db() with an improperly formatted context now adds a debug message in the log:

image

For consistency with previously existing DBSetTest file
Copy link
Copy Markdown
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

I renamed the new test file (DBTestGet -> DBGetTest) for consistency, but otherwise 🚀

Realistically, I should block out time to make the new release (#4034) at the weekend, which gives @LeoColomb a few days to weigh in too :)

@ozh
Copy link
Copy Markdown
Member Author

ozh commented Dec 31, 2025

Morning thought : maybe instead of message in the footer, we could raise a notice, like we do when someone uses a deprecated function. Notices show only if YOURLS_DEBUG is true so this change would not affect YOURLS instances in prod

@ozh
Copy link
Copy Markdown
Member Author

ozh commented Dec 31, 2025

What do we prefer ? When YOURLS_DEBUG is true,

Information message in footer :
image
(which should contain file and line num to be more useful)

or plain old PHP notice :
image

Pros and cons :

  • we, core devs, might forget to add a context to future yourls_get_db() calls with a footer message, not with a notice since PHPUnit screams
  • with a real notice, the context is "less optional", while it will really benefit to 0,01% of users when they'll use a master/slave setup (there might be another future use for this context, let's not rule this out completely)

@dgw
Copy link
Copy Markdown
Member

dgw commented Jan 1, 2026

100% go with the approach that has the best chance of making PHPUnit complain, imo

I believe display_errors is often set to Off by default since it can expose webserver stuff, so it might make sense to emit both a notice and a footer message. 🤷‍♂️

@ozh
Copy link
Copy Markdown
Member Author

ozh commented Jan 1, 2026

Missing or malformed contexts now trigger a notice. No message in the footer for consistency with other deprecation messages in YOURLS

@LeoColomb
Copy link
Copy Markdown
Member

Principle sounds all good to me.
One question, though: in case the context is missing, is it well assumed the request is write as a fallback?
For existing plugins doing database stuff, that would help avoid split issues.

@LeoColomb LeoColomb changed the title Add context to yourls_get_db() Add context to yourls_get_db() Jan 7, 2026
@ozh
Copy link
Copy Markdown
Member Author

ozh commented Jan 7, 2026

If the context is omitted, a notice is thrown, but nothing else happens.

As I see it, someone making a plugin for a master/slave setup should make it so that, by default, unspecified queries are sent to the write connection, in order to deal with existing code using yourls_get_db()->do_something() or, very common, $ydb->do_something().

Maybe I should stress this out in my sample plugin

Copy link
Copy Markdown
Member

@LeoColomb LeoColomb left a comment

Choose a reason for hiding this comment

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

Understood. I think I would have preferred an internal fallback, but all good for me both ways.

@ozh ozh merged commit 1c571eb into master Jan 7, 2026
8 checks passed
@ozh ozh deleted the db_context branch January 7, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants