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
- root¶
- schemas¶
The SchemaCollection of all schemas contained in this database.
Examples
Getting all schemas in
my_db
:>>> root.databases["my_db"].schemas
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"))
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 toFalse
.
Examples
Deleting a database using its reference:
>>> root.databases["to_be_deleted"].delete() The `delete` method is deprecated; use `drop` instead.
- 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"])
- 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()
- 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 toFalse
.
Examples
Using a database reference to drop a database, erroring if it does not exist:
>>> root.databases["to_be_dropped"].drop()
Using a database reference to drop a database, if it exists:
>>> root.databases["to_be_dropped"].drop(if_exists=True)
- 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"])
- 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, ... )
- 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)
- 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()
- 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()
- undrop() None ¶
Undrop this database.
Examples
Undropping a database using its reference:
>>> root.databases["to_be_undropped"].undrop()