Class: Hanami::CLI::Commands::App::DB::Utils::Database Private
- Inherits:
-
Object
- Object
- Hanami::CLI::Commands::App::DB::Utils::Database
- 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.
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.
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
- #gateway_name ⇒ Object readonly private
- #slice ⇒ Object readonly private
- #system_call ⇒ Object readonly private
Class Method Summary collapse
Instance Method Summary collapse
- #applied_migrations ⇒ Object private
- #connection ⇒ Object private
- #database_uri ⇒ Object private
- #database_url ⇒ Object private
- #db_config_dir? ⇒ Boolean private
- #db_config_path ⇒ Object private
- #exec_create_command ⇒ Object private
- #exec_drop_command ⇒ Object private
- #exec_dump_command ⇒ Object private
- #exec_load_command ⇒ Object private
- #exists? ⇒ Boolean private
- #gateway ⇒ Object private
-
#initialize(slice:, gateway_name:, system_call:) ⇒ Database
constructor
private
A new instance of Database.
- #migrations_dir? ⇒ Boolean private
- #migrations_path ⇒ Object private
- #migrator ⇒ Object private
- #name ⇒ Object private
- #run_migrations(**options) ⇒ Object private
- #schema_migrations_sql_dump ⇒ Object private
- #sequel_migrator ⇒ Object private
- #structure_file ⇒ Object private
- #structure_sql_dump ⇒ Object private
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.
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_name ⇒ Object (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.
68 69 70 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 68 def gateway_name @gateway_name end |
#slice ⇒ Object (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.
67 68 69 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 67 def slice @slice end |
#system_call ⇒ Object (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.
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.
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.
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_migrations ⇒ 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.
148 149 150 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 148 def applied_migrations sequel_migrator.applied_migrations end |
#connection ⇒ 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.
94 95 96 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 94 def connection gateway.connection end |
#database_uri ⇒ 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.
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_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.
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.
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_path ⇒ 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.
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_command ⇒ 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.
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_command ⇒ 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.
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_command ⇒ 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.
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_command ⇒ 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.
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.
106 107 108 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 106 def exists? raise Hanami::CLI::NotImplementedError end |
#gateway ⇒ 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.
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.
168 169 170 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 168 def migrations_dir? migrations_path.directory? end |
#migrations_path ⇒ 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.
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 |
#migrator ⇒ 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.
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 |
#name ⇒ 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.
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.
118 119 120 121 122 123 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 118 def run_migrations(**) require "rom/sql" ROM::SQL.with_gateway(gateway) do migrator.run() end end |
#schema_migrations_sql_dump ⇒ 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.
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_migrator ⇒ 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.
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_file ⇒ 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.
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_dump ⇒ 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.
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 |