Class: Brut::CLI::Apps::DB::Status

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/db.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #args_description, #argv, #commands, #default_command, #default_command_class, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#bootstrap?Boolean

Returns:

  • (Boolean)


15
# File 'lib/brut/cli/apps/db.rb', line 15

def bootstrap? = false

#default_rack_envObject



14
# File 'lib/brut/cli/apps/db.rb', line 14

def default_rack_env = "development"

#descriptionObject



13
# File 'lib/brut/cli/apps/db.rb', line 13

def description = "Check the status of the database and migrations"

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/brut/cli/apps/db.rb', line 17

def run
  database_name = URI(Brut.container.database_url).path.gsub(/^\//,"")
  connection = Brut.container.sequel_db_handle
  stdout.puts "Database server is up"
  stdout.puts "Database #{database_name} exists"
  migrations_run = if connection.table_exists?("schema_migrations")
                     connection["select filename from schema_migrations order by filename"].all.map { |_| _[:filename] }
                   else
                     []
                   end
  migration_files = Dir[Brut.container.migrations_dir / "*.rb"].map { |file|
    filename = Pathname(file).basename.to_s
  }
  if migration_files.empty? && migrations_run.empty?
    stdout.puts("✅ NO MIGRATION FILES TO RUN")
  else
    max_length = migration_files.map(&:length).max
    printf_string = "%-#{max_length}s - %s\n"
    migration_files.each do |filename|
      applied = migrations_run.include?(filename)
      stdout.printf(printf_string,filename,applied ? "✅ APPLIED" : "❌ NOT APPLIED")
    end
  end
  0
rescue Sequel::DatabaseConnectionError => ex
  uri_no_database = URI(Brut.container.database_url.to_s)
  database_name = uri_no_database.path.gsub(/^\//,"")
  uri_no_database.path = ""
  begin
    connection = Sequel.connect(uri_no_database.to_s)
    stdout.puts "Database server is up"
    stdout.puts "Database #{database_name} does not exist - run `brut db create` to create it"
    0
  rescue => ex2
    stderr.puts "Database server is not running at #{uri_no_database}: #{ex2.message}"
    stderr.puts "This could be a problem with your dev environment generally, or your .env.test or .env.test.local files"
    1
  end
end