The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

App::Sqitch::Engine - Sqitch Deployment Engine

Synopsis

  my $engine = App::Sqitch::Engine->new( sqitch => $sqitch );

Description

App::Sqitch::Engine provides the base class for all Sqitch storage engines. Most likely this will not be of much interest to you unless you are hacking on the engine code.

Interface

Class Methods

config_vars

  my %vars = App::Sqitch::Engine->config_vars;

Returns a hash of names and types to use for configuration variables for the engine. These can be set under the core.$engine_name section in any configuration file.

The keys in the returned hash are the names of the variables. The values are the data types. Valid data types include:

any
int
num
bool
bool-or-int

Values ending in + (a plus sign) may be specified multiple times. Example:

  (
      client  => 'any',
      db_name => 'any',
      host    => 'any',
      port    => 'int',
      set     => 'any+',
  )

In this example, the port variable will be stored and retrieved as an integer. The set variable may be of any type and may be included multiple times. All the other variables may be of any type.

By default, App::Sqitch::Engine returns an empty list. Subclasses for supported engines will return more.

Constructors

load

  my $cmd = App::Sqitch::Engine->load(%params);

A factory method for instantiating Sqitch engines. It loads the subclass for the specified engine and calls new, passing the Sqitch object. Supported parameters are:

sqitch

The App::Sqitch object driving the whole thing.

new

  my $engine = App::Sqitch::Engine->new(%params);

Instantiates and returns a App::Sqitch::Engine object.

Instance Methods

name

  my $name = $engine->name;

The name of the engine. Defaults to the last part of the package name, so as a rule you should not need to override it, since it is that string that Sqitch uses to find the engine class.

destination

  my $destination = $engine->destination;

Returns the name of the destination database. This will usually be the same as the configured database name or the value of the --db-name option. Hover, subclasses may override it to provide other values, such as when neither of the above have values but there is nevertheless a default value assumed by the engine. Used internally to name the destination in status messages.

deploy

  $engine->deploy($to_target);
  $engine->deploy($to_target, $mode);

Deploys changes to the destination database, starting with the current deployment state, and continuing to $to_target. $to_target must be a valid target specification as passable to the index_of() method of App::Sqitch::Plan. If $to_target is not specified, all changes will be applied.

The second argument specifies the reversion mode in the case of deployment failure. The allowed values are:

all

In the event of failure, revert all deployed changes, back to the point at which deployment started. This is the default.

tag

In the event of failure, revert all deployed changes to the last successfully-applied tag. If no tags were applied during this deployment, all changes will be reverted to the pint at which deployment began.

change

In the event of failure, no changes will be reverted. This is on the assumption that a change failure is total, and the change may be applied again.

Note that, in the event of failure, if a reversion fails, the destination database may be left in a corrupted state. Write your revert scripts carefully!

revert

  $engine->revert($tag);

Reverts the App::Sqitch::Plan::Tag from the database, including all of its associated changes.

deploy_change

  $engine->deploy_change($change);

Used internally by deploy() to deploy an individual change.

revert_change

  $engine->revert_change($change);

Used internally by revert() (and, by deploy() when a deploy fails) to revert an individual change.

is_deployed

  say "Tag deployed"  if $engine->is_deployed($tag);
  say "Change deployed" if $engine->is_deployed($change);

Convenience method that dispatches to is_deployed_tag() or is_deployed_change() as appropriate to its argument.

latest_change

  my $change = $engine->latest_change;

Returns the App::Sqitch::Plan::Change object representing the most recently applied change.

Abstract Instance Methods

These methods must be overridden in subclasses.

begin_work

  $engine->begin_work;

This method is called just before a change is deployed or reverted. It should create a lock to prevent any other processes from making changes to the database, to be freed in finish_work.

finish_work

  $engine->finish_work;

This method is called after a change has been deployed or reverted. It should unlock the lock created by begin_work.

initialized

  $engine->initialize unless $engine->initialized;

Returns true if the database has been initialized for Sqitch, and false if it has not.

initialize

  $engine->initialize;

Initializes a database for Sqitch by installing the Sqitch metadata schema and/or tables. Should be overridden by subclasses. This implementation throws an exception

is_deployed_tag

  say "Tag deployed"  if $engine->is_deployed_tag($tag);

Should return true if the tag has been deployed to the database, and false if it has not.

is_deployed_change

  say "Change deployed"  if $engine->is_deployed_change($change);

Should return true if the change has been deployed to the database, and false if it has not.

log_deploy_change

  $engine->log_deploy_change($change);

Should write to the database metadata and history the records necessary to indicate that the change has been deployed.

