Skip to content

Instantly share code, notes, and snippets.

@davidrenne
davidrenne / cron tab delete old
Created August 6, 2012 12:48
find files with extension sql who are older than 1 day and delete them
0 1 * * sun find /tmp -type f -a -mtime +1 -name "*.sql" -exec rm \{\} \;
@davidrenne
davidrenne / gist:3420197
Created August 21, 2012 23:09
Import Any CSV into mySQL queries
<?php
$file = 'C:\\data.csv';
$fields = array();
$sqlOutput = '';
if($f = fopen($file, 'r'))
{
$fields = fgetcsv($f);
fclose($f);
if (!empty($fields))
module Enumerable
# clumps adjacent elements together
# >> [2,2,2,3,3,4,2,2,1].cluster{|x| x}
# => [[2, 2, 2], [3, 3], [4], [2, 2], [1]]
def cluster
cluster = []
each do |element|
if cluster.last && yield(cluster.last.last) == yield(element)
cluster.last << element
else
birds = ["Golden Eagle", "Gyrfalcon", "American Robin",
            "Mountain BlueBird", "Mountain-Hawk Eagle"]
grouped_by_first_letter = birds.group_by { |s| s[0] }
@davidrenne
davidrenne / gist:4207799
Created December 4, 2012 19:31
Parse Command Line Arguments in ruby
if $0 == __FILE__
ARGV.each{|arg|
case arg
when /\A--ruby=(.+)/
$ruby_program = $1
when /\A--matzruby=(.+)/
$matzruby_program = $1
when /\A--opts=(.+)/
$opts = $1
when /\A(-r|--only-ruby)\z/
@davidrenne
davidrenne / new gem.sh
Created December 12, 2012 17:07
setting up a new gem in bash
CAMELCASE=WidgetList
GEMNAME=widget_list
rvm gemset create $GEMNAME
rvm gemset use $GEMNAME
gem install bundler
bundle gem $GEMNAME
#chown wcapp:wcapp -R $GEMNAME/*
mkdir -p vendor/assets/{images,javascripts,stylesheets}
@davidrenne
davidrenne / gist:4288545
Created December 14, 2012 20:51
load YML with ERB parser.
YAML.load(ERB.new(File.new("#{Rails.root}/config/xxxx.yml").read).result)[ENV['RAILS_ENV']]
@davidrenne
davidrenne / gist:4470085
Created January 6, 2013 20:41
Get the name of the application in rails
Rails.application.class.parent_name.constantize
@davidrenne
davidrenne / gist:4475557
Created January 7, 2013 14:59
log all queries to file in ruby/rails
connection = ActiveRecord::Base.connection
class << connection
alias :original_exec :execute
def execute(sql, *name)
# try to log sql command but ignore any errors that occur in this block
# we log before executing, in case the execution raises an error
begin
file = File.open(Rails.root + "/log/database.log",'a'){|f| f.puts Time.now.to_s+": "+sql}
rescue Exception => e
;
@davidrenne
davidrenne / gist:5124199
Created March 9, 2013 13:42
db connection php script
CREATE TABLE IF NOT EXISTS `database_connections` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`connection_id` int(11) NOT NULL,
`queries` text NOT NULL,
`page` varchar(300) NOT NULL,
`ip_address` varchar(255) NOT NULL,
`t_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `connection_unique` (`connection_id`),
KEY `connection_id` (`connection_id`)