diff --git a/src/Cleanup.php b/src/Cleanup.php index 16a717f..29ac404 100644 --- a/src/Cleanup.php +++ b/src/Cleanup.php @@ -106,7 +106,7 @@ public function preMigration(string $mainRoleName): void $hasMainRoleOwnership = array_reduce( $mainRole, - fn ($found, $v) => $found || $v['owner'] === $this->config->getTargetSnowflakeUser(), + fn ($found, $v) => $found || $v['owner'] === $this->config->getTargetSnowflakeRole(), false, ); @@ -157,13 +157,20 @@ public function preMigration(string $mainRoleName): void } $this->destinationConnection->useRole($mainRoleName); if ($currentRole === $mainRoleName && !$mainRoleExistsOnTargetUser) { - $this->destinationConnection->query(sprintf( - 'GRANT ROLE %s TO USER %s;', - Helper::quoteIdentifier($currentRole), - Helper::quoteIdentifier($this->config->getTargetSnowflakeUser()) - )); + $this->destinationConnection->grantRoleToUser( + $this->config->getTargetSnowflakeUser(), + $currentRole, + ); + } + try { + $this->destinationConnection->useRole($role['granted_by']); + } catch (RuntimeException $e) { + $this->destinationConnection->grantRoleToUser( + $this->config->getTargetSnowflakeUser(), + $role['granted_by'], + ); + $this->destinationConnection->useRole($role['granted_by']); } - $this->destinationConnection->useRole($role['granted_by']); /** @var FutureGrantToRole[] $futureGrants */ $futureGrants = array_map(