Sha256: 3914da2336e7b6a58bd019fbdc91149f27abfe5ca2108f62ce457ae9cf8d7cb9

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

class PeakFlowUtils::DatabaseInitializerService < PeakFlowUtils::ApplicationService
  def execute
    path = File.realpath("#{File.dirname(__FILE__)}/../../../lib/peak_flow_utils/migrations")
    create_schema_table unless schema_table_exists?

    Dir["#{path}/[0-9]*_*.rb"].sort.map do |filename|
      match = filename.match(/migrations\/(\d+)_(.+)\.rb\Z/)
      next unless match

      version = match[1]
      next if version_migrated?(version)

      require filename
      migration = match[2].camelize.constantize
      migration.migrate(:up)
      register_migration_migrated(version)
    end

    succeed!
  end

private

  def create_schema_table
    PeakFlowUtils::ApplicationRecord.connection.execute("CREATE TABLE schema_migrations (version VARCHAT)")
  end

  def register_migration_migrated(version)
    PeakFlowUtils::ApplicationRecord.connection.execute("INSERT INTO schema_migrations (version) VALUES ('#{version}')")
  end

  def schema_table_exists?
    PeakFlowUtils::ApplicationRecord.connection.execute("SELECT * FROM sqlite_master WHERE type = 'table' AND name = 'schema_migrations'").any?
  end

  def version_migrated?(version)
    PeakFlowUtils::ApplicationRecord.connection.execute("SELECT * FROM schema_migrations WHERE version = '#{version}'").any?
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
peak_flow_utils-0.1.12 app/services/peak_flow_utils/database_initializer_service.rb
peak_flow_utils-0.1.11 app/services/peak_flow_utils/database_initializer_service.rb
peak_flow_utils-0.1.10 app/services/peak_flow_utils/database_initializer_service.rb
peak_flow_utils-0.1.9 app/services/peak_flow_utils/database_initializer_service.rb