module DwCR::Metaschema

This module provides functionality to create, update, and verify the part of the DwCR schema needed to persist information about the DwCR

Public Class Methods

columns?(table, *columns) click to toggle source

Performs an integrety check on table, veryfies all columns are present with the parameters given in columns; a column parameter is an array with the structure: [:column_name, :column_type, {column_options} ]

# File lib/dwcr/metaschema/metaschema.rb, line 46
def self.columns?(table, *columns)
  db_cols = inspect_table(table, :schema)
  return unless db_cols
  exp_cols = columns.map(&:first).unshift(:id)
  exp_cols == db_cols.map(&:first)
end
create() click to toggle source

Creates the tables for the metaschema present in every DwCR archives, entities, attributes, content_files loads the Sequel::Model classes for these tables

# File lib/dwcr/metaschema/metaschema.rb, line 13
def self.create
  tabledefs = Psych.load_file(File.join(__dir__, 'metaschema_tables.yml'))
  tabledefs.to_h.each do |table, columns|
    DB.create_table? table do
      primary_key :id
      columns.each { |c| column(*c) }
    end
  end
  load_models
end
indexes?(table, *columns) click to toggle source

Performs an integrety check on table, veryfies all indices are present with the parameters given in columns; a column parameter is an array with the structure: [:column_name, :column_type, {column_options} ]

# File lib/dwcr/metaschema/metaschema.rb, line 57
def self.indexes?(table, *columns)
  db_idxs = inspect_table(table, :indexes)
  return unless db_idxs
  exp_idxs = columns.select { |column| column[2]&.fetch(:index, false) }
                    .map(&:first)
  exp_idxs & db_idxs.values.map { |x| x[:columns] }.flatten == exp_idxs
end
inspect_table(table, method) click to toggle source

Returns schema or index parameters for a table, depending on the second argument (:schema or :indexes) returns false if the table does not exist

# File lib/dwcr/metaschema/metaschema.rb, line 35
def self.inspect_table(table, method)
  DB.indexes(table).values.map { |x| x[:columns] }.flatten
  DB.send(method, table)
rescue Sequel::Error
  false
end
load_models() click to toggle source

Loads all Sequel::Model classes for the metaschema

# File lib/dwcr/metaschema/metaschema.rb, line 25
def self.load_models
  require_relative 'archive'
  require_relative 'entity'
  require_relative 'attribute'
  require_relative 'content_file'
end
update(archive, **options) click to toggle source

Updates all Attribute instances in a Archive with parameters from files in ContentFile schema_options: a Hash with attribute names as keys and boolean values { :type => true, :length => true } updates any attribute given as key where value is true

# File lib/dwcr/metaschema/metaschema.rb, line 70
def self.update(archive, **options)
  return if options.empty?

  # FIXME: handle situation where schema tables have been created
  options.select! { |_k, v| v == true }
  archive.entities
         .each { |entity| entity.update_attributes!(*options.keys) }
end
valid?() click to toggle source

Performs an integrity check on the metaschema in the DWCR file (the current database connection) returns true if all tables, columns, and indices as given in config/metaschema_tables.yml are present

# File lib/dwcr/metaschema/metaschema.rb, line 83
def self.valid?
  tabledefs = Psych.load_file('lib/dwcr/metaschema/metaschema_tables.yml')
  status = tabledefs.map do |td|
    table = td.first
    columns = td.last
    columns?(table, *columns) && indexes?(table, *columns)
  end
  return false if status.uniq.size > 1
  status.first
end