wp-cli / db-command

Performs basic database operations using credentials stored in wp-config.php.
MIT License
71 stars 59 forks source link

size command doesn't work with SQLite #243

Open mrsdizzie opened 1 year ago

mrsdizzie commented 1 year ago

The following only works with MySQL

https://github.com/wp-cli/db-command/blob/0908bf5182b830c302199037070292e20d9f4ea6/src/DB_Command.php#L1019-L1024

I don't believe there is a similar way to get the size of a SQLite database, and the preferred method is to just get the size of the .sqlite file itself.

I wonder what the general solution should be to detect SQLite use in wp-cli commands so any new code is consistent across various projects? It could be good to have a helper function to give the current database type and maybe also the location of the .sqlite file if it is SQLite.

Or is this just better left until WordPress adopts SQLite officially and adds a built in method for wbdp?

danielbachhuber commented 1 year ago

Or is this just better left until WordPress adopts SQLite officially and adds a built in method for wbdp?

I don't think it's likely that WordPress core will add exactly what we need. wp db size is something we invented for our own use.

I wonder what the general solution should be to detect SQLite use in wp-cli commands so any new code is consistent across various projects? It could be good to have a helper function to give the current database type and maybe also the location of the .sqlite file if it is SQLite.

I'm open to it! I'd suggest we fix SQLite support for a couple/few commands first, and then figure out what abstraction they need.

I don't believe there is a similar way to get the size of a SQLite database, and the preferred method is to just get the size of the .sqlite file itself.

Worth noting: wp db size also supports a --tables argument:

$ wp db size --tables
+-----------------------+-----------+
| Name                  | Size      |
+-----------------------+-----------+
| wp_commentmeta        | 49152 B   |
| wp_comments           | 98304 B   |
| wp_links              | 32768 B   |
| wp_options            | 1163264 B |
| wp_postmeta           | 49152 B   |
| wp_posts              | 81920 B   |
| wp_term_relationships | 32768 B   |
| wp_term_taxonomy      | 49152 B   |
| wp_termmeta           | 49152 B   |
| wp_terms              | 49152 B   |
| wp_usermeta           | 49152 B   |
| wp_users              | 65536 B   |
+-----------------------+-----------+

Here's what I see with SQLite:

$ wp db size
+--------+------------------+
| Name   | Size             |
+--------+------------------+
| wplite | WP_COMMENTMETA B |
+--------+------------------+
$ wp db size --tables
+-------------------------+------------------+
| Name                    | Size             |
+-------------------------+------------------+
| _mysql_data_types_cache | WP_COMMENTMETA B |
| sqlite_sequence         | WP_COMMENTMETA B |
| wp_commentmeta          | WP_COMMENTMETA B |
| wp_comments             | WP_COMMENTMETA B |
| wp_links                | WP_COMMENTMETA B |
| wp_options              | WP_COMMENTMETA B |
| wp_postmeta             | WP_COMMENTMETA B |
| wp_posts                | WP_COMMENTMETA B |
| wp_term_relationships   | WP_COMMENTMETA B |
| wp_term_taxonomy        | WP_COMMENTMETA B |
| wp_termmeta             | WP_COMMENTMETA B |
| wp_terms                | WP_COMMENTMETA B |
| wp_usermeta             | WP_COMMENTMETA B |
| wp_users                | WP_COMMENTMETA B |
+-------------------------+------------------+