Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Only run the initial migration for now
Also, add a test case for when migrating a pre-migration database
  • Loading branch information
sr committed Feb 12, 2009
1 parent d919834 commit 0b40dd0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
14 changes: 7 additions & 7 deletions lib/integrity/installer.rb
Expand Up @@ -21,7 +21,7 @@ def install(path)
"Checks the `database_uri` in CONFIG and creates and bootstraps a database for integrity"
def create_db(config, direction="up")
Integrity.new(config)
migrate_db(direction)
migrate_db(direction, 1)
end

desc "version",
Expand All @@ -33,15 +33,15 @@ def version
private
attr_reader :root

def migrate_db(direction="up")
def migrate_db(direction, level=nil)
require 'migrations'

set_up_migrations unless migrations_already_set_up?
add_initial_migration if tables_from_before_migrations_exist?

case direction.to_s
when "up" then Integrity::Migrations.migrate_up!
when "down" then Integrity::Migrations.migrate_down!
case direction
when "up" then Integrity::Migrations.migrate_up!(level)
when "down" then Integrity::Migrations.migrate_down!(level)
else raise ArgumentError, "DIRECTION must be either up or down"
end
end
Expand Down Expand Up @@ -106,8 +106,8 @@ def add_initial_migration
end

def tables_from_before_migrations_exist?
table_exists?("integrity_projects") &&
table_exists?("integrity_builds") &&
table_exists?("integrity_projects") &&
table_exists?("integrity_builds") &&
table_exists?("integrity_notifiers")
end

Expand Down
41 changes: 31 additions & 10 deletions test/acceptance/installer_test.rb
Expand Up @@ -9,27 +9,48 @@ class InstallerTest < Test::Unit::AcceptanceTestCase
Because I am lazy
EOS

def table_exists?(table_name)
database_adapter.storage_exists?(table_name)
end

def database_adapter
DataMapper.repository(:default).adapter
end

before do
@config_path = "/tmp/integrity-test.yml"
@database_path = "/tmp/integrity-test.db"

config = File.read(Integrity.root / "config/config.sample.yml")
config.gsub!(%r(sqlite3:///var/integrity.db), "sqlite3://#{@database_path}")
File.open(@config_path, "w") { |f| f << config }
end

after do
rm @database_path if File.exists?(@database_path)
end

scenario "running #create_db creates and migrates the database specified in config" do
scenario "Running #create_db for the first time" do
Installer.new.create_db(@config_path)

`echo .schema | sqlite3 #{@database_path}`.tap do |schema|
schema.should =~ /integrity_notifiers/
schema.should =~ /integrity_projects/
schema.should =~ /integrity_builds/
schema.should =~ /integrity_commits/
end
database_adapter.query("SELECT * from migration_info").
should == ["initial"]
assert table_exists?("migration_info")
assert table_exists?("integrity_projects")
assert table_exists?("integrity_builds")
assert table_exists?("integrity_notifiers")
assert !table_exists?("integrity_commits") # just to be sure :)
end

scenario "Running #migrate_db on a pre-migrations database" do
DataMapper.setup(:default, "sqlite3://:memory:")
DataMapper.auto_migrate!

assert table_exists?("integrity_projects")
assert table_exists?("integrity_builds")
assert table_exists?("integrity_notifiers")
assert !table_exists?("integrity_commits")
assert !table_exists?("migration_info")

Installer.new.send(:migrate_db, "up", 1)

assert table_exists?("migration_info")
end
end

0 comments on commit 0b40dd0

Please sign in to comment.