Reputation: 840
I decided to create my first gem in ruby and I have a problem. If a user adds my gem to their gemfile, I would like to create a new table in the database.
I believe the only solution is to create a rake task which runs a custom script with the migration. Am I right? Do you know of any tutorials on modifying a database using plain Ruby? How do I create this script? Is there any other solution?
Upvotes: 0
Views: 1135
Reputation: 630
You can try something like this:
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: 'database.db'
ActiveRecord::Schema.define do
unless ActiveRecord::Base.connection.tables.include? 'tags'
create_table :tags do |table|
table.column :tag, :string
end
end
end
Of course, here I'm providing the direct attributes of the database, since this was just a small script for me. You can get it from the YAML or ENV variables, for example.
Upvotes: 7