Skip to content

Commit

Permalink
Merge pull request sinatra-activerecord#11 from zenbaku/master
Browse files Browse the repository at this point in the history
Adding schema load and dump tasks
  • Loading branch information
janko committed Jun 16, 2013
2 parents cd73458 + cbf4148 commit a4159c8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source 'https://rubygems.org'

gemspec

Expand Down
18 changes: 18 additions & 0 deletions lib/sinatra/activerecord/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ def rollback(step = nil)
end
end

def dump_schema(file_name = 'db/schema.rb')
silence_activerecord do
ActiveRecord::Migration.suppress_messages do
# Create file
out = File.new(file_name, 'w')

# Load schema
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, out)

out.close
end
end
end

def load_schema(file_name = 'db/schema.rb')
load(file_name)
end

private

def migrations_dir
Expand Down
14 changes: 14 additions & 0 deletions lib/sinatra/activerecord/tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@ namespace :db do
desc "migrate the database (use version with VERSION=n)"
task :migrate do
Sinatra::ActiveRecordTasks.migrate(ENV["VERSION"])
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end

desc "roll back the migration (use steps with STEP=n)"
task :rollback do
Sinatra::ActiveRecordTasks.rollback(ENV["STEP"])
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end

namespace :schema do
desc "dump schema into file"
task :dump do
Sinatra::ActiveRecordTasks.dump_schema()
end

desc "load schema into database"
task :load do
Sinatra::ActiveRecordTasks.load_schema()
end
end
end
53 changes: 53 additions & 0 deletions spec/sinatra/activerecord/rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,57 @@ def schema_version
expect { rollback(2) }.to change{schema_version}.to(0)
end
end

describe 'db:schema' do
def schema_file
'foo_schema.rb'
end

after(:each) do
File.delete(schema_file)
end

describe "db:schema:dump" do
it "should dump the schema for the current database state" do
dump_schema(schema_file)
File.exists?(schema_file).should be_true
end
end

describe "db:schema:load" do
it "should load the schema file " do
# Create some tables
ActiveRecord::Migration.class_eval do
create_table :posts do |t|
t.string :title
t.text :body
end

create_table :people do |t|
t.string :first_name
t.string :last_name
t.string :short_name
end
end

# Dump file
dump_schema(schema_file)

# Drop table
begin
ActiveRecord::Schema.drop_table('posts')
ActiveRecord::Schema.drop_table('people')
rescue
nil
end

# Load schema
load_schema(schema_file)

# Expect tables to be there
ActiveRecord::Base.connection.table_exists?('posts').should be_true
ActiveRecord::Base.connection.table_exists?('people').should be_true
end
end
end
end
4 changes: 3 additions & 1 deletion spec/sinatra/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require 'sinatra/activerecord'

describe "the sinatra extension" do
let(:database_url) { "sqlite3:///tmp/foo.sqlite3" }
def database_url
"sqlite3:///tmp/foo.sqlite3"
end

def new_sinatra_application
Class.new(Sinatra::Base) do
Expand Down

0 comments on commit a4159c8

Please sign in to comment.