log_fail_change

  $engine->log_fail_change($change);

Should write to the database event history a record reflecting that deployment of the change failed.

log_revert_change

  $engine->log_revert_change($change);

Should write to and/or remove from the database metadata and history the records necessary to indicate that the change has been reverted.

check_requires

  if ( my @requires = $engine->requires($change) ) {
      die "Change requires undeployed changes: @requires\n";
  }

Returns the names of any changes required by the specified change that are not currently deployed to the database. If none are returned, the requirements are presumed to be satisfied. The engine implementation should compare changes by their IDs.

check_conflicts

  if ( my @conflicts = $engine->conflicts($change) ) {
      die "Change conflicts with previously deployed changes: @conflicts\n";
  }

Returns the names of any currently-deployed changes that conflict with specified change. If none are returned, there are presumed to be no conflicts.

If any of the changes that conflict with the specified change have been deployed to the database, their names should be returned by this method. If no names are returned, it's because there are no conflicts. The engine implementation should compare changes by their IDs.

latest_change_id

  my $change_id = $engine->latest_change_id;

Returns the ID of the most recently applied change.

deployed_change_ids

  my @change_ids = $engine->deployed_change_ids;

Returns a list of all deployed change IDs in the order in which they were deployed.

deployed_change_ids_since

  my @change_ids = $engine->deployed_change_ids_since($change);

Returns a list of change IDs for changes deployed after the specified change.

name_for_change_id

  my $change_name = $engine->name_for_change_id($change_id);

Returns the name of the change identified by the ID argument. If a tag was applied to a change after that change, the name will be returned with the tag qualification, e.g., app_user@beta. This value should be suitable for uniquely identifying the change, and passing to the get or index_of methods of App::Sqitch::Plan.

current_state

  my $state = $engine->current_state;

Returns a hash reference representing the current state of the database, or undef if the database has no changes deployed. The hash contains information about the last successfully deployed change, as well as any associated tags. The keys to the hash should include:

change_id

The current change ID.

change

The current change name.

deployed_at

An App::Sqitch::DateTime object representing the date and time at which the change was deployed.

deployed_by

Name of the user who deployed the change.

tags

An array reference of the names of associated tags.

current_changes

  my $iter = $engine->current_changes;
  while (my $change = $iter->()) {
      say '* ', $change->{change};
  }

Returns a code reference that iterates over a list of the currently deployed changes in reverse chronological order. Each change is represented by a hash reference containing the following keys:

change_id

The change ID.

change

The name of the change.

deployed_at

An App::Sqitch::DateTime object representing the date and time at which the change was deployed.

deployed_by

Name of the user who deployed the change.

current_tags

  my $iter = $engine->current_tags;
  while (my $tag = $iter->()) {
      say '* ', $tag->{tag};
  }

Returns a code reference that iterates over a list of the currently deployed tags in reverse chronological order. Each tag is represented by a hash reference containing the following keys:

tag_id

The tag ID.

tag

The name of the tag.

applied_at

An App::Sqitch::DateTime object representing the date and time at which the tag was applied.

applied_by

Name of the user who) or applied the tag.

search_events

  my $iter = $engine->search_events( %params );
  while (my $change = $iter->()) {
      say '* $change->{event}ed $change->{change}";
  }

Searches the deployment event log and returns an iterator code reference with the results. If no parameters are provided, a list of all events will be returned from the iterator reverse chronological order. The supported parameters are:

event

An array of the type of event to search for. Allowed values are "deploy", "revert", and "fail".

change

Limit the events to those with changes matching the specified regular expression.

actor

Limit the events to those logged by the actors (users) with names matching the specified regular expression.

limit

Limit the number of events to the specified number.

offset

Skip the specified number of events.

direction

Return the results in the specified order, which must be a value matching /^(:?a|de)sc/i for "ascending" or "descending".

Each event is represented by a hash reference containing the following keys:

event

The type of event, which is one of:

deploy
revert
fail
change_id

The change ID.

change

The name of the change.

deployed_at

An App::Sqitch::DateTime object representing the date and time at which the event was logged.

deployed_by

Name of the user for whom the event was logged.

run_file

  $engine->run_file($file);

Should execute the commands in the specified file. This will generally be an SQL file to run through the engine's native client.

run_handle

  $engine->run_handle($file_handle);

Should execute the commands in the specified file handle. The file handle's contents should be piped to the engine's native client.

See Also

sqitch

The Sqitch command-line client.

Author

David E. Wheeler <david@justatheory.com>

License

Copyright (c) 2012 iovation Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.