abstract class Moongoon::Database::Scripts::Base

Overview

Scripts inherit from this class.

Example

class Moongoon::Database::Scripts::Test < Moongoon::Database::Scripts::Base
  # Scripts run in ascending order.
  # Default order if not specified is 1.
  order Time.utc(2020, 3, 11).to_unix

  # Use :retry to retry the script next time moongoon connects if the script raises.
  on_error :discard

  def process(db : Mongo::Database)
    # Dummy code that will add a ban flag for users that are called 'John'.
    # This code uses the `cryomongo` syntax, but Models could
    # be used for convenience despite a performance overhead.
    db["users"].update_many(
      filter: {name: "John"},
      update: {"$set": {"banned": true}},
    )
  end
end

Usage

Any class that inherits from Moongoon::Database::Scripts::Base will be registered as a script.

Scripts are run when calling Moongoon::Database.connect and after a successful database connection. They are run a single time and the outcome is be written in the scripts collection.

If multiple instances of the server are started simultaneously they will wait until all the scripts are processed before resuming execution.

Defined in:

Class Method Summary

Instance Method Summary

Class Method Detail

def self.on_error : Action #

The action to perform on failure. Set to :retry to run the script again the next time the program starts.


def self.on_error=(on_error : Action) #

The action to perform on failure. Set to :retry to run the script again the next time the program starts.


def self.on_success : Action #

The action to perform on success. Set to :retry to run the script again the next time the program starts.


def self.on_success=(on_success : Action) #

The action to perform on success. Set to :retry to run the script again the next time the program starts.


def self.order : Int64 #

The order in which the scripts are run.


def self.order=(order : Int64) #

The order in which the scripts are run.


Instance Method Detail

abstract def process(db : Mongo::Database) #

Will be executed once after a successful database connection and if it has never been run against the target database before.