Skip to content

Commit

Permalink
Merge pull request #3064 from uselagoon/feature/api-fact-table-unique…
Browse files Browse the repository at this point in the history
…ness-with-service-column
  • Loading branch information
tobybellwood authored Mar 15, 2022
2 parents b9bb785 + 086926d commit 22adede
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion services/api-db/docker-entrypoint-initdb.d/00-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ CREATE TABLE IF NOT EXISTS task_file (
CREATE TABLE IF NOT EXISTS environment_fact (
id int NOT NULL auto_increment PRIMARY KEY,
environment int REFERENCES environment (id),
service varchar(300) NULL,
name varchar(300) NOT NULL,
value varchar(300) NOT NULL,
type ENUM('TEXT', 'URL', 'SEMVER') DEFAULT 'TEXT',
Expand All @@ -267,7 +268,7 @@ CREATE TABLE IF NOT EXISTS environment_fact (
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
category TEXT NULL DEFAULT '',
key_fact TINYINT(1) NOT NULL DEFAULT(0),
UNIQUE(environment, name)
CONSTRAINT environment_fact UNIQUE(environment, name, source)
);

CREATE TABLE IF NOT EXISTS environment_fact_reference (
Expand Down
35 changes: 35 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 @@ -1094,6 +1094,39 @@ CREATE OR REPLACE PROCEDURE
END;
$$

CREATE OR REPLACE PROCEDURE
update_unique_environment_fact()

BEGIN
IF EXISTS(
SELECT null FROM INFORMATION_SCHEMA.STATISTICS WHERE
TABLE_NAME = 'environment_fact' and INDEX_NAME = 'environment'
) THEN
ALTER TABLE `environment_fact`
DROP INDEX `environment`;
ALTER TABLE `environment_fact` ADD CONSTRAINT environment_fact_unique UNIQUE(environment, name, source);
END IF;
END;
$$

CREATE OR REPLACE PROCEDURE
add_service_to_environment_fact()

BEGIN
IF NOT EXISTS(
SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'environment_fact'
AND table_schema = 'infrastructure'
AND column_name = 'service'
) THEN
ALTER TABLE `environment_fact`
ADD `service` varchar(300) NULL;
END IF;
END;
$$

CREATE OR REPLACE PROCEDURE
update_user_password()

Expand Down Expand Up @@ -1585,6 +1618,8 @@ CALL add_fact_source_and_description_to_environment_fact();
CALL add_fact_type_to_environment_fact();
CALL add_fact_category_to_environment_fact();
CALL add_fact_key_to_environment_fact();
CALL update_unique_environment_fact();
CALL add_service_to_environment_fact();
CALL add_metadata_to_project();
CALL add_content_type_to_project_notification();
CALL convert_project_production_routes_to_text();
Expand Down
13 changes: 8 additions & 5 deletions services/api/src/resources/fact/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const getEnvironmentsByFactSearch: ResolverFn = async (

export const addFact: ResolverFn = async (
root,
{ input: { id, environment: environmentId, name, value, source, description, type, category, keyFact } },
{ input: { id, environment: environmentId, name, value, source, description, type, category, keyFact, service } },
{ sqlClientPool, hasPermission, userActivityLogger }
) => {
const environment = await environmentHelpers(
Expand All @@ -204,7 +204,8 @@ export const addFact: ResolverFn = async (
description,
type,
keyFact,
category
category,
service
}),
);

Expand All @@ -222,7 +223,8 @@ export const addFact: ResolverFn = async (
name,
value,
source,
description
description,
service
}
}
});
Expand Down Expand Up @@ -255,7 +257,7 @@ export const addFacts: ResolverFn = async (

const returnFacts = [];
for (let i = 0; i < facts.length; i++) {
const { environment, name, value, source, description, type, category, keyFact } = facts[i];
const { environment, name, value, source, description, type, category, keyFact, service } = facts[i];
const {
insertId
} = await query(
Expand All @@ -268,7 +270,8 @@ export const addFacts: ResolverFn = async (
description,
type,
keyFact,
category
category,
service
}),
);

Expand Down
7 changes: 4 additions & 3 deletions services/api/src/resources/fact/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const standardFactReturn = {
description: 'description',
type: 'type',
category: 'category',
keyFact: 'keyFact'
keyFact: 'keyFact',
service: 'service',
};

export const Sql = {
Expand All @@ -37,8 +38,8 @@ export const Sql = {

return q.orderBy('f.id', 'asc').toString()
},
insertFact: ({ environment, name, value, source, description, type, category, keyFact }) =>
knex('environment_fact').insert({ environment, name, value, source, description, type, category, keyFact }).toString(),
insertFact: ({ environment, name, value, source, description, type, category, keyFact, service }) =>
knex('environment_fact').insert({ environment, name, value, source, description, type, category, keyFact, service }).toString(),
deleteFact: (environment, name) =>
knex('environment_fact')
.where({
Expand Down
3 changes: 3 additions & 0 deletions services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ const typeDefs = gql`
type: FactType
category: String
references: [FactReference]
service: String
}
input AddFactInput {
Expand All @@ -279,6 +280,7 @@ const typeDefs = gql`
keyFact: Boolean
type: FactType
category: String
service: String
}
input AddFactsInput {
Expand All @@ -294,6 +296,7 @@ const typeDefs = gql`
keyFact: Boolean
type: FactType
category: String
service: String
}
input UpdateFactInput {
Expand Down

0 comments on commit 22adede

Please sign in to comment.