Skip to content

Instantly share code, notes, and snippets.

View vyspiansky's full-sized avatar

Ihor Vyspiansky vyspiansky

View GitHub Profile
@vyspiansky
vyspiansky / mysql-queries-using-timestamp-field.md
Last active October 25, 2024 09:43
MySQL queries using timestamp integer field

MySQL queries for selecting records based on different time criteria using a timestamp integer field named created.

Get records newer than today's 10 AM

SELECT
  *
FROM
  your_table
WHERE
@vyspiansky
vyspiansky / git-rebase-by-default.md
Created September 12, 2024 09:10
Git rebase by default

This technique updates your local branch and then "replays" your changes on top, effectively putting your changes at the head of the branch.

Enable

git config --global pull.rebase true

Disable

@vyspiansky
vyspiansky / coding-programming-quotes.md
Created September 9, 2024 20:26
Coding & Programming Quotes

“There are only two hard things in Computer Science: cache invalidation and naming things.”

— Phil Karlton

@vyspiansky
vyspiansky / clear-older-recent-log-messages-in-drupal.sql
Last active September 9, 2024 15:00
Clear older recent log messages in Drupal 10, retaining only the last 500
-- Remove all watchdog's records from a MySQL table except for the last 500.
DELETE FROM `watchdog`
WHERE wid NOT IN (
SELECT wid
FROM (
SELECT wid
FROM `watchdog`
ORDER BY wid DESC
LIMIT 500
@vyspiansky
vyspiansky / plain_sql_query_in_drupal.php
Last active September 11, 2024 15:57
Retrieve a plain SQL query from a Select query object in Drupal 10
<?php
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\SelectInterface;
/**
* Returns a plain SQL query from a Select query object.
*/
function get_plain_sql_query(SelectInterface $query): string
{
@vyspiansky
vyspiansky / enable-xhprof-in-lando.md
Last active August 27, 2024 19:57
Enable XHProf in Lando

.lando.yml

name: "..."
recipe: lamp
config:
  php: "8.1"
  xdebug: false
services:
 appserver:
@vyspiansky
vyspiansky / drupal-clear-cache-programmatically.md
Last active August 22, 2024 08:19
Clear cache programmatically in Drupal 8+

Clear cache by specific tags

\Drupal::service('cache_tags.invalidator')->invalidateTags([$tag1, $tag2]);

Clear specific cache like render cache

\Drupal::service('cache.render')-&gt;invalidateAll()
@vyspiansky
vyspiansky / set-env-variable-in-htaccess.md
Created July 7, 2024 13:30
How to set environment variables in a .htaccess file
@vyspiansky
vyspiansky / validate_password_with_preg_match.php
Last active May 13, 2024 11:55
Validate a password using preg_match with regular expression
<?php
declare(strict_types=1);
/*
A pattern with multiple conditions:
- at least one digit.
- at least one lowercase letter (including special Scandinavian characters).
- at least one uppercase letter (including special Scandinavian characters).
- at least one character that is not a letter or a number (including special Scandinavian characters).
@vyspiansky
vyspiansky / response_403_in_controller.php
Created April 18, 2024 13:08
Drupal: return a 403 response from a controller
<?php
namespace Drupal\your_module\Controller;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class YourController {
public function yourPage() {
// Some logic here...