More Migration Sexiness

After being inspired by errtheblog’s sexy migrations (
http://errtheblog.com/posts/51-sexy-migrations) I got tired of writing
my
un-sexy migrations containing line after line of remove_column and its
alter-ego add_column.

So I made two new methods, add_columns and remove_columns.

http://frozenplague.net/?p=102

Repetition no more!


Ryan B.
http://www.frozenplague.net

Ryan B. wrote:

After being inspired by errtheblog’s sexy migrations (
http://errtheblog.com/posts/51-sexy-migrations) I got tired of writing
my
un-sexy migrations containing line after line of remove_column and its
alter-ego add_column.

So I made two new methods, add_columns and remove_columns.

http://frozenplague.net/?p=102

Repetition no more!


Ryan B.
http://www.frozenplague.net

doesn’t Rails 2.0 already do something like this with

t.string :column_one, :column_two, :ect

?

That’s for when you’re creating a table. This is for after the table has
been created and you want to change it.

On Dec 19, 2007 10:49 AM, Jeremy W.
[email protected]
wrote:

http://frozenplague.net/?p=102

?

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

Ryan B. wrote:

That’s for when you’re creating a table. This is for after the table has
been created and you want to change it.

On Dec 19, 2007 10:49 AM, Jeremy W.
[email protected]
wrote:

http://frozenplague.net/?p=102

?

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

Hi Ryan,

This feature is now part of rails 2.0 :slight_smile:

http://ryandaigle.com/articles/2007/5/6/what-s-new-in-edge-rails-bringin-sexy-back

That’s not the same as what he’s got here…

I think this is pretty neat. I like remove_columns especially. I
don’t know how much I’d really use add_columns, but having it like
that would be nice just in case. :slight_smile:

–Jeremy

On Dec 18, 2007 10:46 PM, Mohan K. [email protected]
wrote:

http://www.frozenplague.net


http://www.jeremymcanally.com/

My books:
Ruby in Practice

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/

I like it but I am surprised no one has ever made a syntax for adding
and removing columns that matches create table. To me, the following
would be ideal:

add_columns “users” do |t|
t.integer :group_id, :employer_id
t.string :first, :last
t.timestamps
end

remove_columns “users” do |t|
t.integer :group_id, :employer_id
t.string :first, :last
t.timestamps
end

Frankly this would be the most intuitive and should be part of the rails
core migrations system.

Thank you Jeremy M.

Mohan, as I explained before it’s not replicating what is already in
Rails
2.0, as that is for when you’re creating tables.

This is for when you’ve already created the tables and want to do more
to
them.

On Dec 19, 2007 2:31 PM, Jeremy McAnally [email protected]
wrote:

wrote:

http://frozenplague.net/?p=102
Ryan B.

Ruby in Practice


Ryan B.
http://www.frozenplague.net

Why not DRY it up even more?

columns “users” do |t|
t.integer :group_id, :employer_id
t.string :first, :last
t.timestamps
end

You know exactly which columns are being added. Reversing the migration
should be able to use that structure to automagically remove the
columns.

The catch would be if there is additional processing occuring during the
migration which would need to be reversed during the back-out.

cheers

^^ Point taken. Still, doesn’t this syntax just look natural?

Being able to specify it in a block form would be wonderful.

Unfortunately this is beyond my scope of knowledge.

If anyone would like to do that and post it here I would be very
thankful.

On Dec 19, 2007 3:39 PM, Nathan E.
[email protected]
wrote:

^^ Point taken. Still, doesn’t this syntax just look natural?

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

I probably won’t take the time to code this up but in case someone else
here is eager to do this, I can easily show you where to start. The
funny thing is that it would actually be ridiculously easy.

First start here:

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M001150

Here is the (abridged for relevance) code for “create_table”:

def create_table(table_name, options = {})

instantiate a “TableDefinition” class

table_definition = TableDefinition.new(self)
#…set the tables “id”…
#…

yield the instance to the user (let them make calls to the object)

yield table_definition

#make the sql based on the columns added to the instance
create_sql = #… turn the table_definition into SQL
#run the sql that was built based on the columns stored in the
instance
execute create_sql
end

This is actually not very complicated at all. Simply “yielding” to the
code passed into the block so:

create_table “examples” do |t|
t.column :example, :integer
end

simply calls the “column” function on the “t” variable which is actually
an instance of TableDefinition

So, what is TableDefinition?

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html

Just a class with some basic functions that adds columns to an instance
variable array and has a method for turning the columns array into sql.

That is basically the jist of it. I leave actually coding it as an
exercise to the reader, but I guarantee that it is doable.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs