snowflake.core.database.DatabaseResourceΒΆ

class snowflake.core.database.DatabaseResource(name: str, collection: DatabaseCollection)ΒΆ

Bases: ObjectReferenceMixin[DatabaseCollection]

Represents a reference to a Snowflake database.

With this database reference, you can create, update, and fetch information about databases, as well as perform certain unique actions on them.

Attributes

database_rolesΒΆ

The DatabaseRoleCollection of all database roles contained in this database.

Examples

Getting all database roles in my_db:

>>> root.databases["my_db"].database_roles
Copy
rootΒΆ
schemasΒΆ

The SchemaCollection of all schemas contained in this database.

Examples

Getting all schemas in my_db:

>>> root.databases["my_db"].schemas
Copy

Methods

create_or_alter(database: DatabaseModel) β†’ NoneΒΆ

Create or alter a database in Snowflake.

create_or_update(database: DatabaseModel) β†’ NoneΒΆ

Create or update an existing database.

Examples

Create a database from a reference:

>>> root.databases["my_new_db"].create_or_update(Database("my_new_db"))
Copy

The create_or_update method is deprecated; use create_or_alter instead.

delete(if_exists: bool | None = None) β†’ NoneΒΆ

Delete this database.

Parameters:

if_exists (bool, optional) – Check the existence of this database before dropping it. The default is None, which is equivalent to False.

Examples

Deleting a database using its reference:

>>> root.databases["to_be_deleted"].delete()
The `delete` method is deprecated; use `drop` instead.
Copy
disable_failover(accounts: List[str] | None = None) β†’ NoneΒΆ

Disables failover for this primary databases.

Which means that no replica of this database (i.e. secondary database) can be promoted to serve as the primary database.

Optionally provide a comma-separated list of accounts in your organization to disable failover for this database only in the specified accounts.

Parameters:

accounts (list of str, optional) – Array of unique account identifiers for which to enable failover.

Examples

Disabling failover to some account using a database reference:

>>> root.databases["my_db"].enable_failover(accounts=["old_failover_acc"])
Copy
disable_replication(accounts: List[str] | None = None) β†’ NoneΒΆ

Disables replication for this primary database.

Which means that no replica of this database (i.e. secondary database) in another account can be refreshed. Any secondary databases remain linked to the primary database, but requests to refresh a secondary database are denied.

Note that disabling replication for a primary database does not prevent it from being replicated to the same account; therefore, the database continues to be listed in the SHOW REPLICATION DATABASES output.

Optionally provide a comma-separated list of accounts in your organization to disable replication for this database only in the specified accounts.

Parameters:

accounts (list of str, optional) – Array of unique account identifiers for which to disable replication.

Examples

Disabling all replication of β€œmy_db” database using its reference:

>>> root.databases["my_db"].disable_replication()
Copy
drop(if_exists: bool | None = None) β†’ NoneΒΆ

Drop this database.

Parameters:

if_exists (bool, optional) – Check the existence of this database before dropping it. The default is None, which is equivalent to False.

Examples

Using a database reference to drop a database, erroring if it does not exist:

>>> root.databases["to_be_dropped"].drop()
Copy

Using a database reference to drop a database, if it exists:

>>> root.databases["to_be_dropped"].drop(if_exists=True)
Copy
enable_failover(accounts: List[str]) β†’ NoneΒΆ

Enable a list of replicas of this database that can be promoted to primary.

Parameters:

accounts (list of str) – Array of unique account identifiers for which to enable failover.

Examples

Enabling failover to an account using a database reference:

>>> root.databases["my_db"].enable_failover(accounts=["my_failover_acc"])
Copy
enable_replication(accounts: List[str], ignore_edition_check: bool = False) β†’ NoneΒΆ

Promotes a local database to serve as a primary database for replication.

A primary database can be replicated in one or more accounts, allowing users in those accounts to query objects in each secondary (i.e. replica) database.

Alternatively, modify an existing primary database to add to or remove from the list of accounts that can store a replica of the database.

Provide a list of accounts in your organization that can store a replica of this database.

Parameters:
  • accounts (list of str) – Array of unique account identifiers for which to enable replication.

  • ignore_edition_check (bool, optional) –

    Whether to allow replicating data to accounts on lower editions.

    Default is True.

Examples

Enabling replication of β€œmy_db” database on 2 other accounts using its reference:

>>> root.databases["my_db"].enable_replication(
...     accounts=["accountName1", "accountName2"],
...     ignore_edition_check=True,
... )
Copy
fetch() β†’ DatabaseModelΒΆ

Fetch the details of a database.

Examples

Fetching a reference to a database to print whether it’s the currently used database:

>>> my_database = root.databases["my_db"].fetch()
>>> print(my_database.is_current)
Copy
promote_to_primary_failover() β†’ NoneΒΆ

Promotes the specified secondary (replica) database to serve as the primary.

When promoted, the database becomes writeable. At the same time, the previous primary database becomes a read-only secondary database.

Examples

Promoting a my_db failover database to be the primary using its reference:

>>> root.databases["my_db"].promote_to_primary_failover()
Copy
refresh_replication() β†’ NoneΒΆ

Refresh a secondary database from its primary database.

A snapshot includes changes to the objects and data.

Examples

Refreshing a database replication using its reference:

>>> root.databases["db_replication"].refresh_replication()
Copy
undrop() β†’ NoneΒΆ

Undrop this database.

Examples

Undropping a database using its reference:

>>> root.databases["to_be_undropped"].undrop()
Copy