Skip to content

Instantly share code, notes, and snippets.

View rsisco's full-sized avatar

Richard Sisco rsisco

  • 10:58 (UTC -05:00)
View GitHub Profile
@rsisco
rsisco / mysql_m2_category_builder.sql
Last active May 26, 2022 17:42
MySQL to build full Magento 2 category paths with names by store
# Create two stored functions that will be used
DELIMITER $$
CREATE FUNCTION split_string(stringToSplit VARCHAR(256), sign VARCHAR(12), position INT) RETURNS LONGTEXT
BEGIN
# Return the requested section of the delimited string
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(stringToSplit, sign, position),LENGTH(SUBSTRING_INDEX(stringToSplit, sign, position - 1)) + 1), sign, '');
END$$
CREATE FUNCTION get_category_path(catId INT, storeId INT) RETURNS LONGTEXT
BEGIN
@rsisco
rsisco / gist:6deeb3209ec9a396c859ca249a9ab5fa
Last active January 11, 2021 23:07
Magento 2 - Identify gallery image files missing from filesystem
mysql -u <user> -p <db> -h <host> -e "SELECT distinct case when value like '/%' then concat('.',value) else value end FROM catalog_product_entity_media_gallery" | awk '{print $1}' | xargs ls | grep "cannot access"
@rsisco
rsisco / fixascii.sh
Created November 12, 2019 19:35
Replace Windows linefeeds with Unix linefeeds en masse
#!/bin/sh
zip -qr foo.zip "$@" && unzip -aqo foo.zip && rm foo.zip
# Usage: fixascii [files or directories to convert]
# Taken from http://hints.macworld.com/article.php?story=20031018164326986
@rsisco
rsisco / generate_rewrite_data.sql
Created October 24, 2018 20:55
Select data needed for generating URL rewrites for M2
# Select data needed for generating URL rewrites for M2
SELECT url AS old_uri, '' AS new_uri, category_id, product_id, sku FROM m1_url_export WHERE store_id = ?;
@rsisco
rsisco / create_m1_and_ga_tables.sql
Created October 24, 2018 20:52
Create `google_analytics_export` and `m1_url_export` tables
###################################################################
# Table creation SQL
CREATE TABLE `google_analytics_export` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`page` varchar(255) DEFAULT NULL,
`pageviews` int(11) DEFAULT NULL,
`store_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18180 DEFAULT CHARSET=latin1;
@rsisco
rsisco / compare_m1_urls_to_ga_urls.sql
Last active October 24, 2018 20:58
Compare M1 URL rewrites to Google Analytics data to find GA requests that are missing from rewrites
# This script assumes that a table has been created (m1_url_export) containing the output generated by https://gist.github.com/rsisco/a14b3383b2ab9f6435139d107e428a43
# as well as a table (google_analytics_export) that contains page request paths and counts from Google Analytics:
# Select the path without the query string
SELECT SUBSTRING_INDEX(page, '?', 1) AS page, sum(pageviews) AS pageviews FROM google_analytics_export AS gae
LEFT JOIN m1_url_export AS m ON gae.page = m.url and gae.store_id = m.store_id
# Only include results with no M1 match
WHERE m.url IS NULL
# Exclude paths we don't need to know about to keep the list pared down
AND page NOT LIKE '/catalog/product/gallery/%'
@rsisco
rsisco / m1_url_rewrites.sql
Last active October 24, 2018 20:59
MySQL to retrieve a list of all M1 URL rewrites by store ID, but filter products to only include those that are enabled and visible
# Enabled product URLs for provided store ID
SELECT
r.store_id,
request_path, concat('/', request_path) AS url,
concat('/', target_path) AS target_url,
CASE
WHEN r.options IS NULL THEN 'internal'
WHEN r.options = 'RP' THEN '301'
WHEN r.options = 'R' THEN '302'
END AS url_type,
@rsisco
rsisco / duplicate_keys.sql
Last active April 5, 2018 15:05
MySQL to create stored procedure and function to update duplicate URL keys to be unique
DELIMITER ;;
CREATE DEFINER=`magento_prod`@`%` FUNCTION `generate_unique_url_key`(target_value_id INT) RETURNS LONGTEXT CHARSET latin1
READS SQL DATA
BEGIN
DECLARE url_key_attr_id INT DEFAULT 124;
DECLARE counter INT DEFAULT 1;
DECLARE url_key LONGTEXT;
DECLARE first_pass BOOLEAN DEFAULT TRUE;
# Retrieve current URL key for provided value_id row
SELECT `VALUE` INTO @current_url_key FROM catalog_product_entity_varchar WHERE value_id = target_value_id;
@rsisco
rsisco / mysql_m1_category_builder.sql
Last active May 26, 2022 15:42
MySQL to build full Magento 1 category paths with names by store
# Create two stored functions that will be used
DELIMITER $$
CREATE FUNCTION split_string(stringToSplit VARCHAR(256), sign VARCHAR(12), position INT) RETURNS LONGTEXT
BEGIN
# Return the requested section of the delimited string
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(stringToSplit, sign, position),LENGTH(SUBSTRING_INDEX(stringToSplit, sign, position - 1)) + 1), sign, '');
END$$
CREATE FUNCTION get_category_path(catId INT, storeId INT) RETURNS LONGTEXT
BEGIN