Creating and Removing Databases with PowerShell In Azure Cosmos DB

Creating and Removing Databases with PowerShell In Azure Cosmos DB

Creating and Removing Databases with PowerShell In Azure Cosmos DB

SQLShack

SQL Server training Español

Creating and Removing Databases with PowerShell In Azure Cosmos DB

May 27, 2019 by Timothy Smith Our testing or development may call for dynamic creation on the database level for Azure Cosmos DB rather than the account level. As we’ve seen with dynamically working with a Cosmos database account using PowerShell, we can create, remove, and obtain properties of the account. Identically, we can do this on the database level as well and we may use this in testing if we need the same Cosmos database account for other testing purposes. Development situations may also involve use cases where we want to test a concept and dynamically create a database within our Cosmos database account. In this tip, we’ll look at working with our Azure Cosmos database account on the database object level where we do nothing to manipulate the account itself, only add databases to the account once it’s been setup.

Dependencies

In addition to using PowerShell’s Az module and connecting to Azure using the Az module, we will use an existing Cosmos database account for running tests of creating and removing a database. In the below script, we first connect – which will be required for our later scripts – and we also get a list of all our Azure Cosmos DBs. We can use this check for logic when we need to first validate the existence of a Cosmos database account, such as checking if the name of scosdb exists (or the name you use). 12345 Connect-AzAccount Out-Null $resource = "Microsoft.DocumentDb/databaseAccounts" (Get-AzResource -ResourceType $resource).Name In NoSQL contexts such as MongoDB, we can use a “use database” statement or a connection to a database with a database that doesn’t exist and create a collection that doesn’t exist from an insert without explicit creation. For NoSQL databases, this is one way to create a database and collection on the fly, but if our use case requires removing it, we may want to use the explicit creation and drop, like we’ll be doing in this tip with Azure Cosmos DB. To demonstrate this on-the-flow creation process, I run the below commands in order in a MongoDB shell session – first I check the existing databases; second, I use a database that doesn’t exist and create a collection in the database using an insert; third I check the collections and databases, and we see the collection and database now exists. 12345 show dbsuse OurNewestDatabasedb.OurNewestCollection.insert({“_id”:1})show collectionsshow dbs In some contexts, we may create collections and databases without explicit creations in NoSQL databases like Azure Cosmos DB or MongoDB This development style may be an option in some NoSQL contexts, but it may not be the option we choose in testing if we want to re-use the parameters for the explicit creation and removal of the database we create. Our approach will differ in this tip since we’ll focus on the explicit creation and removal of a database.

Getting Properties of a Database

As we noticed in our check, we can call the Get-AzResource to discover if a resource exists – we searched for all Azure Cosmos DB accounts (Microsoft.DocumentDB). With this same function, we can obtain some of the properties of our existing Cosmos DB account – in this case the scosdb account. Calling the function with the parameter of ResourceType and Name will return some specific information about this account – we’ll save some of these values to variables, such as kind, location and rGroup. 12345 $resource = "Microsoft.DocumentDb/databaseAccounts"$cosmosdb = "scosdb"$rGroup = (Get-AzResource -ResourceType $resource -Name $cosmosdb).ResourceGroupName$kind = (Get-AzResource -ResourceType $resource -Name $cosmosdb).Kind$location = (Get-AzResource -ResourceType $resource -Name $cosmosdb).Location These properties can be useful if we want to create another Cosmos database account that has similar properties – such as creating one that has the same location and resource group name. We’ll also look at using properties for creating a database within the Cosmos database account.

Creating a Database

If we consider the use case of a unit and security test where we want to run a test on a newly created database and remove it when finished where our Azure Cosmos DB has other unit tests running on other databases, removing the Cosmos account would cause disruptions. For this use case, we want to dynamically create a SQL API database within our Cosmos database account for our testing. The creation will carry some similarities to the account creation and with our properties that we saved in the above script from the existing account, we’ll be able to use the variables to create our database. In the below code, we create our database account with our Azure Cosmos DB and we see output returned from this call. We use the -Force parameter here to avoid the dialog that returns asking us to input yes or no, which is more appropriate for automated development contexts. If you prefer the dialog box, you can remove this parameter. 1234567891011121314 $resource = "Microsoft.DocumentDb/databaseAccounts"$type = "Microsoft.DocumentDb/databaseAccounts/apis/databases"$cosmosdb = "scosdb"$testdb = "TestDB"$api = "2015-04-08" $rGroup = (Get-AzResource -ResourceType $resource -Name $cosmosdb).ResourceGroupName $properties = @{}$properties.Add("resource", @{"id" = "$testdb"})$properties.Add("options", @{"throughput" = 1000}) New-AzResource -ResourceType $type -ApiVersion $api -ResourceGroupName $rGroup -Name "$cosmosdb/sql/$testdb" -PropertyObject $properties -Force Part of the returned output when we create our SQL API database in our Azure Cosmos DB As we see in the Azure portal, we’ve created our SQL API database with a throughput of 1000 RUs within our Cosmos database account. The Azure Portal view of our SQL API database in our Azure Cosmos DB Like our unit and security testing scenario, we can use this model to horizontally scale SQL API databases within our Cosmos database account, test proof-of-concepts that we don’t intend to keep after we prove out our concept, or other development purposes that allow us to re-use our creation scripts saving us redevelopment efforts.

