Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ForceDrop option #142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/services/database/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ var attrDescriptions = map[string]string{
"id": "Database ID. Can be retrieved using `SELECT DB_ID('<db_name>')`.",
"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 {
Expand All @@ -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(),
}
}

Expand All @@ -48,6 +51,7 @@ func (d resourceData) withSettings(settings sql.DatabaseSettings) resourceData {
Id: d.Id,
Name: types.StringValue(settings.Name),
Collation: types.StringValue(settings.Collation),
ForceDrop: types.BoolValue(settings.ForceDrop),
}

if settings.Collation == "" {
Expand Down
4 changes: 4 additions & 0 deletions internal/services/database/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (d *dataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp
MarkdownDescription: attrDescriptions["collation"],
Computed: true,
},
"force_drop": schema.BoolAttribute{
MarkdownDescription: attrDescriptions["force_drop"],
Computed: true,
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/database/data_acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
4 changes: 4 additions & 0 deletions internal/services/database/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (l *listDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
MarkdownDescription: attrDescriptions["collation"],
Computed: true,
},
"force_drop": schema.BoolAttribute{
MarkdownDescription: attrDescriptions["force_drop"],
Computed: true,
},
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions internal/services/database/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func (r *res) Schema(ctx context.Context, req resource.SchemaRequest, resp *reso
Optional: true,
Computed: true,
},
"force_drop": schema.BoolAttribute{
MarkdownDescription: attrDescriptions["force_drop"],
Computed: true,
Optional: true,
},
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const NullDatabaseId = DatabaseId(-1)
type DatabaseSettings struct {
Name string
Collation string
ForceDrop bool
}

type DatabasePermission struct {
Expand Down Expand Up @@ -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))
}

Expand Down
4 changes: 2 additions & 2 deletions internal/sql/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down