Skip to content

Commit

Permalink
Revert "Don't use account usage for rowcount"
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrajodas authored Oct 25, 2024
1 parent a1c9e2e commit e6bbc2b
Showing 1 changed file with 22 additions and 39 deletions.
61 changes: 22 additions & 39 deletions src/MigrationChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,28 @@ public function postMigrationCheckStructure(Role $mainRoleWithGrants): void
$rolesAndUsers,
['users' => [$databaseRole], 'roles' => [$databaseRole]]
);

$compares = [];
// phpcs:disable Generic.Files.LineLength
// Compare TABLES
$compares[] = [
'group' => 'Tables',
'itemNameKey' => 'TABLE_NAME',
'sql' => sprintf(
'SELECT %s FROM SNOWFLAKE.ACCOUNT_USAGE.TABLES WHERE DELETED IS NULL AND TABLE_CATALOG = %s ORDER BY TABLE_SCHEMA, TABLE_NAME;',
implode(',', [
'CONCAT(TABLE_SCHEMA, \'.\', TABLE_NAME) AS ID',
'TABLE_NAME',
'TABLE_SCHEMA',
'TABLE_OWNER',
'TABLE_TYPE',
'ROW_COUNT',
// 'BYTES',
]),
QueryBuilder::quote($database)
),
];

// Compare USERS
$compares[] = [
'group' => 'Users',
Expand Down Expand Up @@ -122,43 +142,10 @@ public function postMigrationCheckStructure(Role $mainRoleWithGrants): void
implode(', ', array_map(fn($v) => QueryBuilder::quote($v), $rolesAndUsers['roles']))
),
];
// Compare TABLES
$schemas = $this->sourceConnection->fetchAll(sprintf(
'SHOW SCHEMAS IN DATABASE %s',
Helper::quoteIdentifier($database)
));
foreach ($schemas as $schema) {
$tables = $this->sourceConnection->fetchAll(sprintf(
'SHOW TABLES IN SCHEMA %s.%s',
Helper::quoteIdentifier($database),
Helper::quoteIdentifier($schema['name'])
));
foreach ($tables as $table) {
$compares[] = [
'group' => sprintf('Table: %s.%s.%s', $database, $schema['name'], $table['name']),
'itemNameKey' => 'ID',
'sql' => sprintf(
'SELECT \'%s.%s.%s\' AS ID, count(*) AS ROW_COUNT FROM %s.%s.%s',
Helper::quoteIdentifier($database),
Helper::quoteIdentifier($schema['name']),
Helper::quoteIdentifier($table['name']),
Helper::quoteIdentifier($database),
Helper::quoteIdentifier($schema['name']),
Helper::quoteIdentifier($table['name'])
),
'role' => $databaseRole,
];
}
}
// phpcs:enable Generic.Files.LineLength

foreach ($compares as $compare) {
$this->compareData(
$compare['group'],
$compare['itemNameKey'],
$compare['sql'],
array_key_exists('role', $compare) ? $compare['role'] : null
);
$this->compareData($compare['group'], $compare['itemNameKey'], $compare['sql']);
}
}
}
Expand Down Expand Up @@ -299,12 +286,8 @@ public function postMigrationCheckData(Role $mainRoleWithGrants): void
}
}

private function compareData(string $group, string $itemNameKey, string $sql, ?string $role = null): void
private function compareData(string $group, string $itemNameKey, string $sql): void
{
if ($role) {
$this->sourceConnection->useRole($role);
$this->destinationConnection->useRole($role);
}
$this->logger->info(sprintf('Getting source data for "%s".', $group));
$sourceData = $this->sourceConnection->fetchAll($sql);
$this->logger->info(sprintf('Getting target data for "%s".', $group));
Expand Down

0 comments on commit e6bbc2b

Please sign in to comment.