class Shoestring::Migration

Public Instance Methods

check() click to toggle source
# File lib/shoestring/migration.rb, line 7
def check
  Shoestring::Generic.check('Database Migrations') do
    status = db_status
    if $?.success?
      check_schema
      true
    else
      setup_database(status)
    end
  end
end

Private Instance Methods

check_schema() click to toggle source
# File lib/shoestring/migration.rb, line 21
def check_schema
  Shoestring::Cache.check(:schema) do |old_version|
    if File.exist?("db/schema.rb")
      version = File.readlines("db/schema.rb").find { |line| line.include?("define(:version") }
    else
      version = File.readlines("db/structure.sql").last
    end
    if old_version.to_s.strip != version.to_s.strip
      system("bundle exec rake db:migrate") || abort("bundle exec rake db:migrate failed")
    end
    version
  end
end
db_config() click to toggle source
# File lib/shoestring/migration.rb, line 49
def db_config
  @db_config ||= YAML.load(ERB.new(IO.read('./config/database.yml')).result)['development']
end
db_status() click to toggle source
# File lib/shoestring/migration.rb, line 53
def db_status
  if db_config['adapter'] == 'postgresql'
    ENV['PGPASSWORD'] = db_config['password']
    %x(psql -d #{db_config['database']} -U #{db_config['username']} -c "SELECT * from schema_migrations" 2>&1)
  else
    abort("#{db_config['adapter']} is not a support adapter")
  end
end
setup_database(status) click to toggle source
# File lib/shoestring/migration.rb, line 35
def setup_database(status)
  if status =~ /role.* does not exist/
    abort "Trouble connecting to database using the user #{db_config['username']}."
  elsif status =~ /database.* does not exist/
    puts "Unable to find the database #{db_config['database']}. Creating it now..."
    system("bundle exec rake db:setup") || abort("bundle exec rake db:setup failed")
  elsif status =~ /relation \"schema_migrations\" does not exist/
    puts "No migrations table. Creating it now..."
    system("bundle exec rake db:setup") || abort("bundle exec rake db:setup failed")
  else
    abort "Error determining the migration status: #{status}"
  end
end