-
Notifications
You must be signed in to change notification settings - Fork 29
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
Seems to be installing the table without ->install()
#121
Comments
->install()
->install()
Hey @rmorse! Okay, so I did a little digging to figure out what's going on here. I knew that BerlinDB does install tables somewhere automatically but couldn't for the life of me remember if that was something that was actually in core, or if that's something that I had just built into Underpin. Turns out, it is in core: Line 912 in 0a9d65e
So As you can see above, the table will automatically when someone visits an admin page, and it will only install if the tables haven't already been set-up. Generally, this happens automatically as soon as the plugin is activated. So, technically, this bit of code isn't necessary: // If the table does not exist, then create the table.
if ( ! $table->exists() ) {
$table->install();
} However, I do think it helps to rebuild the table when the uninstall snippet is added above. Perhaps the comment above the |
Hey @alexstandiford ! Thanks for the reply - yeah I had started digging though the code to see what it does but didn't find the trigger. As long I know how it works I think I'm happy with this implemenation - I will probably try to ensure that the tables can only be created in admin, and I think the solution for that is to simply only include this:
in some admin facing part of the plugin. ** edit, thats totally not necessary because you're doing that already via Re the From what you've told me and what I can see my understanding is:
I'll do some more testing, but if my assumptions are correct, if you use I'll be working on DB stuff again probably tomorrow and will run these scenarios though (and share my findings). Thanks |
After thought - if I don't instantiate the class
until after |
Hey @alexstandiford So I did a bit more digging and I think my assumptions above are correct:
$table = new Books_Table; // This will create the table
$table->uninstall(); // Removes the table (and version number) again So I think the takeaway is, if you are instantiating the class before Is that how you see it all working? Also: Would be happy to help where I can (just point me in the right direction) and now I've realised this ticket should have been in An extra thought - would it be worth implementing an opt in / out of this behaviour when instantiating the class? |
No worries - I went ahead and transferred this issue to core! 😎 Yep, what you're saying matches my expectations. I usually use I'm kinda iffy on creating some kind-of opt-in/out behavior, but if anything I could see a private property being added to the Table class that can toggle versioning for a table. |
My original intention for the database tables being auto-managed was only to make installing and upgrading them as easy as possible. I did not care much about the idea of deleting them (in the same way we don't know if Apple Notes or Calendar delete their SQLite tables in our iPhones and iPads.) That is to say: I can imagine how uninstalling tables could be less smooth, because I hadn't designed it to be elegant 😅 My gut tells me this might be an implementation detail best suited for the application using Berlin to address (by overriding the If I was going to design a solution for a persistent uninstall, the Table class would need a way to know not to automatically reinstall the database tables on the subsequent include, and that way probably needs to be identical to how it knows what upgrades are pending – an option in the database. Perhaps Berlin could use a unique value in the Once that option is in the database, how will a Table class ever know that it is reinstallable? There isn't a way with 100% certainty to execute code when a plugin is deleted/purged from the file system; all it's got is when someone uninstalls or deletes a plugin using the admin-area UI. Is that enough? When would Berlin ever outright delete the options (indicating reinstalling is OK) vs. setting the option values to Or... would it be enough to simply not immediately trigger a reinstall on the subsequent request? The $this->set_db_version( 'uninstalling' ); Add an /**
* Return whether this table is being uninstalled.
*
* @since 2.1.0
*
* @return bool True if table is being uninstalled. False if not.
*/
public function is_uninstalling() {
// Get the current database version
$this->get_db_version();
// Is the database table being uninstalled?
$is_uninstalling = ( 'uninstalling' === $this->db_version );
// Return true if uninstalling, false if not
return (bool) $is_uninstalling;
} And the // Bail if uninstalling
if ( $this->is_uninstalling() ) {
$this->delete_db_version();
return;
} It's not much, but at least that gives plugin authors some window? |
I kind of think you could create a protected property |
Hey @JJJ + @alexstandiford , thanks so much for the feedback and details. My ticket really didn't start as a request to change anything, rather, understand in absolute terms what is occuring and why... I think it all stems from the - not knowing what is being automated - and therefor a desire to have absolute control over the inner workings... but I think overriding the default class would be a fine way to do it. I think even making this clear in docs somehow would have prevented me from opening this ticket altogether., That all being said, in the name of rounding off all areas of the library, having a look at the uninstall + *automation scenarios couldn't hurt, but in terms of coming up with implementation ideas, I think they are best left in both your capable hands! :) I'll mull this over anyway as I'm using this on a plugin I'm working on and no doubt will come up with something (...but perhaps nothing) over the next couple of weeks. Thanks for talking this out! |
Hi Alex + co!
Checking the sample plugin I see code like:
However, running just:
Seems to create the table anyway...
I had planned to put the install logic on plugin activation (assuming that's the best place for something like this).
Also, I did notice, when I delete the
wp_options
option, with the table version number (that is also auto created), this behaviour did not re-occur. I guess maybe there is check for an existing version number which triggers this behaviour.Thanks!
The text was updated successfully, but these errors were encountered: