Git and Rails : Switch your database.yml when changing branch

Posted by Antonin AMAND Wed, 09 Jul 2008 10:01:00 GMT

As a lot of people, I think, I have different database.yml file according to git branch I'm working on. I was constantly switching it manually until I discovered that git has more hooks that I thought.

I set up a post-checkout hooks script to automagically switch my database.yml when checking out a branch.

It is a simple script that recover the branch name a do a symlink.

if i checkout branch master it will look for a file named config/database.master.yml and create a symlink to config/database.yml

here it is :


#! /bin/sh
#
# .git/hooks/post-checkout

BRANCH=`git symbolic-ref HEAD 2> /dev/null | sed -e 's/^.*\///'`
GITPATH="`git rev-parse --git-dir 2>/dev/null`/.."

if [ "$BRANCH" == "" ]; then
        echo "no branch name. do nothing"
        exit 0
fi

if [ -f "$GITPATH/config/database.yml" ] && [ ! -L "$GITPATH/config/database.yml" ]; then
        echo "database.yml exists and not a symlink. do nothing."
        exit 0
fi

echo "look for $GITPATH/config/database.$BRANCH.yml"

if [ -f "$GITPATH/config/database.$BRANCH.yml" ]; then
        echo "switch database configuration file to config/database.$BRANCH.yml"
        rm -f "$GITPATH/config/database.yml";
        ln -s "$GITPATH/config/database.$BRANCH.yml" \
        "$GITPATH/config/database.yml"
fi
 

or on Pastie

Using git to mirror a SVN repository on Leopard 3

Posted by Antonin AMAND Mon, 04 Feb 2008 10:20:00 GMT

Working for a new client on a Rails project, I decided to give a shot at Git.

I’m working with an other developer who holds the svn repository. He is the only person allowed to commit to the trunk and I have to commit to my own branch. At first, this doesn’t seemed to be a problem. But there is a lot of files modified every day on the trunk and I’m constantly merging from the trunk to keep my branch up to date. Git seems to have some nice features that could help me to avoid those constant merges.

I writing this article while installing git so I don’t already know if it is the solution to my troubles. I just read this article and it convinced me to give git a try.

I have to mention that I never worked with git before so I will discover git with this project and we’ll see if it’s reasonable. I heard about git on rubyinside blog a few weeks ago.

So, as I’m on leopard, the easiest way (for me) to build git is macports so let’s do it.

sudo port install git-core +svn

This installs all git git-svn and gitk. All ran smoothly for me. If it fails try this install tutorial for leopard.

Now it’s installed properly I need to build my git project from subversion repository

daiquiri% git svn init https://repos.../trunk
Initialized empty Git repository in .git/

It doesn’t checkout the sources so let’s check this previously mentioned article to see what I do next.

All right, I need to figured what is the HEAD revision number to go next :
svn info https://repos.../trunk
I can now fetch the sources.
git-svn fetch -r3741
.
.
...The checkout happens...
.
.
r3741 = b444a88efc7248dfdd5f96a7dd7e36165a0f3ed6 (git-svn)
Checking 4130 files out...
 100% (4130/4130) done
Checked out HEAD:
  https://82.240.32.108/svn/eco-sys/trunk r3741

It seems all went ok and that my git project is ready to go.

I little config seems to make things a little prettier. I took this from here

git config --global user.name "Antonin Amand"
git config --global user.email "aamand@gwikzone.org"
git config --global color.status auto
git config --global color.diff auto                                             
git config --global color.diff auto
git config --global color.branch auto
git config --global merge.tool opendiff
git config --global core.excludesfile ~/.gitignore
echo ".DS_Store" >> ~/.gitignore
cat > ~/.gitk <<EOF
set mainfont {Monaco 10}
set textfont {Monaco 10}
set uifont {Monaco 10}
EOF

I will know do some changes in the configuration to get the working on my mac. I understood that the best practice is to create a new branch.

git checkout -b localconfig HEAD

This creates a new branch a switch in it immediatly.

I’ve modified some files into my local branch, and have commit to the localconfig branch.
git add mymodifiedfile
git add ...
git commit
will only commit files you added with git add. Or you can use
git commit -a
will commit every modified files.

Now I’ve seen that the other developer add update a lot of files in the trunk. So I will try to update my git branch.

git svn rebase

This first revert your repository before my modifications update from subversion and apply again my commit. All went nicely. Notice that it is extremely more powerfull that trying to apply the update directly on the modified sources.

Now I want to commit to the remote svn, but the other developer don’t allow me to commit to the trunk. I will create a patch with git, create a new branch in the subversion repository from the trunk, apply the patch and then ask for the developer to merge changes from the new branch to the trunk.

