tulip.pkg.database module exports the
database package. The package runs the database migrations when app:run is
called, executing all registered migrations.
connection_string: string = the connection string
migrations_connection_string: string = if set, the connection string to use only for the migrations, in which case the connection is not returned to the pool even if one is configured. This is typically useful to run the migrations with a different role than the rest of the application.
migrations: array of tables = the migrations to run, each table being an array of migration steps (string or function, as described in the Migrator) with a 'package' field that identifies for which package the migrations apply, and an optional 'after' field that identifies package names (array of strings) that must have their migrations run before this package.
The field migrations.check_only can be set to true to prevent this App instance from running the migrations. It will only check that all migrations have been run (by looking at the latest version applied for each registered package) and fail if it is not in sync with its configuration.
The field migrations.check_timeout can be set to limit the time to wait for migrations in seconds, it defaults to 10.
pool: table = if set, configures a connection pool so that calling App:db returns a pooled connection if available, and calling conn:close returns it to the pool if possible. The fields are max_idle, max_open, idle_timeout and life_timeout. Defaults are respectively 2, 0 (unlimited), 0 (no timeout) and 0 (no timeout). A release_connection field can also be set to a function, and it will be called prior to return a connection to the pool. It can be used to e.g. reset session settings, rollback any pending transaction, or release any locks. It receives the connection instance and the app instance as arguments.
Registering this package provides the following method and field extensions.
conn | ... = App:db([f, ...])
Returns a connection or, if a function is provided as argument, that function is called with the connection which is then released automatically when the function is done - regardless of whether an error was raised or not - and the call returns the return values of the function or nil and the error message.
v = App:lookup_migrations(name)
Returns the migrations registered for that name, or nil if none.
Registers the migrations migs for name. If there are already registered migrations for name, the new ones are appended at the end.