Skip to content

Commit

Permalink
Merge branch 'main' into crowd-linux
Browse files Browse the repository at this point in the history
  • Loading branch information
sausage-todd committed Sep 13, 2023
2 parents 2c2a6bd + 62c4f59 commit 456f81d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
4 changes: 3 additions & 1 deletion backend/src/bin/jobs/refreshMaterializedViewsForCube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const job: CrowdJob = {
for (const view of materializedViews) {
await logExecutionTimeV2(
() =>
dbOptions.database.sequelize.query(`REFRESH MATERIALIZED VIEW CONCURRENTLY "${view}"`),
dbOptions.database.sequelize.query(`REFRESH MATERIALIZED VIEW CONCURRENTLY "${view}"`, {
useMaster: true,
}),
log,
`Refresh Materialized View ${view}`,
)
Expand Down
3 changes: 3 additions & 0 deletions backend/src/database/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ function models() {
credentials.password,
{
dialect: DB_CONFIG.dialect,
dialectOptions: {
application_name: SERVICE,
},
port: DB_CONFIG.port,
replication: {
read: [
Expand Down
10 changes: 8 additions & 2 deletions backend/src/database/repositories/organizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,14 @@ class OrganizationRepository {
dateEnd: new Date(Math.max.apply(null, endDates)).toISOString(),
memberId: memberOrganization.memberId,
organizationId: toOrganizationId,
title: foundIntersectingRoles.length > 0 ? foundIntersectingRoles[0].title : memberOrganization.title,
source: foundIntersectingRoles.length > 0 ? foundIntersectingRoles[0].source : memberOrganization.source,
title:
foundIntersectingRoles.length > 0
? foundIntersectingRoles[0].title
: memberOrganization.title,
source:
foundIntersectingRoles.length > 0
? foundIntersectingRoles[0].source
: memberOrganization.source,
})

// we'll delete all roles that intersect with incoming org member roles and create a merged role
Expand Down
70 changes: 38 additions & 32 deletions backend/src/database/repositories/segmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,12 @@ class SegmentRepository extends RepositoryBase<
if (data.activityChannels && typeof data.activityChannels === 'object') {
if (Object.keys(data.activityChannels).length > 0) {
const replacements = {}
let valuePlaceholders = ''
const valuePlaceholders = []
Object.keys(data.activityChannels).forEach((platform) => {
data.activityChannels[platform].forEach((channel, i) => {
valuePlaceholders += data.activityChannels[platform]
.map(
() =>
`(:tenantId_${platform}_${i}, :segmentId_${platform}_${i}, :platform_${platform}_${i}, :channel_${platform}_${i})`,
)
.join(', ')
valuePlaceholders.push(
`(:tenantId_${platform}_${i}, :segmentId_${platform}_${i}, :platform_${platform}_${i}, :channel_${platform}_${i})`,
)

replacements[`tenantId_${platform}_${i}`] = this.options.currentTenant.id
replacements[`segmentId_${platform}_${i}`] = id
Expand All @@ -236,7 +233,7 @@ class SegmentRepository extends RepositoryBase<
await this.options.database.sequelize.query(
`
INSERT INTO "segmentActivityChannels" ("tenantId", "segmentId", "platform", "channel")
VALUES ${valuePlaceholders}
VALUES ${valuePlaceholders.join(', ')}
ON CONFLICT DO NOTHING;
`,
{
Expand Down Expand Up @@ -330,22 +327,24 @@ class SegmentRepository extends RepositoryBase<
const transaction = this.transaction

const records = await this.options.database.sequelize.query(
`SELECT
s.*,
json_agg(sac."activityChannels") AS "activityChannels"
`
SELECT
s.*,
jsonb_merge_agg(sac."activityChannels") AS "activityChannels"
FROM segments s
LEFT JOIN (
SELECT
"tenantId",
json_build_object(concat(platform), json_agg(sac.channel)) AS "activityChannels"
"segmentId",
jsonb_build_object(platform, json_agg(DISTINCT sac.channel)) AS "activityChannels"
FROM "segmentActivityChannels" sac
WHERE "tenantId" = :tenantId
GROUP BY "tenantId", "platform"
) sac
ON sac."tenantId" = s."tenantId"
GROUP BY "tenantId", "segmentId", "platform"
) sac ON sac."tenantId" = s."tenantId" AND sac."segmentId" = s.id
WHERE id in (:ids)
AND s."tenantId" = :tenantId
GROUP BY s.id;`,
GROUP BY s.id;
`,
{
replacements: {
ids,
Expand All @@ -357,7 +356,10 @@ class SegmentRepository extends RepositoryBase<
)

records.forEach((row) => {
row.activityChannels = Object.assign({}, ...row.activityChannels)
if (!row.activityChannels) {
row.activityChannels = {}
}
row.activityChannels = { ...row.activityChannels }
})

return records.map((sr) => SegmentRepository.populateRelations(sr))
Expand All @@ -380,22 +382,24 @@ class SegmentRepository extends RepositoryBase<
const transaction = this.transaction

const records = await this.options.database.sequelize.query(
`SELECT
s.*,
json_agg(sac."activityChannels") AS "activityChannels"
`
SELECT
s.*,
jsonb_merge_agg(sac."activityChannels") AS "activityChannels"
FROM segments s
LEFT JOIN (
SELECT
"tenantId",
json_build_object(concat(platform), json_agg(sac.channel)) AS "activityChannels"
"segmentId",
jsonb_build_object(platform, json_agg(DISTINCT sac.channel)) AS "activityChannels"
FROM "segmentActivityChannels" sac
WHERE "tenantId" = :tenantId
GROUP BY "tenantId", "platform"
) sac
ON sac."tenantId" = s."tenantId"
GROUP BY "tenantId", "segmentId", "platform"
) sac ON sac."tenantId" = s."tenantId" AND sac."segmentId" = s.id
WHERE s.id = :id
AND s."tenantId" = :tenantId
GROUP BY s.id;`,
GROUP BY s.id;
`,
{
replacements: {
id,
Expand All @@ -411,7 +415,10 @@ class SegmentRepository extends RepositoryBase<
}

const record = records[0]
record.activityChannels = Object.assign({}, ...record.activityChannels)
if (!record.activityChannels) {
record.activityChannels = {}
}
record.activityChannels = { ...record.activityChannels }

if (SegmentRepository.isProjectGroup(record)) {
// find projects
Expand Down Expand Up @@ -631,17 +638,17 @@ class SegmentRepository extends RepositoryBase<
SELECT
COUNT(DISTINCT s.id) AS count,
s.*,
json_agg(sac."activityChannels") AS "activityChannels"
jsonb_merge_agg(sac."activityChannels") AS "activityChannels"
FROM segments s
LEFT JOIN (
SELECT
"tenantId",
json_build_object(concat(platform), json_agg(sac.channel)) AS "activityChannels"
"segmentId",
jsonb_build_object(platform, json_agg(sac.channel)) AS "activityChannels"
FROM "segmentActivityChannels" sac
WHERE "tenantId" = :tenantId
GROUP BY "tenantId", "platform"
) sac
ON sac."tenantId" = s."tenantId"
GROUP BY "tenantId", "segmentId", "platform"
) sac ON sac."tenantId" = s."tenantId" AND sac."segmentId" = s.id
WHERE s."grandparentSlug" IS NOT NULL
AND s."parentSlug" IS NOT NULL
AND s."tenantId" = :tenantId
Expand All @@ -665,7 +672,6 @@ class SegmentRepository extends RepositoryBase<

const rows = subprojects
rows.forEach((row, i) => {
rows[i].activityChannels = rows[i].activityChannels[0]
if (rows[i].activityChannels === null) {
rows[i].activityChannels = {}
}
Expand Down
1 change: 1 addition & 0 deletions services/libs/database/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const getDbConnection = async (
...config,
max: maxPoolSize || 5,
query_timeout: 30000,
application_name: process.env.SERVICE || 'unknown-app',
})

await dbConnection.connect()
Expand Down

0 comments on commit 456f81d

Please sign in to comment.