Removing a Database

In testing situations, we may only create a database to perform a unit or security test without keeping the database that we created. The same pattern may also be true if we have a quick proof-of-concept test where we build an Azure Cosmos DB and remove it when we’re done with our test (retaining the results of our findings). The purpose of removing the database doesn’t only eliminate possible costs by keeping something unused in our environment, it prevents other developers from using it or misunderstanding the intent. This latter point becomes an issue in organizations where developers move on and newer developers wonder what was the function of architecture and feel reluctant to remove it since the history is questionable. We never want resources we’re not using for this reason. Finally, removing a resource that we’re not using completes the cycle of our intent – if our intent is testing, the final cycle may be the removal of what we created. If we create databases for unit and security testing or proof-of-concept reasons in our Azure Cosmos DB and don’t need the Cosmos database account, we may remove it on this level. Recall that the creation and removal of the Cosmos database requires some time, so we may want to keep the account and remove individual databases within the account. This is also true if we use the Cosmos database account as a holder for databases used in testing and development. 123456789 $resource = "Microsoft.DocumentDb/databaseAccounts"$type = "Microsoft.DocumentDb/databaseAccounts/apis/databases"$cosmosdb = "scosdb"$testdb = "TestDB"$api = "2015-04-08" $rGroup = (Get-AzResource -ResourceType $resource -Name $cosmosdb).ResourceGroupName Remove-AzResource -ResourceType $type -ApiVersion $api -ResourceGroupName $rGroup -Name "$cosmosdb/sql/$testdb" -Force We confirm that our database within our Azure Cosmos DB is removed in the portal When removing our SQL API database, we don’t need to include the properties and we can see the number of parameters we’re able to re-use from our creation process. Like with the creation process, we include the -Force parameter to avoid the dialog box – a useful technique when we want an automated process building and removing our SQL API database.

Conclusion

As we see with PowerShell and the Az module, we can get information about our Azure Cosmos DB account, and dynamically create and remove databases within our Cosmos database account. This can be useful in development for new concepts we want to test and validate – such as expansions to horizontal scaling – or it can be useful for unit and security tests where we create, test and remove the database following the test. Unlike creating the Cosmos database account, creating a database within the Cosmos database account in PowerShell is faster and is more appropriate if we want to retain the account while letting the database go. In addition to the dynamic creation, we can get properties we need for the account to update configuration information if required. Finally, if our situation calls for removal, we can remove the database within the Azure Cosmos DB.

Table of contents

Creating and Removing Azure Cosmos DBs with PowerShell Getting and Updating Connection Information for Azure Cosmos DB Creating and Removing Databases with PowerShell In Azure Cosmos DB Increasing or Decreasing Scale for Azure Cosmos DB Creating Containers with PowerShell For Azure Cosmos DB Author Recent Posts Timothy SmithTim manages hundreds of SQL Server and MongoDB instances, and focuses primarily on designing the appropriate architecture for the business model.

He has spent a decade working in FinTech, along with a few years in BioTech and Energy Tech.He hosts the West Texas SQL Server Users' Group, as well as teaches courses and writes articles on SQL Server, ETL, and PowerShell.

In his free time, he is a contributor to the decentralized financial industry.

View all posts by Timothy Smith Latest posts by Timothy Smith (see all) Data Masking or Altering Behavioral Information - June 26, 2020 Security Testing with extreme data volume ranges - June 19, 2020 SQL Server performance tuning – RESOURCE_SEMAPHORE waits - June 16, 2020

Related posts

Creating and Removing Azure Cosmos DBs with PowerShell Increasing or Decreasing Scale for Azure Cosmos DB Getting and Updating Connection Information for Azure Cosmos DB Creating Containers with PowerShell For Azure Cosmos DB What is Azure SQL Cosmos DB? 3,012 Views

Follow us

Popular

SQL Convert Date functions and formats SQL Variables: Basics and usage SQL PARTITION BY Clause overview Different ways to SQL delete duplicate rows from a SQL Table How to UPDATE from a SELECT statement in SQL Server SQL Server functions for converting a String to a Date SELECT INTO TEMP TABLE statement in SQL Server SQL WHILE loop with simple examples How to backup and restore MySQL databases using the mysqldump command CASE statement in SQL Overview of SQL RANK functions Understanding the SQL MERGE statement INSERT INTO SELECT statement overview and examples SQL multiple joins for beginners with examples Understanding the SQL Decimal data type DELETE CASCADE and UPDATE CASCADE in SQL Server foreign key SQL Not Equal Operator introduction and examples SQL CROSS JOIN with examples The Table Variable in SQL Server SQL Server table hints – WITH (NOLOCK) best practices

Trending

