user2239655
user2239655

Reputation: 840

Create migration in gem

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

Answers (1)

Thyago B. Rodrigues
Thyago B. Rodrigues

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

Related Questions