betr-io / terraform-provider-mssql

Terraform provider for Microsoft SQL Server
https://registry.terraform.io/providers/betr-io/mssql/latest
MIT License
35 stars 28 forks source link

Destroying user fails if user owns a fulltext catalog #29

Open sigurdhj opened 2 years ago

sigurdhj commented 2 years ago

When trying to destroy a user that owns a fulltext catalog I get the following error message:

Error: unable to delete user [<name>].[<name>]: mssql: The database principal owns a fulltext catalog in the database, and cannot be dropped.

I'm working around this issue with the following SQL:

DECLARE @username VARCHAR(MAX) = '<username>'
DECLARE @userPrincipalId int = 
(
    SELECT [principal_id]
    FROM sys.database_principals
    WHERE [name] = @username
)

DECLARE @crsr CURSOR
SET @crsr = CURSOR FOR SELECT [name] from sys.fulltext_catalogs WHERE principal_id = @userPrincipalId
DECLARE @catalogName VARCHAR(MAX)

OPEN @crsr
FETCH NEXT FROM @crsr INTO @catalogName
WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC('ALTER AUTHORIZATION ON Fulltext Catalog::[' + @catalogName + '] TO [dbo]')
    FETCH NEXT FROM @crsr INTO @catalogName
END
magne commented 2 years ago

@sigurdhj would you consider submitting a PR?

alexdresko commented 1 year ago

I'm getting a similar error on destroy: Error: unable to delete user [example-simple-vi].[example-simple-vi]: mssql: The database principal owns a schema in the database, and cannot be dropped.

Is this related?