Class: Pakyow::Data::Migrator
- Inherits:
-
Object
- Object
- Pakyow::Data::Migrator
- Defined in:
- lib/pakyow/data/migrator.rb
Constant Summary collapse
- IVARS_TO_DISCONNECT =
%i(@runner @migrator).freeze
Class Method Summary collapse
- .connect(adapter:, connection:, connection_overrides: {}) ⇒ Object
- .connect_global(adapter:, connection:, connection_overrides: {}) ⇒ Object
- .connect_raw(opts:, type:, name:) ⇒ Object private
- .migrator_for_adapter(adapter, type = :Migrator) ⇒ Object
Instance Method Summary collapse
- #auto_migrate! ⇒ Object
- #create! ⇒ Object
- #disconnect! ⇒ Object
- #drop! ⇒ Object
- #finalize! ⇒ Object
-
#initialize(connection) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate! ⇒ Object
Constructor Details
#initialize(connection) ⇒ Migrator
Returns a new instance of Migrator.
11 12 13 |
# File 'lib/pakyow/data/migrator.rb', line 11 def initialize(connection) @connection = connection end |
Class Method Details
.connect(adapter:, connection:, connection_overrides: {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/pakyow/data/migrator.rb', line 117 def connect(adapter:, connection:, connection_overrides: {}) adapter = if adapter adapter.to_sym else Pakyow.config.data.default_adapter end connection = if connection connection.to_sym else Pakyow.config.data.default_connection end connection_opts = Connection.parse_connection_string( Pakyow.config.data.connections.send(adapter)[connection] ) merge_connection_overrides!(connection_opts, connection_overrides) connect_raw(opts: connection_opts, type: adapter, name: connection) end |
.connect_global(adapter:, connection:, connection_overrides: {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/pakyow/data/migrator.rb', line 138 def connect_global(adapter:, connection:, connection_overrides: {}) adapter = if adapter adapter.to_sym else Pakyow.config.data.default_adapter end connection = if connection connection.to_sym else Pakyow.config.data.default_connection end connection_opts = Connection.parse_connection_string( Pakyow.config.data.connections.send(adapter)[connection] ) merge_connection_overrides!(connection_opts, connection_overrides) globalize_connection_opts!(adapter, connection_opts) connect_raw(opts: connection_opts, type: adapter, name: connection) end |
.connect_raw(opts:, type:, 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.
161 162 163 |
# File 'lib/pakyow/data/migrator.rb', line 161 def connect_raw(opts:, type:, name:) new(Connection.new(opts: opts, type: type, name: name)) end |
Instance Method Details
#auto_migrate! ⇒ Object
50 51 52 |
# File 'lib/pakyow/data/migrator.rb', line 50 def auto_migrate! migrator.auto_migrate! end |
#create! ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pakyow/data/migrator.rb', line 27 def create! migrator.create! disconnect! # Recreate the connection, since we just created the database it's supposed to connect to. # @connection = Connection.new( opts: @connection.opts, type: @connection.type, name: @connection.name ) end |
#disconnect! ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/pakyow/data/migrator.rb', line 17 def disconnect! IVARS_TO_DISCONNECT.each do |ivar| if instance_variable_defined?(ivar) && value = instance_variable_get(ivar) value.disconnect! end end @connection.disconnect end |
#drop! ⇒ Object
40 41 42 |
# File 'lib/pakyow/data/migrator.rb', line 40 def drop! migrator.drop! end |
#finalize! ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pakyow/data/migrator.rb', line 54 def finalize! migrator.finalize!.each do |filename, content| FileUtils.mkdir_p(migration_path) File.open(File.join(migration_path, filename), "w+") do |file| file.write <<~CONTENT Pakyow.migration do #{content.to_s.split("\n").map { |line| " #{line}" }.join("\n")} end CONTENT end end end |
#migrate! ⇒ Object
44 45 46 47 48 |
# File 'lib/pakyow/data/migrator.rb', line 44 def migrate! if migrations_to_run? runner.run! end end |