Skip to content

Instantly share code, notes, and snippets.

View vladdancer's full-sized avatar
🖖

Vlad Moyseenko vladdancer

🖖
View GitHub Profile
@vladdancer
vladdancer / gist:c896710a1453c8b27f82c4ac33e71509
Created November 8, 2022 14:24 — forked from jleehr/gist:7ac2a5de785881dfce99f7034547f07b
Drupal 8 - Migrate from public to private files
# Enable maintanance mode
drush state-set system.maintenance_mode 1
# Backup file field data
drush sql-query "CREATE TABLE media__field_file_bak AS SELECT * FROM media__field_file;"
drush sql-query "CREATE TABLE media_revision__field_file_bak AS SELECT * FROM media_revision__field_file;"
# Truncate file field tables (need to change storage settings)
drush sql-query "TRUNCATE media__field_file;"
drush sql-query "TRUNCATE media_revision__field_file;"
@vladdancer
vladdancer / mysqldump and mysqlpump backup.md
Created October 27, 2022 21:47 — forked from ebta/mysqldump and mysqlpump backup.md
Backup MySQL database using mysqldump & mysqlpump

Backup Using mysqldump

Mysqldump is a command-line utility that is used to generate the logical backup of the MySQL database. It produces the SQL Statements that can be used to recreate the database objects and data. The command can also be used to generate the output in the XML, delimited text, or CSV format.

Note: By default, mysqldump command does not dump the information_schema database, performance_schema, and MySQL Cluster ndbinfo database. If you want to include the information_schema tables, you must explicitly specify the name of the database in the mysqldump command, also include the —skip-lock-tables option.

mysqldump -uroot -p  --all-databases > all-databases.sql
mysqldump -uroot -p dbname > dbname.sql
mysqldump -uroot -p dbname table1 table2 > dbname_table_1_2.sql
@vladdancer
vladdancer / drupla8_image_file_statistics.sql
Last active June 20, 2023 13:52
Get stats about drupal files/commerce orders in database
# All image files
SELECT
floor(filesize/1000000)*1000000 as bucket,
SUM(`filesize`) as bytes,
(SUM(`filesize`) / 1024 / 1024 / 1024 ) AS GB,
COUNT(*) AS COUNT,
RPAD('', LN(COUNT(*)), '*') AS bar
FROM file_managed
WHERE (`filemime` LIKE 'image%') AND (`status` = '1')
GROUP BY bucket
@vladdancer
vladdancer / custom.settings.php
Last active June 21, 2022 10:05
Load PHP class from custom module within settings.php file #php #drupal8+
<?php
/**
* @file
* Load PHP class from custom module within settings.php file.
*/
if (class_exists(ClassLoader::class)) {
$class_loader = new ClassLoader();
$class_loader->addPsr4('Drupal\\mymodule\\', DRUPAL_ROOT . '/modules/custom/mymodule/src');
@vladdancer
vladdancer / legacy_database_info.php
Created June 21, 2022 09:53
Include database connection information from not native connection file #drupal8+
<?php
$legacy_db_key = 'project_legacy_db';
// Define database connection for local lando env.
if (getenv('LANDO_INFO')) {
$lando_info = json_decode(getenv('LANDO_INFO'), TRUE);
$databases[$legacy_db_key]['default'] = [
'database' => 'project_legacy',
'username' => $lando_info['database']['creds']['user'],
@vladdancer
vladdancer / find.sh
Created June 21, 2022 09:41
Find/search file by text
# https://stackoverflow.com/questions/1987926/how-do-i-recursively-grep-all-directories-and-subdirectories
grep --include="*.ext" -nRHI "text" ~/projects/
@vladdancer
vladdancer / drush-run-command-unser-user.drush.php
Created June 21, 2022 09:40
Run drush 9 command from specific user
// https://github.com/drush-ops/drush/issues/3396#issuecomment-1009268719
// Bulk generate using action: https://gist.github.com/seth-shaw-unlv/bd64930fa1f75be3aba05ade34433092
// accountSwitcher->switchTo(new UserSession(['uid' => 1]);
@vladdancer
vladdancer / moderation_state_migration_example.yml
Created June 9, 2022 11:26
Map moderation_state field in migration #drupal8 #drupla9 #migrate
# https://www.drupal.org/project/workflow/issues/3016286
# https://www.drupal.org/project/drupal/issues/3157105#comment-13830538
process:
...
moderation_state:
plugin: default_value
default_value: published
process:
...
@vladdancer
vladdancer / media--pp-external.html.twig
Created June 3, 2022 21:45
PP Media template example
{% if pp_media.asset.meta.type === 'image' %}
{{ attach_library('easy_responsive_images/resizer') }}
{% set srcset = [
asset.sizes['400w'].url ~ ' 600w',
asset.sizes['900w'].url ~ ' 900w',
asset.sizes['3000w'].url ~ ' 3000w',
] %}
<img src="{{ src }}" data-srcset="{{ srcset|join(',')|raw }}" alt="{{ pp_media.asset.meta.alt }}" loading="lazy" />
{% endif %}
@vladdancer
vladdancer / Redirect.php
Created January 28, 2022 18:26
Add extra source records to migrate #drupal8 #drupal9 #migrate
<?php
final class Redirect extends SqlBase {
/**
* Return list of additional redirects that doesn't exist in source db.
*
* Some redirects are not in db, but we need to add them.
*
* @return array[]