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

Refactor Cleanup class #47

Merged
merged 7 commits into from
Feb 13, 2025
Merged

Refactor Cleanup class #47

merged 7 commits into from
Feb 13, 2025

Conversation

ondrajodas
Copy link
Contributor

@ondrajodas ondrajodas commented Feb 4, 2025

  • Optimize code in Cleanup class: Add constants for frequently used strings, Simplify logic in getDataToRemove, Create switchRole method for better readability
  • Add detailed logging: Info logs for main operations, Debug logs for detailed tracking
  • Improve type safety: Fix type hints, Proper handling of null values

@ondrajodas
Copy link
Contributor Author

po incidentu kdy nám migrace smazala všechny usery (i ty co to smazat nemělo) jsem to přepsal tak aby to vzalo jen databázovou roli `tu najdu buď od databáze a nebo ze zadaného pole) a našlo to jen usery které ta role vlastní (WS users) + role která ta role vlastní (RO role, WS role)

je to víc přímočarý a jednodušší a nemělo by se tak stt že to smaže něco co nemá...

příklad výstupu:

USE ROLE "SAPI_10346";
REVOKE SELECT ON FUTURE TABLES IN SCHEMA SAPI_10346."in.c-as" FROM ROLE "SAPI_10346_RO";
REVOKE SELECT ON FUTURE TABLES IN SCHEMA SAPI_10346."in.c-keboola-ex-db-snowflake-1223319156" FROM ROLE "SAPI_10346_RO";
REVOKE SELECT ON FUTURE TABLES IN SCHEMA SAPI_10346."in.c-test" FROM ROLE "SAPI_10346_RO";
DROP USER IF EXISTS "SAPI_WORKSPACE_1225392703";
DROP USER IF EXISTS "SAPI_WORKSPACE_1225392706";
DROP USER IF EXISTS "SAPI_WORKSPACE_1225392709";
DROP USER IF EXISTS "SAPI_WORKSPACE_1225392716";
USE ROLE "KEBOOLA_STORAGE";
DROP USER IF EXISTS "SAPI_10346";
USE ROLE "SAPI_10346";
DROP ROLE IF EXISTS "SAPI_10346_RO";
DROP ROLE IF EXISTS "SAPI_WORKSPACE_1225392703";
DROP ROLE IF EXISTS "SAPI_WORKSPACE_1225392706";
DROP ROLE IF EXISTS "SAPI_WORKSPACE_1225392709";
DROP ROLE IF EXISTS "SAPI_WORKSPACE_1225392716";
USE ROLE "KEBOOLA_STORAGE";
DROP ROLE IF EXISTS "SAPI_10346";
DROP DATABASE IF EXISTS "SAPI_10346_OLD";
ALTER DATABASE IF EXISTS "SAPI_10346" RENAME TO "SAPI_10346_OLD";

@ondrajodas
Copy link
Contributor Author

dal jsem na review i TF, se kterým jsem ten tool dělal a byl i u toho incidentu :)

romantmb
romantmb previously approved these changes Feb 6, 2025
Copy link
Contributor

@romantmb romantmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Za mě ok, ale úplně se neorientuju, počkal bych ještě na @tomasfejfar

src/Cleanup.php Outdated Show resolved Hide resolved
tomasfejfar
tomasfejfar previously approved these changes Feb 7, 2025
Copy link
Contributor

@tomasfejfar tomasfejfar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jako působí to, že to bude fungovat :) Akorát ten naming je teda 🙈

@@ -33,7 +37,14 @@ public function sourceAccount(): void
$databaseRole = $this->sourceConnection->getOwnershipRoleOnDatabase($database);
$projectUser = $this->getProjectUser($databaseRole);

$data = $this->getDataToRemove($this->sourceConnection, $databaseRole);
$grantedOnDatabaseRole = $this->sourceConnection->fetchAll(sprintf(
'SHOW GRANTS ON ROLE %s',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Myslel jsem, že jet o TO ROLE? ale funguje podle dokumentace oboje :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tohle teda vezme všechny granty, která má project role

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHOW GRANTS ON ROLE KEBOOLA_123

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tohle souvisí i s tím druhým commentem
SHOW GRANT TO ROLE KEBOOLA_123 - načte všechny granty vlastněné touto rolí
image

SHOW GRANT ON ROLE KEBOOLA_123 - načte granty které vlastní tuto roli - takže KEBOOLA_STORAGE
image

@@ -33,7 +37,14 @@ public function sourceAccount(): void
$databaseRole = $this->sourceConnection->getOwnershipRoleOnDatabase($database);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Owner DB je "project role".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KEBOOLA_123

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jo je...jo je to role která vlastní databázi, ano projektová role no

'SHOW GRANTS ON ROLE %s',
Helper::quoteIdentifier($databaseRole)
));
assert(count($grantedOnDatabaseRole) === 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nrozumím proč je jen jeden ten grant. Může jich být imho klidně víc - třeba na ws usery, schemata, ws role, etc., ne?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jako je super, že je tu assert, ale nerozumím proč to funguje

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funguje to protože - #47 (comment)

src/Cleanup.php Outdated Show resolved Hide resolved
src/Cleanup.php Outdated Show resolved Hide resolved
src/Cleanup.php Outdated
);
$this->destinationConnection->useRole($role['granted_by']);
}
$dataToRemove = $this->getDataToRemove($this->destinationConnection, $roleName, $mainRoleName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A co projektový user KEBOOLA_123?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jestli myslíš smazání ho tak se přidává v metodě "addProjectRoleAndUserToRemove"

src/Cleanup.php Outdated
$sqls[] = sprintf('USE ROLE %s;', Helper::quoteIdentifier($currentRole));
// Drop users owned by roles
if (!empty($dataToRemove['USER'])) {
$this->logger->info(sprintf('Dropping %d users', count($dataToRemove['USER'])));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady bych zalogoval které přesně to dropuje, ne?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to samé - #47 (comment)

src/Cleanup.php Outdated
$sqls[] = sprintf('DROP ROLE IF EXISTS %s;', Helper::quoteIdentifier($databaseRole));
// Drop roles
if (!empty($dataToRemove['ROLE'])) {
$this->logger->info(sprintf('Dropping %d roles', count($dataToRemove['ROLE'])));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taky bych si myslel, že by tu mělo být které přesně

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no nevím jestli bych to tu měl vypsat 🤷‍♂️ seznam který chceme migrovat těch rolí a userů má několik stovek/tisíc

src/Cleanup.php Outdated
@@ -297,36 +306,100 @@ public function postMigration(): void
}
}

private function getDataToRemove(Connection $connection, string $role): array
private function getDataToRemove(Connection $connection, string $name, string $mainRoleName): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tyjo, ten naming... $roleName, ne?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a getDataToRemoveForRole

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming zajišťoval cursor, tak jsem se svezl... přejmenuju

src/Cleanup.php Outdated Show resolved Hide resolved
@ondrajodas ondrajodas dismissed stale reviews from tomasfejfar and romantmb via 3af4bb3 February 10, 2025 21:47
tomasfejfar
tomasfejfar previously approved these changes Feb 10, 2025
Copy link
Contributor

@tomasfejfar tomasfejfar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asi LGTM :)

@ondrajodas
Copy link
Contributor Author

@tomasfejfar sry ještě jednou.. on je tu dismiss když pošlu commit 🤦

@ondrajodas ondrajodas merged commit ed21240 into main Feb 13, 2025
4 checks passed
@ondrajodas ondrajodas deleted the premigration-cleanup-refactoring branch February 13, 2025 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants