Skip to content

Instantly share code, notes, and snippets.

@zealot128
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zealot128/9554027 to your computer and use it in GitHub Desktop.
Save zealot128/9554027 to your computer and use it in GitHub Desktop.
ActiveRecord Sqlite auto connection + migration without Rails in one file (AR 4)
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