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

Revert "Don't use account usage for rowcount" #43

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
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