Created
June 15, 2010 19:27
-
-
Save blakesmith/439588 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Migration | |
class << self | |
def create_table(name, &block) | |
table = Table.new name | |
table.evaluate &block | |
table.create | |
end | |
def drop_table(name) | |
table = Table.new name | |
table.drop | |
end | |
end | |
end | |
class Table | |
def initialize(name) | |
@name = name | |
@columns = [] | |
end | |
def evaluate(&block) | |
instance_eval &block | |
end | |
def string(*columns) | |
@columns << columns | |
end | |
def create | |
#DB logic to create table here | |
"Table #{@name} created with columns #{@columns.join(', ')}" | |
end | |
def drop | |
#DB logic to remove table here | |
"Table #{@name} dropped from database" | |
end | |
end | |
class MyMigration < Migration | |
def self.up | |
create_table 'users' do | |
string 'username', 'password' | |
end | |
end | |
def self.down | |
drop_table 'users' | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment