Permalink
Browse files

active record stuff and tasks improved

  • Loading branch information...
1 parent d979acd commit b7ccdb5a3e366f3bc02c39e3609eb4ed7ef32ab5 @nu7hatch committed Jul 20, 2012
Showing with 150 additions and 9 deletions.
  1. +1 −0 Gemfile
  2. +27 −0 db/schema.rb
  3. +57 −0 lib/reusable/active_record_tasks.rb
  4. +54 −0 lib/tasks/db.rake
  5. +7 −5 lib/tasks/populate.rake
  6. +2 −4 spec/spec_helper.rb
  7. +2 −0 spec/support/jasmine.rb
View
@@ -23,6 +23,7 @@ gem "ffaker"
gem "rspec", "~> 2.4"
gem "mocha", "0.11.4"
gem 'fuubar'
+gem "jasmine"
group :mysql do
gem 'mysql2'
View
@@ -0,0 +1,27 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 3) do
+
+ create_table "signups", :force => true do |t|
+ t.string "email", :null => false
+ t.string "ip_address", :null => false
+ t.string "referer"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.float "lat"
+ t.float "lon"
+ t.string "city"
+ t.string "country"
+ end
+
+end
@@ -0,0 +1,57 @@
+module Reusable
+ # Public: Bunch of tasks to make work with ActiveRecord
+ # easier, kinda more similar to Rails, though.
+ module ActiveRecordTasks
+ extend self
+
+ def truncate
+ conn = ActiveRecord::Base.connection
+ tables = conn.execute("show tables").map { |r| r[0] }
+ tables.delete "schema_migrations"
+ tables.each { |t| conn.execute("TRUNCATE #{t}") }
+ end
+
+ def reset
+ conn = ActiveRecord::Base.connection
+ tables = conn.execute("show tables").map { |r| r[0] }
+ tables.each { |t| conn.execute("DROP TABLE #{t}") }
+ end
+
+ def migrate_reset(dir, verbose = true)
+ reset
+ migrate(dir, nil, verbose)
+ end
+
+ def migrate(dir, version, verbose = true)
+ self.verbose = verbose
+ ActiveRecord::Migrator.migrate(dir, version)
+ end
+
+ def rollback(dir, step, verbose = true)
+ self.verbose = verbose
+ ActiveRecord::Migrator.rollback(dir, step)
+ end
+
+ def schema_dump(file)
+ require 'active_record/schema_dumper'
+
+ File.open(file, "w") do |f|
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, f)
+ end
+ end
+
+ def schema_load(file, verbose = true)
+ $stdout = StringIO.new unless verbose
+ load(file).tap { $stdout = STDOUT }
+ end
+
+ def test_prepare(schema, verbose = false)
+ reset
+ schema_load(schema, verbose)
+ end
+
+ def verbose=(verbose)
+ ActiveRecord::Migration.verbose = verbose
+ end
+ end
+end
View
@@ -0,0 +1,54 @@
+require 'reusable/active_record_tasks'
+
+namespace :db do
+ task :paths do
+ $db_dir = File.join(ROOT_PATH, 'db')
+ $migrations_dir = File.join($db_dir, 'migrate')
+ end
+
+ desc "Truncate whole the database"
+ task :truncate => [:environment, :paths] do
+ Reusable::ActiveRecordTasks.truncate
+ end
+
+ desc "Delete all tables and reload schema"
+ task :reset => [:environment, :paths] do
+ Reusable::ActiveRecordTasks.reset
+ Rake::Task["db:schema:load"].invoke
+ end
+
+ desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
+ task :migrate => [:environment, :paths] do
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
+ Reusable::ActiveRecordTasks.migrate($migrations_dir, version)
+ Rake::Task["db:schema:dump"].invoke
+ end
+
+ namespace :migrate do
+ desc 'Resets database - cleans it up and re-runs migrations'
+ task :reset => [:environment, :paths] do
+ Reusable::ActiveRecordTasks.migrate_reset($migrations_dir)
+ end
+ end
+
+ desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
+ task :rollback => [:environment, :paths] do
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
+ Reusable::ActiveRecordTasks.rollback($migrations_dir, step)
+ Rake::Task["db:schema:dump"].invoke
+ end
+
+ namespace :schema do
+ desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
+ task :dump => [:environment, :paths] do
+ file = ENV['SCHEMA'] || File.join($db_dir, 'schema.rb')
+ Reusable::ActiveRecordTasks.schema_dump(file)
+ end
+
+ desc "Load a ar_schema.rb file into the database"
+ task :load => :environment do
+ file = ENV['SCHEMA'] || File.join($db_dir, 'schema.rb')
+ Reusable::ActiveRecordTasks.schema_load(file)
+ end
+ end
+end
View
@@ -1,10 +1,12 @@
-desc "Populates dummy data to the database"
-task :populate => :'db:reset' do
- require File.expand_path('../../../apps/core/spec/support/blueprints', __FILE__)
+namespace :db do
+ desc "Populates dummy data to the database"
+ task :populate => :'db:reset' do
+ require File.expand_path('../../../apps/core/spec/support/blueprints', __FILE__)
- include Populate
+ include Populate
- populate_signups!
+ populate_signups!
+ end
end
module Populate
View
@@ -7,10 +7,8 @@
require 'airstrip'
# Flush database and re-run migrations.
-migrations = File.expand_path('../../db/migrate', __FILE__)
-ActiveRecord::Migration.verbose = false
-ActiveRecord::Migrator.migrate(migrations, 0)
-ActiveRecord::Migrator.migrate(migrations, nil)
+require 'reusable/active_record_tasks'
+Reusable::ActiveRecordTasks.test_prepare(File.join(ROOT_PATH, 'db/schema.rb'))
# Load extra spec helpers and support files.
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each do |helper|
View
@@ -0,0 +1,2 @@
+require 'jasmine'
+

0 comments on commit b7ccdb5

Please sign in to comment.