Class: Hanami::CLI::Commands::App::DB::Utils::Database Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/cli/commands/app/db/utils/database.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

Direct Known Subclasses

Mysql, Postgres, Sqlite

Constant Summary collapse

SCHEME_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

{
  "sqlite" => -> {
    require_relative("sqlite")
    Sqlite
  },
  "postgres" => -> {
    require_relative("postgres")
    Postgres
  },
  "postgresql" => -> {
    require_relative("postgres")
    Postgres
  },
  "mysql" => -> {
    require_relative("mysql")
    Mysql
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app:, config:) ⇒ Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Database.

Since:

  • 2.0.0



60
61
62
63
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 60

def initialize(app:, config:)
  @app = app
  @config = config
end

Instance Attribute Details

#appObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



14
15
16
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 14

def app
  @app
end

#configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



17
18
19
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 17

def config
  @config
end

Class Method Details

.[](app) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 40

def self.[](app)
  database_url =
    if app.key?(:settings) && app[:settings].respond_to?(:database_url)
      app[:settings].database_url
    else
      ENV.fetch("DATABASE_URL")
    end

  config = DatabaseConfig.new(database_url)

  resolver = SCHEME_MAP.fetch(config.db_type) do
    raise "#{config.db_type} is not a supported db scheme"
  end

  klass = resolver.()

  klass.new(app: app, config: config)
end

Instance Method Details

#applied_migrationsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



132
133
134
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 132

def applied_migrations
  sequel_migrator.applied_migrations
end

#connectionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



110
111
112
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 110

def connection
  gateway.connection
end

#create_commandObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 2.0.0



66
67
68
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 66

def create_command
  raise Hanami::CLI::NotImplementedError
end

#drop_commandObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 2.0.0



71
72
73
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 71

def drop_command
  raise Hanami::CLI::NotImplementedError
end

#dump_commandObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 2.0.0



76
77
78
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 76

def dump_command
  raise Hanami::CLI::NotImplementedError
end

#gatewayObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



105
106
107
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 105

def gateway
  rom_config.gateways[:default]
end

#load_commandObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 2.0.0



81
82
83
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 81

def load_command
  raise Hanami::CLI::NotImplementedError
end

#migratorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



123
124
125
126
127
128
129
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 123

def migrator
  @migrator ||=
    begin
      require "rom/sql"
      ROM::SQL::Migration::Migrator.new(connection, path: File.join(root_path, "db/migrate"))
    end
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



100
101
102
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 100

def name
  config.db_name
end

#rom_configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



91
92
93
94
95
96
97
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 91

def rom_config
  @rom_config ||=
    begin
      app.prepare(:persistence)
      app.container["persistence.config"]
    end
end

#root_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



86
87
88
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 86

def root_path
  app.root
end

#run_migrations(**options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



115
116
117
118
119
120
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 115

def run_migrations(**options)
  require "rom/sql"
  ROM::SQL.with_gateway(gateway) do
    migrator.run(options)
  end
end