how to undo script/generate migration
September 17th, 2007
Adding type to a table in migrations
August 28th, 2007
script/generate migration add_type_to_users
in my terminal and it automatically generated a migration with the code generated for me.
1 2 3 4 5 6 7 8 9 |
class AddTypeToUsers < ActiveRecord::Migration def self.up add_column :users, :type, :type, :null => :no?, :default => :maybe? end def self.down remove_column :users, :type end end |
Am I being n00b, or is this a new feature in edge rails? Does this work for columns not reserved for rails?
UPDATE:
On further inspection, this is a new thing in edge rails and it does work for other columns. While running the following:
script/generate migration add_name_to_users
I got the following migration:
1 2 3 4 5 6 7 8 9 |
class AddNameToUsers < ActiveRecord::Migration def self.up add_column :users, :name, :type, :null => :no?, :default => :maybe? end def self.down remove_column :users, :name end end |
script/generate migration add_name_string_to_users
1 2 3 4 5 6 7 8 9 |
class AddNameStringToUsers < ActiveRecord::Migration def self.up add_column :users, :name_string, :type, :null => :no?, :default => :maybe? end def self.down remove_column :users, :name_string end end |
script/generate migration add_name_and_date_to_users
and got the following:
class AddNameAndDateToUsers < ActiveRecord::Migration
def self.up
add_column :users, :name_and_date, :type, :null => :no?, :default => :maybe?
end
def self.down
remove_column :users, :name_and_date
end
end
It seems that the implementation is pretty primitive. Obviously I'm using a bad approach as I should be looking at the source to see if any additional implementation does exist without my knowing, but I was hoping that the most intuitive thing for me would implemented already. I think it would be cool to see this syntactic sugar get more advanced. Comments? Opinions?
NOTE: The code that is generated by the migration does not work automatically. You have to change the stubbed symbols placed inside the migration to whatever you need. It's more of a guide than a performance enhancer (which IMO think that it should be both).
Importing PHPMyAdmin export files using migrations
August 27th, 2007
1 2 3 4 5 6 7 8 9 10 |
def self.up @first_query = true sql_file = File.dirname(__FILE__) + "/../some_crazy_file.sql" IO.readlines(sql_file).join.split(/;\nINSERT INTO /).each do |q| q = "INSERT INTO " + q unless @first_query execute q @first_query = false end end |
UPDATE: My friend Brandon, who chose not to comment on this blog post (shame on him!) said that running the sql file in command line could work as a more elegant solution there. The only problem I have with this approach is whether or not the rake task fails if the operation fails. So you code would like like this:
1 2 3 |
def self.up result = `mysql db_name < some_crazy_file.sql` end |
The only thing left is to make sure that result returns a favorable result, and if it doesn't then fail the migration. I'm not exactly sure how to do this (to busy to investigate). Any takers?