Find the size of a MySQL database from CLI

Need: To run a single command in the MySQL console / CLI to show the size of a paricular database.

Solution: This command, when logged in with a user that has permssions to read the information_schema table, substituting “the_database” with the name of the db you want to check (include the double quotes!)

SELECT table_schema "the_database", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MiB" FROM information_schema.TABLES GROUP BY table_schema;

Example result: 

mysql> SELECT table_schema "db", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MiB" FROM information_schema.TABLES GROUP BY table_schema;
+--------------------+-----------------------+
| db                 | Data Base Size in MiB |
+--------------------+-----------------------+
| db                 |            5.53125000 |
| information_schema |            0.18750000 |
+--------------------+-----------------------+
2 rows in set (0.01 sec)

Please let me know if this was useful? Nope, not helpful...Yep, more useful than not! (No Ratings Yet)
Loading...

Test a Composer package from a local version / copy

Problem: I need to test a small change to a Composer package that’s on GitHub / Packagist, but I don’t want (yet) to request the change from the maintainer or fork my own version on Packagist or whatever… I literally just want to change one digit in a version number requirement.

Not that it’s particularly relevant, but the Package in question is a Volume storage driver for Craft CMS that makes use of Fortrabbit’s Object Storage.

Solution: Composer has a repositories feature which allows us to add Repositories other than the default Packagist.org one by adding a new block to the project’s composer.json. There are quite a lot of different sorts of repositories that can be setup, but I’m interested in the path one. This option will allow me to stick a package’s contents almost anywhere on my local machine, and path to it. Example: Continue reading “Test a Composer package from a local version / copy”

stream_socket_client(): unable to connect to smtp.mailtrap.io:2525 (Permission denied) – CentOS 7 / Mailtrap / Laravel

Problem:
After rebuilding a CentOS 7 system with a Laravel app on it, every time the app attempted to mail a notification using MailTrap, the above error got fired.

Solution:
SELinux. Again. It’s always flipping SE LINUX! The SELinux policy can block in and outbound ports and all sorts of things. It’s a git. A clever useful git.

Running `sudo sealert -a /var/log/audit/audit.log` showed the problem. If sealert isn’t possible, then tailing the audit log with `sudo tail -f /var/log/audit/audit.log` goes some way to finding the problem.

In this case a solution (being Linux there are probably 74 equally effective and largely contested solutions) was to allow Apache to use port 2525 like so:

sudo semanage port -a -t http_port_t -p tcp 2525

Any good? If this helped please let me know: Nope, not helpful...Yep, more useful than not! (No Ratings Yet)
Loading...

Fixing ReflectionException: Class scope does not exist in file when setting up Laravel Passport Scopes

Problem: I’m retro-fitting ‘scopes’ to an existing Laravel Passport API to allow some different levels of client access. After adding the available scopes with Passport::tokensCan, and default scopes with Passport::setDefaultScope, and attempting to specify a scope on a route like so:

Passport::setDefaultScope([
    'other-access'
]);

Passport::tokensCan([
    'write-access' => 'Read-write access',
    'read-access' => 'Read-only access',
    'other-access' => 'Other access'
]);
Route::get('endpoint/count', 'API\EndpointController@count')->middleware(['auth:api', 'scope:read-access']);

And an attempt to query the route gives this error:

ReflectionException: Class scope does not exist in file /path/to/webroot/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 790

Solution: READ THE DOCS. Again, I didn’t read the docs FULLY. In order to use token scopes, the docs say…

To get started, add the following middleware to the $routeMiddleware property of your app/Http/Kernel.php file:

'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,

🤦🏻‍♂️ 🤦🏻‍♂️ 🤦🏻‍♂️ 🤦🏻‍♂️ 🤦🏻‍♂️ 🤦🏻‍♂️

Adding sections or headings to WordPress Menus – Making use of the walker_nav_menu_start_el filter

The WordPress menu builder makes it easy to add nested menu item links to a named menu, and display it in pre-defined theme location. However, as of writing in Jan 2019, there’s not an easy or built-in way to add menu sections or section headers. Here’s my solution:

The Design

Example visual to be recreated as a WordPress menu
Example visual to be recreated as a WordPress menu

The adjacent image shows the design that I’ve been asked to translate to a WordPress native menu. The orange items are page links. The white items are section headers and should not be active in any way. They are simply visual cues to aid navigation.

The Challenge

‘Out of the box’ WordPress doesn’t provide a place for menu section headers like ‘Services’ and ‘Products’ to be entered. It is possible to add them based on a menu item’s class or ID using the CSS ‘content‘ property, or an absolutely positioned image, but this is tacky and hard-coded, so site owners and admins can’t easily change the menu section headers without a developer type person or some Additional CSS hackery.

We need to find a way of adding section headers that uses the WordPress menu builder and remains editable. Continue reading “Adding sections or headings to WordPress Menus – Making use of the walker_nav_menu_start_el filter”

Manually verifying created users when using Laravel Email Verification

Laravel 5.7 ships with bundled Email Verification. This is great if you want to make sure a user’s email address is valid (or at least that the user can access it) before allowing them access.

Problem:
What if you want to manually verify a user without sending them an email address? For example you might want to add or import a load of existing ‘known-good’ users to a migrated app. Or you might be creating Admin or System users that don’t really have accessible email addresses. There could be many reasons.

At present as soon you register a user, the sendEmailVerificationNotification method is called. There’s a good breakdown of how this works in this Stack Overflow answer. If a user is manually created, then when they try to log in they’ll still see the “Verify your email address” message:

Laravel's email verification message

Solutions:
Whether a user has verified their email address or not is indicated by a timestamp in the email_verified_at column of the User table¹. If this column is set to a valid timestamp upon user creation, the user will be ‘validated’ and no email will be sent. So… how can we set that timestamp?  Continue reading “Manually verifying created users when using Laravel Email Verification”

Fixing “[vue-loader] vue-template-compiler must be installed as a peer dependency”

Problem:
Whilst attempting to upgrade an old Laravel project, I hit the following error.

Module build failed (from ./node_modules/vue-loader/lib/index.js):
Error: [vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options.

Solution:
After some searching of GitHub and stackoverflow, it turns out that The vue and vue-template-compiler module version must be the same. Here was my existing package.json:
Continue reading “Fixing “[vue-loader] vue-template-compiler must be installed as a peer dependency””

Stop WordPress installing new bundled themes and plugins when the core is upgraded.

Problem: Every time WordPress updates to a newer major version it tries to install the newest default Theme too, i.e. WP5.0 arrived with the theme Twenty Nineteen.

In many – maybe MOST cases – we aren’t going to want this to happen on an existing WordPress installation, especially if you are already using a custom theme.

Solution: Thankfully there’s a config value to stop this happening. The value is:

define('CORE_UPGRADE_SKIP_NEW_BUNDLED', true);

…and if you add this line to your `wp-config.php` file (usually found in the root of your site) it will skip new bundles items when the core is upgraded, as the name suggests.

Please let me know if this post was useful with a click, be honest 🙂
Nope, not helpful...Yep, more useful than not! - +3 thumb, 7 overall.
Loading...

Updated for 1.2 & 1.3: Adding category depth tag to Shopp

Problem: I needed to display sub categories of a category in category.php but only one level deep. Using:

<?php if(shopp('category','hascategories')): ?>
  <?php while(shopp('category','subcategories')): ?>
    //display stuff
  <?php endwhile; ?>
<?php endif; ?>

…displays all subcast AND their subcats. Continue reading “Updated for 1.2 & 1.3: Adding category depth tag to Shopp”

Remove ‘ul’ and ‘li’ tags from WordPress menus while retaining all the classes

Problem:
I need to output a WordPress nav menu as a series of anchors surrounded by spans, within one nav element. Manipulating the args passed to wp_nav_menu() function will allow the <ul> element to be removed, and passing the output through ‘striptags’ can remove the <li> elements, but I end up losing all the link ancestry classes.

Solution:
If a ‘Walker’ class is used, the output from wp_nav_menu() can be controlled totally. Here’s an example that removes the list elements, adds a span around each item link, and adds the possibility of a separator string between each menu item.

Add to your theme / header / etc: Continue reading “Remove ‘ul’ and ‘li’ tags from WordPress menus while retaining all the classes”