So, let’s create the patch
git format-patch remotes/git-svn
This creates a traditional UNIX patch.

Then I create the branch into the subversion repository and checkout.

svn copy http://repos/trunch http://repos/branches/merge-branch
svn checkout http://repos/branches/merge-branch
cd merge-branch

Now I need to apply the patch.

patch -p1 < /path/to/thepatch
All I have to do now is to commit to the svn.
svn commit

Now I just have to send an email to the other developer.

With this first shot, I am conviced that git will improve my merging process and be a lot more flexible that SVN.

Now that I will play a lot with patches, next time I’ll probably take a look at StGIT (Stacked Git), patch management utility.

Rails : convert all tables to utf8 2

Posted by Antonin AMAND Tue, 06 Nov 2007 15:42:00 GMT

I sometimes need to convert old rails mysql databases to use unicode (utf8)

here is a way to do this quickly :

require 'rubygems'
require 'active_record'

db = "my_database"

sqlconn = ActiveRecord::Base.establish_connection(
  :adapter  => "mysql", 
  :host     => "localhost", 
  :username => "root", 
  :password => "", 
  :database => db
)

conn = ActiveRecord::Base.send(sqlconn.adapter_method,sqlconn.config)

conn.tables.each do |table|
    q = "ALTER TABLE #{table} CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci"
    puts q
    conn.execute q
end 
conn.execute("ALTER DATABASE #{db} DEFAULT CHARACTER SET utf8 ;")

edit : see Agouti’s comment

New syntax for DefaultOrder plugin 2

Posted by Antonin AMAND Mon, 15 Oct 2007 13:51:00 GMT

I’ve updated the DefaultOrder plugin that now have a new syntax.


class Contact < ActiveRecord::Base 

 order_by :fields => ['last_name', 'first_name'], :mode => :desc

end

More details.

Rails : Validate availablity of a distant ressource over HTTP

Posted by Antonin AMAND Wed, 18 Jul 2007 22:39:00 GMT

Sometimes you may host some ressources on a different website and you may want to validate the availability of it when creating a Rails model object (ActiveRecord::Base).

It would be dramatically unefficient to download the full media with a GET request. So, to achieve this we will use the HEAD http request that act as the GET request but return only the HTTP header without the body.

Here is a exemple of an ActiveRecord::Base object that validates the presence of the ressource among the Internet.


require 'net/http'

# the class is supposed to have a "media" attribute
# that is a relative url to a ressource hosted an a different
# website
class Article < ActiveRecord::Base

  RESOURCE_HOST = "flickr.com"
  RESOURCE_PORT = 80

  validate :validates_presence_of_external_media

  protected

  def validates_presence_of_external_media
    response = nil
    # initiating the HTTP connection
    Net::HTTP.start(RESOURCE_HOST, RESOURCE_PORT) do |http|
      # doing the HEAD request
      response = http.head(media)
    end
    # add an error if the request is not successful
    errors.add :media, "ressource unavailable : #{response.code} #{response.msg}" unless
      responce.kind_of?(Net::HTTPSuccess)
  end

end

This is a very minimalist example, but this kind of technic may have a lot of uses.

Default Order Plugin for Ruby On Rails

Posted by Antonin AMAND Mon, 16 Oct 2006 18:40:00 GMT

I’ve started the development of Default Order Plugin for Ruby on Rails

more…

Easy setup a Mongrel cluster with Apache 2.2 on Mac OS X

Posted by Antonin AMAND Mon, 02 Oct 2006 12:19:00 GMT

Mongrel is a very fast and secure server for Ruby On Rails applications. It also supports Nitro and Camping. Setting up a mongrel_cluster on Mac OS X is not very difficult but it is a bit different from other Unix platforms.

What you need to start :

Step 1 : Install Mongrel

Mongrel is available for unix platform and Win32 systems as a gem.

$ sudo gem install mongrel mongrel_cluster
Choose the latest ruby version.

Installing Apache 2.2

You can compile it from sources. But it is easier to install it via MacPorts.

$ sudo port install apache2

All apache2 files will be installed in /opt/local/apache2, including binaries and configuration files.

Setting up Apache 2.2

chdir to /opt/local/apache2/conf and copy the sample file to httpd.conf
$ cd /opt/local/apache2/conf
$ sudo cp httpd.conf.sample httpd.conf

Create a new directory that will contain your application specific configuration files for Apache.

$ sudo mkdir myapp
$ cd myapp

Create 3 files for your new application. (adapted from codahale blog)

$ sudo touch myapp.common myapp.conf myapp.cluster.conf

Here is the files content :

