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

Defined Under Namespace

Classes: DumpResult

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



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

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



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

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



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

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



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

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



47
48
49
50
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 47

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 52

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



114
115
116
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 114

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



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

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



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

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



168
169
170
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 168

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



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

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



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

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



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

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



118
119
120
121
122
123
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 118

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



186
187
188
189
190
191
192
193
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 186

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



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 134

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



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

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

#structure_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



182
183
184
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 182

def structure_sql_dump
  DumpResult.new(exec_dump_command, post_process: method(:post_process_dump))
end