class Pakyow::Data::Migrator
Constants
- IVARS_TO_DISCONNECT
Public Class Methods
new(connection)
click to toggle source
# File lib/pakyow/data/migrator.rb, line 11 def initialize(connection) @connection = connection end
Private Class Methods
connect(adapter:, connection:, connection_overrides: {})
click to toggle source
# 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: {})
click to toggle source
# 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:)
click to toggle source
@api private
# File lib/pakyow/data/migrator.rb, line 161 def connect_raw(opts:, type:, name:) new(Connection.new(opts: opts, type: type, name: name)) end
globalize_connection_opts!(adapter, connection_opts)
click to toggle source
# File lib/pakyow/data/migrator.rb, line 167 def globalize_connection_opts!(adapter, connection_opts) migrator_for_adapter(adapter).globalize_connection_opts!(connection_opts) end
merge_connection_overrides!(connection_opts, connection_overrides)
click to toggle source
# File lib/pakyow/data/migrator.rb, line 171 def merge_connection_overrides!(connection_opts, connection_overrides) connection_overrides.each do |key, value| key = key.to_sym connection_opts[key] = if value.is_a?(Proc) value.call(connection_opts[key]) else value end end end
migrator_for_adapter(adapter, type = :Migrator)
click to toggle source
# File lib/pakyow/data/migrator.rb, line 113 def migrator_for_adapter(adapter, type = :Migrator) Adapters.const_get(Support.inflector.camelize(adapter)).const_get(type) end
Public Instance Methods
auto_migrate!()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 50 def auto_migrate! migrator.auto_migrate! end
create!()
click to toggle source
# 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!()
click to toggle source
# 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!()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 40 def drop! migrator.drop! end
finalize!()
click to toggle source
# 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!()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 44 def migrate! if migrations_to_run? runner.run! end end
Private Instance Methods
migration_path()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 102 def migration_path File.join(Pakyow.config.data.migration_path, "#{@connection.type}/#{@connection.name}") end
migrations()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 94 def migrations Dir.glob(File.join(migration_path, "*.rb")) end
migrations_to_run?()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 98 def migrations_to_run? migrations.count > 0 end
migrator()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 70 def migrator @migrator = self.class.migrator_for_adapter(Support.inflector.classify(@connection.type), :Migrator).new( @connection, sources: sources ) end
runner()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 76 def runner @runner = self.class.migrator_for_adapter(Support.inflector.classify(@connection.type), :Runner).new( @connection, migration_path ) end
sources()
click to toggle source
# File lib/pakyow/data/migrator.rb, line 82 def sources Pakyow.apps.reject(&:rescued?).flat_map { |app| app.data.containers.flat_map(&:sources).concat( app.plugs.flat_map { |plug| plug.data.containers.flat_map(&:sources) } ) }.select { |source| source.connection == @connection.name && source.adapter == @connection.type } end
track_exported_migrations() { || ... }
click to toggle source
# File lib/pakyow/data/migrator.rb, line 106 def track_exported_migrations initial_migrations = migrations yield migrations - initial_migrations end