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.
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
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.
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_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.
56 57 58 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 56 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.
55 56 57 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 55 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.
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.
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.
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_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.
136 137 138 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 136 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.
82 83 84 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 82 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.
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_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.
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.
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_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.
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_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.
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_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.
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_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_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.
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.
94 95 96 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 94 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.
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.
156 157 158 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 156 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.
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 |
#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.
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 |
#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.
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.
106 107 108 109 110 111 |
# File 'lib/hanami/cli/commands/app/db/utils/database.rb', line 106 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.
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_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.
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_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.
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 |