SQL Server Transaction Log Backup, Truncate and Shrink Operations Six different methods to copy tables between databases in SQL Server How to implement error handling in SQL Server Working with the SQL Server command line (sqlcmd) Methods to avoid the SQL divide by zero error Query optimization techniques in SQL Server: tips and tricks How to create and configure a linked server in SQL Server Management Studio SQL replace: How to replace ASCII special characters in SQL Server How to identify slow running queries in SQL Server SQL varchar data type deep dive How to implement array-like functionality in SQL Server All about locking in SQL Server SQL Server stored procedures for beginners Database table partitioning in SQL Server How to drop temp tables in SQL Server How to determine free space and file size for SQL Server databases Using PowerShell to split a string into an array KILL SPID command in SQL Server How to install SQL Server Express edition SQL Union overview, usage and examples

Solutions

Read a SQL Server transaction logSQL Server database auditing techniquesHow to recover SQL Server data from accidental UPDATE and DELETE operationsHow to quickly search for SQL database data and objectsSynchronize SQL Server databases in different remote sourcesRecover SQL data from a dropped table without backupsHow to restore specific table(s) from a SQL Server database backupRecover deleted SQL data from transaction logsHow to recover SQL Server data from accidental updates without backupsAutomatically compare and synchronize SQL Server dataOpen LDF file and view LDF file contentQuickly convert SQL code to language-specific client codeHow to recover a single table from a SQL Server database backupRecover data lost due to a TRUNCATE operation without backupsHow to recover SQL Server data from accidental DELETE, TRUNCATE and DROP operationsReverting your SQL Server database back to a specific point in timeHow to create SSIS package documentationMigrate a SQL Server database to a newer version of SQL ServerHow to restore a SQL Server database backup to an older version of SQL Server

Categories and tips

►Auditing and compliance (50) Auditing (40) Data classification (1) Data masking (9) Azure (295) Azure Data Studio (46) Backup and restore (108) ►Business Intelligence (482) Analysis Services (SSAS) (47) Biml (10) Data Mining (14) Data Quality Services (4) Data Tools (SSDT) (13) Data Warehouse (16) Excel (20) General (39) Integration Services (SSIS) (125) Master Data Services (6) OLAP cube (15) PowerBI (95) Reporting Services (SSRS) (67) Data science (21) ►Database design (233) Clustering (16) Common Table Expressions (CTE) (11) Concurrency (1) Constraints (8) Data types (11) FILESTREAM (22) General database design (104) Partitioning (13) Relationships and dependencies (12) Temporal tables (12) Views (16) ►Database development (418) Comparison (4) Continuous delivery (CD) (5) Continuous integration (CI) (11) Development (146) Functions (106) Hyper-V (1) Search (10) Source Control (15) SQL unit testing (23) Stored procedures (34) String Concatenation (2) Synonyms (1) Team Explorer (2) Testing (35) Visual Studio (14) DBAtools (35) DevOps (23) DevSecOps (2) Documentation (22) ETL (76) ►Features (213) Adaptive query processing (11) Bulk insert (16) Database mail (10) DBCC (7) Experimentation Assistant (DEA) (3) High Availability (36) Query store (10) Replication (40) Transaction log (59) Transparent Data Encryption (TDE) (21) Importing, exporting (51) Installation, setup and configuration (121) Jobs (42) ▼Languages and coding (686) Cursors (9) DDL (9) DML (6) JSON (17) PowerShell (77) Python (37) R (16) SQL commands (196) SQLCMD (7) String functions (21) T-SQL (275) XML (15) Lists (12) Machine learning (37) Maintenance (99) Migration (50) Miscellaneous (1) ►Performance tuning (869) Alerting (8) Always On Availability Groups (82) Buffer Pool Extension (BPE) (9) Columnstore index (9) Deadlocks (16) Execution plans (125) In-Memory OLTP (22) Indexes (79) Latches (5) Locking (10) Monitoring (100) Performance (196) Performance counters (28) Performance Testing (9) Query analysis (121) Reports (20) SSAS monitoring (3) SSIS monitoring (10) SSRS monitoring (4) Wait types (11) ►Professional development (68) Professional development (27) Project management (9) SQL interview questions (32) Recovery (33) Security (84) Server management (24) SQL Azure (271) SQL Server Management Studio (SSMS) (90) SQL Server on Linux (21) ►SQL Server versions (177) SQL Server 2012 (6) SQL Server 2016 (63) SQL Server 2017 (49) SQL Server 2019 (57) SQL Server 2022 (2) ▼Technologies (334) AWS (45) AWS RDS (56) Azure Cosmos DB (28) Containers (12) Docker (9) Graph database (13) Kerberos (2) Kubernetes (1) Linux (44) LocalDB (2) MySQL (49) Oracle (10) PolyBase (10) PostgreSQL (36) SharePoint (4) Ubuntu (13) Uncategorized (4) Utilities (21) Helpers and best practices BI performance counters SQL code smells rules SQL Server wait types © 2022 Quest Software Inc. ALL RIGHTS RESERVED. GDPR Terms of Use Privacy
Share:
0 comments

Comments (0)

Leave a Comment

Minimum 10 characters required

* All fields are required. Comments are moderated before appearing.

No comments yet. Be the first to comment!