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

Optimize getBackupsByEnvironmentId #1223

Merged
merged 6 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion node-packages/commons/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ const getEnvironmentBackups = (openshiftProjectName: string): Promise<Project[]>
name
}
backups {
...${backupFragment}
id
backupId
source
created
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions services/api-db/docker-entrypoint-initdb.d/01-migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,24 @@ CREATE OR REPLACE PROCEDURE
END;
$$

CREATE OR REPLACE PROCEDURE
add_index_for_environment_backup_environment()

BEGIN
IF NOT EXISTS (
SELECT NULL
FROM INFORMATION_SCHEMA.STATISTICS
WHERE
table_name = 'environment_backup'
AND table_schema = 'infrastructure'
AND index_name='backup_environment'
) THEN
ALTER TABLE `environment_backup`
ADD INDEX `backup_environment` (`environment`);
END IF;
END;
$$

DELIMITER ;

CALL add_production_environment_to_project();
Expand Down Expand Up @@ -739,6 +757,7 @@ CALL add_deploy_base_head_ref_title_to_environment();
CALL convert_env_vars_from_varchar_to_text();
CALL convert_user_ssh_key_usid_to_char();
CALL add_private_key_to_project();
CALL add_index_for_environment_backup_environment();

-- Drop legacy SSH key procedures
DROP PROCEDURE IF EXISTS CreateProjectSshKey;
Expand Down
2 changes: 2 additions & 0 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const {
getEnvironmentHitsMonthByEnvironmentId,
getEnvironmentByDeploymentId,
getEnvironmentByTaskId,
getEnvironmentByBackupId,
getEnvironmentServicesByEnvironmentId,
setEnvironmentServices,
deleteEnvironment,
Expand Down Expand Up @@ -199,6 +200,7 @@ const resolvers /* : { [string]: ResolversObj | typeof GraphQLDate } */ = {
},
Backup: {
restore: getRestoreByBackupId,
environment: getEnvironmentByBackupId,
},
Query: {
userBySshKey: getUserBySshKey,
Expand Down
6 changes: 2 additions & 4 deletions services/api/src/resources/backup/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ const Sql /* : SqlObj */ = {
{ environmentId, includeDeleted } /* : { environmentId: number, includeDeleted: boolean } */,
) => {
const query = knex('environment_backup')
.join('environment as e', 'e.id', '=', 'environment_backup.environment')
.select('environment_backup.*')
.where('e.id', environmentId);
.where('environment', environmentId);

if (includeDeleted) {
return query.toString();
}

return query.where('environment_backup.deleted', '=', '0000-00-00 00:00:00').toString();
return query.where('deleted', '=', '0000-00-00 00:00:00').toString();
},
insertBackup: (
{
Expand Down
36 changes: 36 additions & 0 deletions services/api/src/resources/environment/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,41 @@ const getEnvironmentByTaskId = async (
return environment;
};

const getEnvironmentByBackupId = async (
{ id: backup_id },
args,
{
sqlClient,
hasPermission,
},
) => {
const prep = prepare(
sqlClient,
`SELECT
e.*
FROM environment_backup eb
JOIN environment e on eb.environment = e.id
JOIN project p ON e.project = p.id
WHERE eb.id = :backup_id
LIMIT 1
`,
);

const rows = await query(sqlClient, prep({ backup_id }));

const environment = rows[0];

if (!environment) {
return null;
}

await hasPermission('environment', 'view', {
project: environment.project,
});

return environment;
};

const getEnvironmentStorageByEnvironmentId = async (
{ id: eid },
args,
Expand Down Expand Up @@ -764,6 +799,7 @@ const Resolvers /* : ResolversObj */ = {
getEnvironmentHitsMonthByEnvironmentId,
getEnvironmentByDeploymentId,
getEnvironmentByTaskId,
getEnvironmentByBackupId,
getEnvironmentServicesByEnvironmentId,
setEnvironmentServices,
deleteEnvironment,
Expand Down