Skip to content

Commit

Permalink
Merge pull request #53 from mogotest/no_migrations_fix
Browse files Browse the repository at this point in the history
Allow dumping the schema even if there aren't any migrations to run.
  • Loading branch information
JonathanTron committed Oct 6, 2013
2 parents 2e3cfc9 + f59ae4b commit 6c3fce6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
41 changes: 26 additions & 15 deletions lib/sequel_rails/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,46 @@ class << self
def migrate(version=nil)
opts = {}
opts[:target] = version.to_i if version
::Sequel::Migrator.run(::Sequel::Model.db, Rails.root.join("db/migrate"), opts)
::Sequel::Migrator.run(::Sequel::Model.db, migrations_dir, opts)
end
alias_method :migrate_up!, :migrate
alias_method :migrate_down!, :migrate

def pending_migrations?
return false unless File.exists?(Rails.root.join("db/migrate"))
!::Sequel::Migrator.is_current?(::Sequel::Model.db, Rails.root.join("db/migrate"))
return false unless available_migrations?
!::Sequel::Migrator.is_current?(::Sequel::Model.db, migrations_dir)
end

def dump_schema_information(opts={})
sql = opts.fetch :sql
db = ::Sequel::Model.db
migrations_dir = Rails.root.join("db/migrate")
migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
migrator = migrator_class.new db, migrations_dir

inserts = migrator.ds.map do |hash|
insert = migrator.ds.insert_sql(hash)
sql ? "#{insert};" : " self << #{insert.inspect}"
end
res = ""
if inserts.any?
res << "Sequel.migration do\n change do\n" unless sql
res << inserts.join("\n")
res << "\n end\nend\n" unless sql

if available_migrations?
migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
migrator = migrator_class.new db, migrations_dir

inserts = migrator.ds.map do |hash|
insert = migrator.ds.insert_sql(hash)
sql ? "#{insert};" : " self << #{insert.inspect}"
end

if inserts.any?
res << "Sequel.migration do\n change do\n" unless sql
res << inserts.join("\n")
res << "\n end\nend\n" unless sql
end
end
res
end

def migrations_dir
Rails.root.join("db/migrate")
end

def available_migrations?
File.exists?(migrations_dir) && Dir[File.join(migrations_dir, '*')].any?
end
end
end
end
12 changes: 10 additions & 2 deletions spec/lib/sequel_rails/migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@
described_class.pending_migrations?.should == false
end

context "when db/migrate directory exists" do
before { FileUtils.mkdir_p path }
it "returns false if db/migrate directory exists, but is empty" do
FileUtils.mkdir_p path
described_class.pending_migrations?.should == false
end

context "when db/migrate directory exists and contains migrations" do
before do
FileUtils.mkdir_p path
FileUtils.touch(File.join(path, 'test_migration.rb'))
end

it "returns true if any pending migration" do
::Sequel::Migrator.should_receive(:is_current?).with(
Expand Down

0 comments on commit 6c3fce6

Please sign in to comment.