InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Simplify SQL Migration Scripts with SQrbL

Posted by Sebastien Auvray on Aug 24, 2009

Sections
Architecture & Design,
Development,
Operations & Infrastructure
Topics
Ruby ,
Database Design
Tags
ActiveRecord ,
SQL ,
migration

Managing SQL-based script can become a nightmare with time. Rails solved this with ActiveRecord Migration. Sam Livingston-Gray wrote a small standalone Ruby tool to generate hierarchical migration script. Based on the fact that SQL scripts can become very verbose and duplication-prone, Sam started SQrbL which is a mix of SQL and Ruby.

You'll be writing such script:

include Sqrbl

Sqrbl.migration do
    @output_directory='/path/to/generated/sql'

    group "Widgets" do
      step "Create widgets" do
        up do
       
          helpers do
            def widget_import_note
                '"Imported from old_widgets"'
            end
          end
       
          action "Migrate old_widgets" do
            <<-SQL
              #{
                insert_into("new_widgets", {
                  :name     => 'widget_name',
                  :part_num => 'CONCAT("X_", part_number)',
                  :note     => widget_import_note,
                })
              }
              FROM old_widgets
            SQL
          end
        end

        down do
          action "Drop imported organizational contacts" do
            'DELETE FROM new_widgets WHERE note LIKE "Imported from old_widgets"'
          end
        end
      end
    end   
   
    group 'Second Group' do
      step 'Step one' do
        up { write '-- Step one up' }
        down { write '-- Step one down' }
      end
      step 'Step two' do
        up { write '-- Step two up' }
        down { write '-- Step two down'}
      end
    end       
       
end

And SQrbL will produce the following files:

/path/to/generated/sql/up/1_widgets/1_create_widgets.sql
/path/to/generated/sql/down/1_widgets/1_create_widgets.sql
/path/to/generated/sql/up/2_second_group/1_step_one.sql
/path/to/generated/sql/down/2_second_group/1_step_one.sql
/path/to/generated/sql/up/2_second_group/2_step_two.sql
/path/to/generated/sql/down/2_second_group/2_step_two.sql
/path/to/generated/sql/all_up.sql
/path/to/generated/sql/all_down.sql

For example all_up.sql is filled up with the SQL queries:

-- Migrate old_widgets

INSERT INTO new_widgets (
    name,
    part_num,
    note
)
SELECT
    widget_name AS name,
    CONCAT("X_", part_number) AS part_num,
    "Imported from old_widgets" AS note
FROM old_widgets

-- Step one up

-- Step two up

For the moment SQrbL only insert_into to simplify INSERT writing statements.

While the use of SQrbL might not look the best solution for people already using ActiveRecord Migration, it still might satisfy people looking for a quick simple standalone tool. For the moment SQrbL is in its 0.1.3 version and is lacking a proper SQL DSL.

No comments

Watch Thread Reply

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.