nicheware.platform.utilities.common.state.migration

Functions used when handling migration of state, where state is represented by a Clojure map.

The functions within the migration namespace can be categorised as:

Function group Functions
helper functions major-version, minor-version, major-minor-version, could-upgrade-version
migration interface migrate-state

The migration functions assume the state map contains a version attribute

  { :version "1.0.1"}

Some functions require a migration map which for each current version defines the migrations that are defined to a later version.

  {"1.0"{:from-version "1.0"
           :to-version "1.1.0"
           :migration-fns [identity]}

   "1.1"{:from-version "1.1"
           :to-version "1.2.0"
           :migration-fns [add-effects/add-pattern-effects]}}

The :migration-fns attribute is a vector of functions that accept the old state map and return a newly migrated state map.

could-upgrade-version

(could-upgrade-version first second)

Test whether the first version is lower or equal to the second version. Just considers the major and minor versions, i.e. it is possible to upgrade.

The revision number (last digit) is not considered, as it is deemed not necessary to upgrade for revision changes only.

  • first: Version number of form’x.x.x’
  • second: Version number of form’x.x.x’
  • returns: True if possible to upgrade from first to second (i.e. first is less than or equal to second)

major-minor-version

(major-minor-version version)

Returns the major minor version string, removing any revision number (if present)

eg:

(major-minor-version "2.3.1") => "2.3"
(major-minor-version "1.1") => "1.1"

major-version

(major-version version)

Extracts the major version as an int from a version string of the form ‘x.x.x’

  • version: version number string of the form ‘x.x.x’ or ‘x.x’
  • returns: The major version (first x) as an int. nil if not a valid version number.

migrate-state

(migrate-state {:keys [version], :as incoming-state} target-version migrations)

Migrate state from its existing version to the goal version using the supplied migration functions.

  • incoming-state: Current state including the version number. {:version "1.2", ...}
  • target-version: New version state should migrate to. eg "1.3.1"
  • migrations: Map defining migrations for different source and target versions. See below.
  • returns: a new state map, upgraded to the target-version.

migrations:

{<source-migration-version> {:from-version :to-version :migration-fns[]]}}

:migration-fns is a vector of functions that when applied in order will accept a state map conforming to the :from-version state schema, and will return a new state map conforming to the :to-version schema.

migrate-state will work out which migrations need to be applied to convert the state to the eventual target version.

minor-version

(minor-version version)

Extracts the minor version as an int from a version string of the form ‘x.x.x’

  • version: version number string of the form ‘x.x.x’ or ‘x.x’
  • returns: The minor version (second x) as an int. nil if not a valid version number.