Skip to content

Instantly share code, notes, and snippets.

View attilahorvath's full-sized avatar

Attila Horváth attilahorvath

  • Dublin, Ireland
View GitHub Profile
@attilahorvath
attilahorvath / migrate_to_utf8mb4.rb
Created March 3, 2015 16:08
Migrate entire MySQL database to utf8mb4 encoding
class MigrateToUtf8mb4 < ActiveRecord::Migration
def change
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET utf8mb4;"
ActiveRecord::Base.connection.tables.each do |table|
execute "ALTER TABLE `#{table}` CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;"
ActiveRecord::Base.connection.columns(table).each do |column|
if column.sql_type =~ /varchar\((\d+)\)/
limit = $1.to_i
limit = 191 if ActiveRecord::Base.connection.indexes(table).any?{|i| i.columns.include? column.name} && limit > 191
@attilahorvath
attilahorvath / tar_compress_extract_directory.sh
Last active August 29, 2015 14:17
Tar compress/extract directory
# compress
tar -czvf archive.tar.gz directory
# extract
tar -xzvf archive.tar.gz
# flags:
# (c)reate
# e(x)tract
# use g(z)ip
@attilahorvath
attilahorvath / dump_restore_mysql_utf8mb4.sh
Created March 13, 2015 13:33
Dump/restore MySQL database with utf8mb4 encoding
# dump
mysqldump -u user -p database --default-character-set=utf8mb4 --result-file=dump.sql
# restore
mysql -u user -p database < dump.sql
@attilahorvath
attilahorvath / multistage_whenever_capistrano_3.rb
Last active August 29, 2015 14:17
Multistage Whenever configuration for Capistrano 3
# Capfile
require "whenever/capistrano"
# deploy.rb
set :whenever_environment, -> { fetch(:stage) }
set :whenever_identifier, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
# schedule.rb
case @environment
when "staging"
@attilahorvath
attilahorvath / apache_2_rails_config.sh
Created March 13, 2015 13:54
Apache 2 config for Rails apps running on localhost
# /etc/apache2/users/user.conf
NameVirtualHost *:80
<Directory "/path/to/rails/apps/">
Options Indexes Multiviews Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
@attilahorvath
attilahorvath / active_record_mysql_utf8mb4_patch.rb
Last active September 23, 2015 08:33
Limit ActiveRecord strings to 191 characters to avoid index too long errors with MySQL utf8mb4 encoding
# config/initializers/active_record_patch.rb
require 'active_record/connection_adapters/abstract_mysql_adapter'
module ActiveRecord
module ConnectionAdapters
class AbstractMysqlAdapter
NATIVE_DATABASE_TYPES[:string] = { name: "varchar", limit: 191 }
end
end
@attilahorvath
attilahorvath / Preferences.sublime-settings
Last active October 21, 2015 08:46
My Sublime Text 3 user settings
{
"caret_extra_width": 1,
"caret_style": "phase",
"color_scheme": "Packages/Predawn Twilight Theme/Predawn Twilight.tmTheme",
"detect_indentation": false,
"draw_minimap_border": true,
"findreplace_small": true,
"font_size": 13,
"highlight_line": true,
"ignored_packages":
@attilahorvath
attilahorvath / generate_delayed_job_executable.sh
Last active August 29, 2015 14:17
Generate bin/delayed_job executable on server to fix command not found error on Capistrano deploy
# To fix 'command not found: bin/delayed_job' error on Capistrano deploy, execute this on the server
bundle exec rails generate delayed_job
@attilahorvath
attilahorvath / youtube_video_id_and_cover.rb
Created April 9, 2015 11:23
Extract video ID and get cover image URL from a YouTube embed code
# Method to get the cover image URL for a YouTube video by its ID
# There are several cover images avaiable for every video, you can specify which one you need through the type parameter
# For a list of possible cover types see http://stackoverflow.com/a/2068371/3463845
# The returned URL will be protocol-relative, so it should work on sides loaded through HTTP and HTTPS as well
def youtube_cover_for(id, type = :default)
"//img.youtube.com/vi/#{id}/#{type}.jpg"
end
# Method to extract the video ID from a YouTube embed code
# This very simple regex works by exploiting the fact that the YouTube video IDs are always 11 characters long
@attilahorvath
attilahorvath / gist:8634656bae76ea191b78
Created May 15, 2015 11:24
Whenever tasks to automatically start web apps and background workers after a server reboot
# config/deploy.rb
# Using Capistrano 3
# Set the current environment for Whenever and avoid name collisions
set :whenever_environment, -> { fetch(:stage) }
set :whenever_identifier, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
# config/schedule.rb
every "@reboot" do
# If the app is using Puma
command "cd #{path} && bundle exec puma -e #{@environment}"