Module: Torque::PostgreSQL::VersionedCommands

Defined in:
lib/torque/postgresql/versioned_commands.rb,
lib/torque/postgresql/versioned_commands/migrator.rb,
lib/torque/postgresql/versioned_commands/generator.rb,
lib/torque/postgresql/versioned_commands/schema_table.rb,
lib/torque/postgresql/versioned_commands/command_migration.rb,
lib/torque/postgresql/versioned_commands/migration_context.rb

Overview

Takes advantage of Rails migrations to create other sorts of objects/commands that can also be versioned. Everything migrated will still live within Migrations borders (i.e., the schema_migrations), but the way they are handled and registered in the schema dumper is completely different

Defined Under Namespace

Modules: Generator, Migration, MigrationContext, Migrator Classes: CommandMigration, SchemaTable

Constant Summary collapse

RAILS_APP =
defined?(Rails.application.paths)
NAME_MATCH =
'"?((?:[_a-z0-9]+"?\."?)?[_a-z0-9]+)"?'

Class Method Summary collapse

Class Method Details

.fetch_command(dirs, type, name, version) ⇒ Object

Get the content of the command based on the type, name, and version

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/torque/postgresql/versioned_commands.rb', line 33

def fetch_command(dirs, type, name, version)
  paths = Array.wrap(dirs).map { |d| "#{d}/**/*_#{type}_#{name}_v#{version}.sql" }
  files = Dir[*paths]
  return File.read(files.first) if files.one?

  raise ArgumentError, "    No previous version found for \#{type} \#{name}\n    of version v\#{version}.\n  MSG\n\n  raise ArgumentError, <<~MSG.squish if files.many?\n    Multiple files found for \#{type} \#{name}\n    of version v\#{version}.\n  MSG\nend\n".squish if files.none?

.filename_regexpObject

The regexp is dynamic due to the list of available types



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/torque/postgresql/versioned_commands.rb', line 50

def filename_regexp
  @filename_regexp ||= begin
    types = PostgreSQL.config.versioned_commands.types
    Regexp.new([
      "\\A([0-9]+)_",
      "(create|update|remove)_",
      "(#{types.join('|')})_",
      "([_a-z0-9]*)",
      "_v([0-9]+)",
      "\\.?([_a-z0-9]*)?",
      "\\.sql\\z",
    ].join)
  end
end

.valid_type?(type) ⇒ Boolean

Check if the type is current enabled

Returns:

  • (Boolean)


21
22
23
# File 'lib/torque/postgresql/versioned_commands.rb', line 21

def valid_type?(type)
  PostgreSQL.config.versioned_commands.types.include?(type.to_sym)
end

.validate!(type, content, name) ⇒ Object

Run the internal validations for the given type and content

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/torque/postgresql/versioned_commands.rb', line 26

def validate!(type, content, name)
  method_name = :"validate_#{type}!"
  return send(method_name, content, name) if valid_type?(type)
  raise ArgumentError, "Unknown versioned command type: #{type}"
end