myapp.common :

  ServerName www.myserver.com  
  DocumentRoot /path/to/my/app/public

  <Directory "/path/to/my/app/public">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>

  RewriteEngine On

  # Make sure people go to www.myapp.com, not myapp.com
  RewriteCond %{HTTP_HOST} ^myapp.com$ [NC]
  RewriteRule ^(.*)$ http://www.myapp.com$1 [R=301,L]
  # Yes, I've read no-www.com, but my site already has much Google-Fu on
  # www.blah.com. Feel free to comment this out.

  # Uncomment for rewrite debugging
  #RewriteLog logs/myapp_rewrite_log
  #RewriteLogLevel 9 

  # Check for maintenance file and redirect all requests
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]

  # Rewrite index to check for static
  RewriteRule ^/$ /index.html [QSA] 

  # Rewrite to check for Rails cached page
  RewriteRule ^([^.]+)$ $1.html [QSA]

  # Redirect all non-static requests to cluster
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

  # Deflate
  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0[678] no-gzip
  BrowserMatch bMSIE !no-gzip !gzip-only-text/html

  # Uncomment for deflate debugging
  #DeflateFilterNote Input input_info
  #DeflateFilterNote Output output_info
  #DeflateFilterNote Ratio ratio_info
  #LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
  #CustomLog logs/myapp_deflate_log deflate

myapp.conf :

<VirtualHost *:80>
  Include /opt/local/apache2/conf/myapp/myapp.common

  ErrorLog /path/to/logs/error.log
  CustomLog /path/to/logs/access.log combined
</VirtualHost>

myapp.cluster.conf :

<Proxy balancer://mongrel_cluster>
  BalancerMember http://127.0.0.1:8000
  BalancerMember http://127.0.0.1:8001
  BalancerMember http://127.0.0.1:8002
</Proxy>

Now that your files are created. at this to the bottom of /opt/local/apache2/conf/httpd.conf

NameVirtualHost *:80

Include conf/myapp/myapp.conf
Include conf/sites/myapp.cluster.conf

Apache is now ready to run.

Setting up Mongrel cluster

Create a new dir for mongrel cluster config

mkdir /opt/local/etc/mongrel
cd /opt/local/etc/mongrel

And create a new file “myapp.yml”

--- 
user: www
group: www
port: "8000"
cwd: /path/to/your/app/root
environment: production
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 3

Setting up Startup Scripts

Enable apache startup script

$ sudo vi /etc/hostconfig

#add the following lines

APACHE2=-YES-
MONGREL=-YES-

Copy the following into /Library/StartupItems/mongrel_cluster/mongrel_cluster

(adapted from original mongrel init script)
#!/bin/bash
#
# Copyright (c) 2006 Bradley Taylor, bradley@railsmachine.com
#
# mongrel_cluster       Startup script for Mongrel clusters.
#
# chkconfig: - 85 15
# description: mongrel_cluster manages multiple Mongrel processes for use \
#              behind a load balancer.
#              

[ -r "/etc/hostconfig" ] && . "/etc/hostconfig"

CONF_DIR="/opt/local/etc/mongrel"
RETVAL=0
ENABLE_FLAG=${MONGREL:=-NO-}

if [ "${ENABLE_FLAG}" = "-NO-" ]; then
        echo "Mongrel is disabled. see /etc/hostconfig"
        exit 0
fi

case "$1" in
    start)
      mongrel_cluster_ctl start -c $CONF_DIR
      RETVAL=$?
  ;;
    stop)
      mongrel_cluster_ctl stop -c $CONF_DIR
      RETVAL=$?
  ;;
    restart)
      mongrel_cluster_ctl restart -c $CONF_DIR
      RETVAL=$?
  ;;
    *)
      echo "Usage: mongrel_cluster {start|stop|restart}"
      exit 1
  ;;
esac      

exit $RETVAL

Ruby on Rails / Prototype : Shortcuts for javascript

Posted by Antonin AMAND Fri, 22 Sep 2006 12:05:00 GMT

If, as me, you use the prototype javascript library on top of all your scripts, you might want to use this shortcuts to save some time and increase readability of your code (But only if you know them ;) ).

Here is the list of shorcuts from Johnathan Tron’s blog

Ruby on Rails : Find with default order 1

Posted by Antonin AMAND Tue, 05 Sep 2006 12:56:00 GMT

I’ve found a solution (I am propably not the first ;-) ) for default ordering on ActiveRecord:Base children. The tip is to override the find method :


class Contact < ActiveRecord:Base

  def Contact.find(*args)
    if args[1] 
      args[1][:order] = "last_name, first_name" if args[1].is_a?(Hash) && !args[1][:order]
    else
      args[1] = {:order => "last_name, first_name"}
    end
    super(*args)
  end

end

If you have something cleaner please comment this post.