Archive

Archive for February, 2010

How to make rails migrations pretty

February 26th, 2010 5 comments

I have seen a lot of migrations, most of them was just doing what it had to do. But I found a nice way to make my migrations more pretty.

So I had an Article model, which had field :published of type :boolean , as comparing to other solutions, I found that it should be rather :publish_date of type date.
First I have created migration using “script/generate migration article_update” and got empty migration:

class UpdateArticles < ActiveRecord::Migration
  def self.up
  end
  def self.down
  end
end
&#91;/sourcecode&#93;
To be able to freely play with Fields we add our private implementation of article model, we change the fields and move data for them:
&#91;sourcecode language='ruby'&#93;
class UpdateArticles < ActiveRecord::Migration
  class Article < ActiveRecord::Base; end
  def self.up
    add_column :articles, :publish_date, :datetime, :null => true
    Article.reset_column_information
    Article.all.each { |article| article.update_attribute(:publish_date, :created_at) if article.publish }
    remove_column  :articles, :publish
  end
  def self.down
    add_column  :articles, :publish, :boolean
    Article.reset_column_information
    Article.all.each { |article| article.update_attribute(:publish, true) unless article.publish_date.nil? }
    remove_column :articles, :publish_date
  end
end

But this is not really what we wanted, if we have already a lot of data in Article tables then we will not know what is going on during update attribute action.
To make it display pretty we have to add say_with_time function:

class UpdateArticles < ActiveRecord::Migration
  class Article < ActiveRecord::Base; end

  def self.up
    add_column :articles, :publish_date, :datetime, :null => true
    say_with_time "Updating publish_date column" do
      Article.reset_column_information
      Article.all.each { |article| article.update_attribute(:publish_date, :created_at) if article.publish }
    end
    remove_column  :articles, :publish
  end

  def self.down
    add_column  :articles, :publish, :boolean
    say_with_time "Updating publish column" do
      Article.reset_column_information
      Article.all.each { |article| article.update_attribute(:publish, true) unless article.publish_date.nil? }
    end
    remove_column :articles, :publish_date
  end
end

And after running migration we will get he following result:

==  UpdateArticles: migrating =================================================
-- add_column(:articles, :publish_date, :datetime, {:null=>true})
-> 0.0007s
-- Updating publish_date column
-> 0.0034s
-- remove_column(:articles, :publish)
-> 0.0224s
==  UpdateArticles: migrated (0.0269s) ========================================

For me it was very small amount of data and the migration went fast, but if i would run it on production with thousands of records then it may take a bit more.

I know this exact code could be rewritten using SQL – and it would be almost always done in seconds, but even then It is nice to show progress, because person responsible for deployment might be confused seeing only blinking cursor.

Did You liked this post or maybe not, vote on it at dzone.

Categories: Development Tags: ,

trac plugin – Dependency graph sitebar

February 13th, 2010 Comments off

After installation of dozens of plugins to trac I started wondering why people can not simplify their life ?

By ex. MasterTicketsPlugin can draw pretty graphs with great links to other tickets, but … you have to leave the ticket view to see the graph, So my thought was to have the graph on ticket view, as I already had TicketSidebarProviderPlugin I had *only* to embed output of one plugin into new sidebar.

As things look quite straight there were few gotchas which make the task spread to few hours, but finally I succeded and the result is already published at Trac-Hacks.orgDepgraphSidebarPlugin

opensuse 11.2 kde 3.5 is alive

February 4th, 2010 Comments off

in previous entry(opensuse 11.2 kde 3.5) I have described how it was possible to build my distro with opensuse 11.2 and kde 3.5,

right now I’m using my new distro :) and I’m quite happy with it, it’s almost ready to be used, only few changes needed to make it reproducible

I have found tutorial which describes how to use susestudio.com

I will try to release a dump of my installed apps so anybody can build his own (kde 3.5) distro but basing on my experience in this area :)

Categories: Linux Tags: , ,

opensuse 11.2 kde 3.5

February 3rd, 2010 2 comments

Did you ever dreamed of you own distribution, I know some of you did like me.
Now this becomes easily accessible to everybody with some knowledge what he wants.

Returning to theme of the article while searching for opensuse 11.2 and kde 3.5 I found worth trying one of novel services: http://susestudio.com

So gathering information from around the web and my previous trials of this configuration I did added plenty of repositories, my favorite packages to my new appliance … after some tries I was able to finally build my distribution, that’s quite orginall that I could build whole distro without using anything else then my browser.
But guess how I was surprised when under my fresh build distro I found link testdrive – I dont need anything then browser not only to create an distro, I can also test it within browser.
Ofcourse there are limitations of this site:

  • max 15GB can be stored
  • builds older then 7 days are automatically deleted
  • single testdrive can be maximally run for one hour

Does any of this limitations concern anybody? It is always possible to recreate deleted appliance, you can always launch new session of testdrive to get another hour, and 15GB – it’s a free of charge service
You can always download kiwi configuration and use it to build the distro on your machine.

Just to make it more real here is screenshot from my browser:
opensuse 11.2 kde 3.5
I’m really excited to test my new diostro on my new laptop tomorrow, probably I will write something more on it :)

Did You liked this post or maybe not, vote on it at dzone.

Categories: Linux Tags: , , ,