class Lexicon::Cli::Command::ProductionCommand

Public Instance Methods

config() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 26
        def config
          puts <<~TEXT
            Version dir: #{container.parameter('lexicon.common.package_dir')}
            Prod database URL: #{container.parameter('lexicon.common.production.database.url')}
          TEXT
        end
delete(version) click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 112
def delete(version)
  semver = Semantic::Version.new(version)

  if loaded_versions.include?(semver)
    production_database
      .query("DROP SCHEMA \"#{version_to_schema(semver)}\" CASCADE")

    puts '[  OK ] '.green + "The version #{semver} has been deleted."
  else
    puts '[ NOK ] '.red + "The version #{semver.to_s.yellow} is not loaded or is enabled."
  end
end
disable() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 79
def disable
  if enabled? && !(version = enabled_version).nil?
    puts "Disabling version #{version.to_s.yellow}"
    do_disable
    puts '[  OK ] Done'.green
  else
    puts 'Lexicon is not enabled'.red
  end
end
enable(version = nil) click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 91
def enable(version = nil)
  if enabled?
    puts 'Disabling current version'
    do_disable
  end

  semver = if version.nil?
             loaded_versions.max
           else
             Semantic::Version.new(version)
           end

  puts "Enabling version #{semver.to_s.yellow}"

  do_enable(semver)

  puts '[  OK ] Done'.green
end
load(pkg_name) click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 38
def load(pkg_name)
  # @type [Production::DatasourceLoader] datasource_loader
  datasource_loader = get(Lexicon::Common::Production::DatasourceLoader)
  # @type [Package::PackageIntegrityValidator] integrity_validator
  integrity_validator = get(Lexicon::Common::Package::PackageIntegrityValidator)
  # @type [Package::DirectoryPackageLoader] package_loader
  package_loader = get(Lexicon::Common::Package::DirectoryPackageLoader)

  validate = options.fetch(:validate)
  names = options.fetch(:datasources, [])
  without = options.fetch(:without, [])
  package = package_loader.load_package(pkg_name)

  if package.nil?
    puts '[ NOK ] Did not find any package to load'.red
  elsif package.nil?
    puts "[ NOK ] No Package found for version #{version}".red
  elsif !validate || integrity_validator.valid?(package)
    datasource_loader.load_package(package, only: (names.empty? ? nil : names), without: without)
  else
    puts "[ NOK ] Lexicon package #{package.version} is corrupted".red
  end
end
loadable() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 13
def loadable
  available_packages.each do |package|
    puts package.version.to_s.green
    package.file_sets
           .sort_by(&:name)
           .each do |fs|
      puts ' -> ' + fs.name.send(fs.data_path.nil? ? :yellow : :green)
    end
  end
end
versions() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 64
def versions
  puts "Lexicon is #{enabled? ? 'ENABLED'.green + " (#{enabled_version.to_s.yellow})" : 'DISABLED'.red}"

  available = loaded_versions
  if available.empty?
    puts 'No other versions are loaded'
  else
    puts 'Available loaded versions are:'
    loaded_versions
      .each { |e| puts " - #{e}" }
  end
end

Private Instance Methods

as_version(version, &block) click to toggle source

@param [String, nil] version @return [Semantic::Version]

# File lib/lexicon/cli/command/production_command.rb, line 203
def as_version(version, &block)
  if version.nil?
    block.call
  else
    Semantic::Version.new(version)
  end
end
available_packages() click to toggle source

@return [Array<Package::Package>]

# File lib/lexicon/cli/command/production_command.rb, line 132
def available_packages
  # @type [Package::DirectoryPackageLoader] package_loader
  package_loader = get(Lexicon::Common::Package::DirectoryPackageLoader)

  if package_loader.root_dir.exist?
    package_loader.root_dir
                  .children
                  .select(&:directory?)
                  .map { |dir| package_loader.load_package(dir.basename.to_s) }
                  .compact
                  .sort { |a, b| a.version <=> b.version }
  else
    []
  end
end
do_disable() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 159
          def do_disable
            version = enabled_version
            raise StandardError.new('No version table present, cannot continue automatically') if version.nil?

            production_database.query <<~SQL
              BEGIN;
                DROP TABLE "lexicon"."version";
                ALTER SCHEMA "lexicon" RENAME TO "#{version_to_schema(version)}";
              COMMIT;
            SQL
          end
do_enable(version) click to toggle source

@param [Semantic::Version] version

# File lib/lexicon/cli/command/production_command.rb, line 149
          def do_enable(version)
            production_database.query <<~SQL
              BEGIN;
                ALTER SCHEMA "lexicon__#{version.to_s.gsub('.', '_')}" RENAME TO "lexicon";
                CREATE TABLE "lexicon"."version" ("version" VARCHAR);
                INSERT INTO "lexicon"."version" VALUES ('#{version}');
              COMMIT;
            SQL
          end
enabled?() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 218
def enabled?
  production_database
    .query("SELECT count(nspname) AS presence FROM pg_catalog.pg_namespace WHERE nspname = 'lexicon'")
    .to_a.first
    .fetch('presence').to_i.positive?
end
enabled_version() click to toggle source

@return [Semantic::Version, nil]

# File lib/lexicon/cli/command/production_command.rb, line 188
def enabled_version
  if version_table_present?
    Semantic::Version.new(
      production_database
        .query('SELECT version FROM lexicon.version LIMIT 1')
        .to_a.first
        .fetch('version')
    )
  else
    nil
  end
end
loaded_versions() click to toggle source

@return [Array<Semantic::Version>]

# File lib/lexicon/cli/command/production_command.rb, line 172
def loaded_versions
  disabled = production_database
               .query("SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname LIKE 'lexicon__%';")
               .to_a
               .map { |name| schema_to_version(name.fetch('nspname')) }

  enabled = enabled_version

  if enabled.nil?
    disabled
  else
    [*disabled, enabled].sort
  end
end
production_database() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 127
def production_database
  get(Lexicon::Cli::Extension::ProductionExtension::DATABASE)
end
version_table_present?() click to toggle source
# File lib/lexicon/cli/command/production_command.rb, line 211
def version_table_present?
  production_database
    .query("SELECT count(*) AS presence FROM information_schema.tables WHERE table_schema = 'lexicon' AND table_name = 'version'")
    .to_a.first
    .fetch('presence').to_i.positive?
end