From b530c2fdbe82d6f23e7e5f61e90c3265611fbc97 Mon Sep 17 00:00:00 2001 From: "romg@pecan.ai" Date: Mon, 27 May 2024 14:35:53 +0100 Subject: [PATCH] add ForceDrop option --- internal/services/database/base.go | 3 +++ internal/services/database/data_acctest.go | 2 +- internal/sql/database.go | 4 ++++ internal/sql/database_test.go | 4 ++-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/services/database/base.go b/internal/services/database/base.go index f4792e1..60f7694 100644 --- a/internal/services/database/base.go +++ b/internal/services/database/base.go @@ -14,12 +14,14 @@ var attrDescriptions = map[string]string{ "id": "Database ID. Can be retrieved using `SELECT DB_ID('')`.", "name": fmt.Sprintf("Database name. %s.", common.RegularIdentifiersDoc), "collation": "Default collation name. Can be either a Windows collation name or a SQL collation name.", + "force_drop": "Whether to drop all connections to the database before dropping it.", } type resourceData struct { Id types.String `tfsdk:"id"` Name types.String `tfsdk:"name"` Collation types.String `tfsdk:"collation"` + ForceDrop types.Bool `tfsdk:"force_drop"` } func (d resourceData) getDbId(ctx context.Context) sql.DatabaseId { @@ -40,6 +42,7 @@ func (d resourceData) toSettings() sql.DatabaseSettings { return sql.DatabaseSettings{ Name: d.Name.ValueString(), Collation: d.Collation.ValueString(), + ForceDrop: d.ForceDrop.ValueBool(), } } diff --git a/internal/services/database/data_acctest.go b/internal/services/database/data_acctest.go index a906df1..c136685 100644 --- a/internal/services/database/data_acctest.go +++ b/internal/services/database/data_acctest.go @@ -11,7 +11,7 @@ import ( func testDataSource(testCtx *acctest.TestContext) { const resourceName = "data.mssql_database.test" var dbId string - dbSettings := sql.DatabaseSettings{Name: "data_test_db", Collation: "SQL_Latin1_General_CP1_CS_AS"} + dbSettings := sql.DatabaseSettings{Name: "data_test_db", Collation: "SQL_Latin1_General_CP1_CS_AS", ForceDrop: false} newDataResource := func(name string) string { return fmt.Sprintf(` diff --git a/internal/sql/database.go b/internal/sql/database.go index f6d338d..892381d 100644 --- a/internal/sql/database.go +++ b/internal/sql/database.go @@ -13,6 +13,7 @@ const NullDatabaseId = DatabaseId(-1) type DatabaseSettings struct { Name string Collation string + ForceDrop bool } type DatabasePermission struct { @@ -141,6 +142,9 @@ func (db *database) SetCollation(ctx context.Context, collation string) { func (db *database) Drop(ctx context.Context) { settings := db.GetSettings(ctx) + if settings.ForceDrop { + db.conn.exec(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;", settings.Name)) + } db.conn.exec(ctx, fmt.Sprintf("DROP DATABASE [%s]", settings.Name)) } diff --git a/internal/sql/database_test.go b/internal/sql/database_test.go index a931b6d..625ac50 100644 --- a/internal/sql/database_test.go +++ b/internal/sql/database_test.go @@ -99,7 +99,7 @@ func (s *DatabaseTestSuite) TestCreteDatabaseNoCollation() { } func (s *DatabaseTestSuite) TestCreteDatabaseWithCollation() { - settings := DatabaseSettings{Name: "new_test_db", Collation: "new_test_db_collation"} + settings := DatabaseSettings{Name: "new_test_db", Collation: "new_test_db_collation", ForceDrop: false} dbId := DatabaseId(1223464) expectExactExec(s.mock, "CREATE DATABASE [%s] COLLATE %s", settings.Name, settings.Collation). WillReturnResult(sqlmock.NewResult(0, 1)) @@ -111,7 +111,7 @@ func (s *DatabaseTestSuite) TestCreteDatabaseWithCollation() { } func (s *DatabaseTestSuite) TestGetSettings() { - expSettings := DatabaseSettings{Name: "test_db_name", Collation: "test_collation"} + expSettings := DatabaseSettings{Name: "test_db_name", Collation: "test_collation", ForceDrop: false} s.expectDatabaseSettingQuery(). WithArgs(s.db.id). WillReturnRows(newRows("name", "collation_name").AddRow(expSettings.Name, expSettings.Collation))