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.2.0

Direct Known Subclasses

Mysql, Postgres, Sqlite

Constant Summary collapse

DATABASE_CLASS_RESOLVER =

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.2.0

Hash.new { |_, key|
  raise "#{key} is not a supported db scheme"
}.update(
  "sqlite" => -> {
    require_relative("sqlite")
    Sqlite
  },
  "postgres" => -> {
    require_relative("postgres")
    Postgres
  },
  "postgresql" => -> {
    require_relative("postgres")
    Postgres
  },
  "mysql2" => -> {
    require_relative("mysql")
    Mysql
  }
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slice:, gateway_name:, system_call:) ⇒ 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.2.0



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

def initialize(slice:, gateway_name:, system_call:)
  @slice = slice
  @gateway_name = gateway_name
  @system_call = system_call
end

Instance Attribute Details

#gateway_nameObject (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.2.0



56
57
58
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 56

def gateway_name
  @gateway_name
end

#sliceObject (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.2.0



55
56
57
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 55

def slice
  @slice
end

#system_callObject (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.2.0



58
59
60
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 58

def system_call
  @system_call
end

Class Method Details

.database_class(database_url) ⇒ 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.2.0



35
36
37
38
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 35

def self.database_class(database_url)
  database_scheme = URI(database_url).scheme
  DATABASE_CLASS_RESOLVER[database_scheme].call
end

.from_slice(slice:, system_call:) ⇒ 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.2.0



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

def self.from_slice(slice:, system_call:)
  provider = slice.container.providers[:db]
  raise "No :db provider for #{slice}" unless provider

  provider.source.database_urls.map { |(gateway_name, database_url)|
    database = database_class(database_url).new(
      slice: slice,
      gateway_name: gateway_name,
      system_call: system_call
    )

    [gateway_name, database]
  }.to_h
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.2.0



136
137
138
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 136

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.2.0



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

def connection
  gateway.connection
end

#database_uriObject

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.2.0



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

def database_uri
  @database_uri ||= URI(database_url)
end

#database_urlObject

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.2.0



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

def database_url
  slice.container.providers[:db].source.database_urls.fetch(gateway_name)
end

#db_config_dir?Boolean

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:

  • (Boolean)

Since:

  • 2.2.0



144
145
146
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 144

def db_config_dir?
  db_config_path.directory?
end

#db_config_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.2.0



140
141
142
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 140

def db_config_path
  slice.root.join("config", "db")
end

#exec_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.2.0



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

def exec_create_command
  raise Hanami::CLI::NotImplementedError
end

#exec_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.2.0



90
91
92
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 90

def exec_drop_command
  raise Hanami::CLI::NotImplementedError
end

#exec_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.2.0



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

def exec_dump_command
  raise Hanami::CLI::NotImplementedError
end

#exec_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.2.0



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

def exec_load_command
  raise Hanami::CLI::NotImplementedError
end

#exists?Boolean

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:

  • (Boolean)

Raises:

Since:

  • 2.2.0



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

def exists?
  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.2.0



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

def gateway
  slice["db.config"].gateways[gateway_name]
end

#migrations_dir?Boolean

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:

  • (Boolean)

Since:

  • 2.2.0



156
157
158
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 156

def migrations_dir?
  migrations_path.directory?
end

#migrations_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.2.0



148
149
150
151
152
153
154
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 148

def migrations_path
  if gateway_name == :default
    db_config_path.join("migrate")
  else
    db_config_path.join("#{gateway_name}_migrate")
  end
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.2.0



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

def migrator
  @migrator ||= begin
    slice.prepare :db

    require "rom/sql"
    ROM::SQL::Migration::Migrator.new(connection, path: migrations_path)
  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.2.0



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

def name
  database_uri.path.sub(%r{^/}, "")
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.2.0



106
107
108
109
110
111
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 106

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

#schema_migrations_sql_dumpObject

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.2.0



170
171
172
173
174
175
176
177
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 170

def schema_migrations_sql_dump
  return unless migrations_dir?

  sql = +"INSERT INTO schema_migrations (filename) VALUES\n"
  sql << applied_migrations.map { |v| "('#{v}')" }.join(",\n")
  sql << ";"
  sql
end

#sequel_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.2.0



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 122

def sequel_migrator
  @sequel_migrator ||= begin
    slice.prepare :db

    require "sequel"
    Sequel.extension :migration

    require "rom/sql"
    ROM::SQL.with_gateway(gateway) do
      Sequel::TimestampMigrator.new(migrator.connection, migrations_path, {})
    end
  end
end

#structure_fileObject

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.2.0



160
161
162
163
164
165
166
167
168
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 160

def structure_file
  path = slice.root.join("config", "db")

  if gateway_name == :default
    path.join("structure.sql")
  else
    path.join("#{gateway_name}_structure.sql")
  end
end