Moongoon::Database::Scripts::Base
Inherits Reference / Object
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.
Class methods
The action to perform on failure. Set to :retry to run the script again the next time the program starts.
The action to perform on failure. Set to :retry to run the script again the next time the program starts.
The action to perform on success. Set to :retry to run the script again the next time the program starts.
The action to perform on success. Set to :retry to run the script again the next time the program starts.
Instance methods
Will be executed once after a successful database connection and if it has never been run against the target database before.