Last active
August 29, 2015 13:57
-
-
Save zealot128/9554027 to your computer and use it in GitHub Desktop.
ActiveRecord Sqlite auto connection + migration without Rails in one file (AR 4)
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
require 'active_record' | |
require 'fileutils' | |
ActiveRecord::Base.logger = Logger.new(STDERR) | |
db = "db/#{ENV['RACK_ENV']}.sqlite3" | |
if not File.exists? db | |
`sqlite3 #{db} '' ` | |
end | |
ActiveRecord::Base.establish_connection( | |
:adapter => "sqlite3", | |
:database => db | |
) | |
class FeedMigration < ActiveRecord::Migration | |
def change | |
create_table :feeds do |t| | |
t.string :url | |
t.string :title | |
t.datetime :lastmodified | |
t.string :etag | |
t.timestamps | |
end | |
end | |
end | |
migrations = [FeedMigration] | |
migrations = migrations.each_with_index.map do |m,i| | |
proxy = ActiveRecord::MigrationProxy.new(m.to_s, i.to_s, '', '') | |
proxy.define_singleton_method(:load_migration, -> { m }) | |
proxy | |
end | |
ActiveRecord::Migrator.new(:up, migrations, nil).migrate | |
class Feed < ActiveRecord::Base | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment