pub(crate) mod ddl;
pub(crate) mod dml;
fn locked<I>(schema: &str, stmts: I, key: Option<&str>) -> String
where
I: IntoIterator<Item = String>,
{
format!(
"
BEGIN;
SET LOCAL lock_timeout = 30000;
SET LOCAL idle_in_transaction_session_timeout = 30000;
SELECT pg_advisory_xact_lock(('x' || encode(sha224((current_database() || '.pgboss.{schema}${1}')::bytea), 'hex'))::bit(64)::bigint);
{0};
COMMIT;
",
stmts.into_iter().collect::<Vec<_>>().join("\n"),
key.unwrap_or_default(),
)
}
pub(crate) fn install_app(schema: &str) -> String {
locked(
schema,
[
ddl::create_schema(schema),
ddl::create_job_state_enum(schema),
ddl::create_version_table(schema),
ddl::create_queue_table(schema),
ddl::create_schedule_table(schema),
ddl::create_subscription_table(schema),
ddl::create_job_table(schema),
ddl::create_job_common_table(schema),
ddl::proc::create_create_queue_function(schema),
ddl::proc::create_delete_queue_function(schema),
dml::insert_version(schema, crate::CURRENT_PGBOSS_APP_VERSION),
],
None,
)
}