Skip to contents

Ensures a database exists and sets it as the active database. If connected to MotherDuck, the function will run `CREATE DATABASE IF NOT EXISTS` followed by `USE <database>`. Prints CLI status information about the current user and database.

Usage

create_if_not_exists_database(.con, database_name)

Arguments

.con

A valid `DBI` connection (DuckDB / MotherDuck).

database_name

Name of the database to create/ensure and switch to.

Value

Invisibly returns `NULL`. Side effect: may create a database and switches to it; prints CLI status.

Details

- Connection type is checked via `validate_md_connection_status()` (with `return_type = "arg"`). - On MotherDuck, executes: - `CREATE DATABASE IF NOT EXISTS <database>` - `USE <database>` - Displays status and environment info with CLI messages.

See also

[DBI::dbExecute()]

Examples

if (FALSE) { # \dontrun{
con <- DBI::dbConnect(duckdb::duckdb())
create_if_not_exists_database(con, "analytics")
} # }