Example SQL Report with the Ruby Spreadsheet Gem

By kenglish

If you want to create a Excel reports for your users, this can be done rather easily in Ruby using the Spreadsheet Gem.

def spreadsheet_report(excel_filename, worksheet_name, 
                       column_order, result)
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => worksheet_name
 
    rownum = 0
    for column in column_order
      sheet1.row(rownum).push column
    end
    for row in result
      rownum += 1
      for column in column_order
        sheet1.row(rownum).push row[column].nil? ? 'N/A' : row[column]
      end
    end
    book.write "#{excel_filename}.xls"
 
end

Here’s what the code would like in your rake task

column_order = ["Name", "DOB", "Rank","Hire Date", 
                "Height", "Weight" ]
 
sql =<<-END
  SELECT name AS Name,
   date_of_birth AS DOB, 
   rank AS Rank,
   hire_date Hire Date,
   height AS Height,
   weight AS  Weight
  FROM fire_fighters
  ORDER BY name
END
 
conn  = ActiveRecord::Base.connection
result    = conn.select_all(sql)
 
excel_filename = "FireFighterReport#{Time.year}" 
worksheet_name = "FireFighter Report #{Time.year}"
spreadsheet_report(excel_filename, worksheet_name, 
                   column_order, result)

Now, that’s easy!

Ruby Spreadsheet Gem Documentation

categoriaMysql, Programming commento1 Comment dataJune 17th, 2009
Read All

svn2git for real men

By kenglish

On the server, set up the remote repositories:

1
2
3
mkdir project1.git
cd project1.git
git --bare init

Here’s a script to do them all in one shot, just modifiy the REPOS variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
 
REPOS="project1 project2 project3 project4"
 
for repo in $REPOS
do
    repo_dir="$repo.git"
    mkdir -p $repo_dir
    echo "Creating git directory  $repo_dir"
    cd $repo_dir
    git --bare init
    cd ..
done
exit

Now, on the workstation:

1
2
sudo apt-get install git-core git-svn
sudo gem install nirvdrum-svn2git --source http://gems.github.com

Create the authors.txt in the following format:

1
2
dburger = David Burger <email@email.com>
jdoe = John Doe <jdoe@doe.com>

For one project, do the following:

1
2
3
4
5
mkdir project1
cd project1
svn2git  https://svn.myserver.org/repos/ses --authors ../authors.txt
git remote add origin hailstorm.myserver.org:/home/kenglish/repotest/ses.git
git push --all

The script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
 
REPOS="project1 project2 project3 project4"
 
for repo in $REPOS
do
    mkdir $repo
    cd $repo
 
    cmd="svn2git https://svn.myserver.org/repos/$repo --authors ../authors.txt"
    echo $cmd
    `$cmd`
    cmd="git remote add origin hailstorm.myserver.org:/home/kenglish/repotest/$repo.git"
    echo $cmd
    `$cmd`
    cmd="git push --all"
    echo $cmd
    `$cmd`
    cd ..
    echo "DONE EXPORTING $repo" 
done
exit

Don’t diss the shell script, I leave in the echoes in case i need to test stuff out…

Note: Another option for the authors file is place it in your home directory .svn2git/authors (e.g. /home/kenglish/.svn2git/authors). Svn2git will automatically detect it and use it.
Any questions? Comments?

categoriaMysql, Programming, Tech commento3 Comments dataMay 21st, 2009
Read All

PHP/Mysql on Ubuntu 7.04

By admin

On Ubuntu 7.04 (Feisty Fawn), mysql_connect and all those leftover functions from php3/php4 do not work. YOu have to go into /etc/php5/apache2/php.ini and add the .so file. Do this:

sudo vi /etc/php5/apache2/php.ini

Add:

extensions = mysql.so

Make sure to restart apache:

sudo /etc/init.d/apache2 restart

categoriaMysql commentoNo Comments dataAugust 31st, 2007
Read All

phpmyadmin login cookie timeout

By admin

If you are working on your local box, phpmyadmin will expire the session every 1800 seconds which is 30 minutes. sometimes that just too soon, so you can hack it by change the value of $cfg['LoginCookieValidity'] in /usr/share/phpmyadmin/libraries/config.default.php

i usually set it to 18000…

$cfg['LoginCookieValidity'] = 18000; // validity of cookie login
// (in seconds)

I usually set this and forget it but it always takes me like 4-5 minutes to FTFSC and find where to set it again..

categoriaMysql commentoNo Comments dataAugust 9th, 2007
Read All

TRUNCATE ALL Tables

By admin

USE AT YOUR OWN RISK. Put this in a script, mysqltrunc.sh then run it….it will truncate all the data in all of your table

#!/usr/bin/env bash
# Written by David Burger

if [ $# -eq 2 ]; then
user=$1
dbname=$2
basecmd=”mysql -u ${user} -D ${dbname}”
elif [ $# -eq 3 ]; then
user=$1
PASS=$2
dbname=$3
basecmd=”mysql -u ${user} -p$PASS -D ${dbname}”
else
echo “usage: mysqltrunc user [pass] database” >&2
exit 1
fi

tables=$(${basecmd} -e “SHOW TABLES;” | grep -v “+–” | grep -v “Tables_in_${dbname}”)
if [ $? -ne 0 ]; then
echo “Unable to retrieve the table names.” >&2
exit 1
fi

cmd=”"

for table in ${tables}; do
cmd=”${cmd} TRUNCATE ${table};”
done

$(${basecmd} -e “${cmd}”)

categoriaMysql commentoNo Comments dataAugust 3rd, 2007
Read All

Mysql Dump

By admin

I have a love/hate with this program. Here are so notes so I don't have to keep looking it up…

Creates all the tables with the data but don't add the “drop database” statements. Good for if you are going to get a dump so you can restart a replication that's died:

mysqldump -umyuser -pmypasswrd -h localhost –add-drop-table –no-create-db –quick –databases –extended-insert db1 db2 db3

Sometimes, you many want to do this in 2 stages

Dump structure only:
mysqldump -umyuser -pmypasswrd -h localhost –add-drop-table –no-data –no-create-db

Dump data:
mysqldump -umyuser -pmypasswrd -h localhost –quick –extended-insert –no-create-db –no-create-info

mysqldump -usps -p sps_development –quick –extended-insert –no-create-db –no-create-info

categoriaMysql commentoNo Comments dataJune 9th, 2007
Read All