Skip to content

Instantly share code, notes, and snippets.

View leemour's full-sized avatar

Viacheslav Ptsarev leemour

View GitHub Profile
@leemour
leemour / restore_postgres.sh
Created July 21, 2015 08:41
Restore Postgres dump
cat db.txt | psql dbname
@leemour
leemour / Rakefile.rb
Created November 11, 2015 12:03 — forked from drogus/Rakefile.rb
This is the example contents of the Rakefile, which you would use to run active record tasks without using Rails. It assumes using the same directories as rails uses: `db/migrate`, `config/database.yml`.
require 'bundler/setup'
require 'active_record'
include ActiveRecord::Tasks
db_dir = File.expand_path('../db', __FILE__)
config_dir = File.expand_path('../config', __FILE__)
DatabaseTasks.env = ENV['ENV'] || 'development'
@leemour
leemour / logger-colors.rb
Created November 13, 2015 12:52 — forked from janlelis/logger-colors.rb
logger-colors
# Colorizes the output of the standard library logger, depending on the logger level:
# To adjust the colors, look at Logger::Colors::SCHEMA and Logger::Colors::constants
require 'logger'
class Logger
module Colors
VERSION = '1.0.0'
NOTHING = '0;0'
@leemour
leemour / iptablesrc
Created November 23, 2015 12:45
Iptables redirect port 3000 to port 80
# sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000
# sudo iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 3000
sudo iptables -t nat -A OUTPUT -d localhost -p tcp --dport 80 -j REDIRECT --to-port 3000
# Save iptables to file
sudo bash -c "iptables-save -c > /etc/iptablesrc"
# Apply localhost redirect saved in iptablesrc on system start
# sudo bash -c "iptables-restore < /etc/iptablesrc"
@leemour
leemour / change_extension.sh
Created December 29, 2015 12:00
Rename files, change extension in subdirectories
for file in $(find `pwd` -name "*.html.slim"); do
mv "$file" "`dirname $file`/`basename $file .html.slim`.slim"
done
@leemour
leemour / hstore.sh
Last active February 22, 2018 11:16 — forked from terryjray/gist:3296171
Enabling hstore for new Postgresql 9.5 and Rails 5 install on Ubuntu 14.04
RAILS_ENV=production rake db:setup
# produces the error below.....hmmm.....it's a no-worky
psql:/yourprojectpath/yourproject/db/structure.sql:29: ERROR: could not open extension control file "/usr/share/postgresql/9.1/extension/hstore.control": No such file or directory
# hstore postgresql extension needs to be installed, so....
sudo apt-get install postgresql-contrib
# now your extension should be available to enable so log in with psql
psql -d yourproject_production -U yourdbuser -W
@leemour
leemour / capybara cheat sheet.rb
Created March 20, 2016 17:14 — forked from zhengjia/capybara cheat sheet
capybara cheat sheet
is_expected.to have_link('link_text', href: 'url')
expect(find_link('link_text')[:target]).to eq('_blank')
#=Navigating=
visit('/projects')
visit(post_comments_path(post))
#=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
@leemour
leemour / export_records.sql
Created April 2, 2016 12:30
Export selected records from DB to file dump - Postgres
# For a data-only export use COPY.
# You get a file with one table row per line as plain text (not INSERT commands), it's smaller and faster:
COPY (SELECT * FROM nyummy.cimory WHERE city = 'tokio') TO '/path/to/file.csv';
# Import the same to another table of the same structure anywhere with:
COPY other_tbl FROM '/path/to/file.csv';
@leemour
leemour / books_controller.rb
Created April 11, 2016 18:30 — forked from cupakromer/books_controller.rb
Rails 4 Standard CRUD Controller
# No flash messages are set with this controller.
#
# This is a fairly basic / bare controller which does only the basic CRUD.
class BooksController < ApplicationController
respond_to :html, :xml, :json
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
respond_with @books = Book.all
@leemour
leemour / pg_regexp_replace.rb
Created June 13, 2016 09:07
Postgres replace regexp character in a field
ActiveRecord::Base.connection.execute("
UPDATE adverts SET tel = regexp_replace(tel, '\\+78', '7');
")