Skip to content

feat: add schema-aligned db types and test factory primitives#4326

Open
skwowet wants to merge 11 commits into
mainfrom
test-kit-primitives
Open

feat: add schema-aligned db types and test factory primitives#4326
skwowet wants to merge 11 commits into
mainfrom
test-kit-primitives

Conversation

@skwowet

@skwowet skwowet commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

Introduces schema-aligned DB types, DAL batch insert helpers, and test-kit factory primitives. Shared foundation for DB-backed tests and better type safety when reading/writing persisted data across backend, workers, DAL, and test utilities.

Why

Entity shapes are duplicated and drift across @crowd/types, DAL inputs, merge helpers, and API models — often with wrong nullability and incomplete field sets.

This PR starts fixing that by anchoring types to the database schema and routing inserts through consistent DAL helpers. Tests build on the same paths production code uses, instead of parallel ad-hoc shapes.

Changes

Types (@crowd/types/src/db)

  • Add *DbRow / *DbInsert types for members, identities, organizations, segments, activity relations, member organizations, and related tables
  • Derive write payloads from row types (Pick, Partial, Omit) — no more hand-maintained duplicates
  • Trim overlapping org types from organizations.ts

Test kit (@crowd/test-kit)

  • Reorganize into db/ (postgres, withQx) and factories/
  • create* — persist via DAL, no faker or hidden scaffolding
  • with*Defaults — opt-in fill for harmless fields only; tests own scenario-defining data
  • Factories for member, org, segment, identities, member-organization, activity relations, etc.

DAL

  • Batch insert helpers aligned with *DbInsert: insertMemberIdentities, insertOrganizationIdentities, insertOrganizations, insertSegments
  • Rename insertManyMemberIdentitiesinsertMemberIdentities, addOrgIdentityinsertOrganizationIdentities
  • Skip segments.type on insert (generated column)
  • changeMemberOrganizationAffiliationOverrides supports returnRows
  • Deep-import activityTypes to avoid pulling the integrations barrel in tests

Call sites

  • Update createMember, organizationRepository, enrichment.ts, member.repo.ts to use new helpers

Docs

  • ADR-0006: schema types as source of truth
  • ADR-0007: factory primitives + opt-in defaults

Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
@skwowet skwowet self-assigned this Jul 12, 2026
Copilot AI review requested due to automatic review settings July 12, 2026 16:24
Comment thread services/libs/data-access-layer/package.json
Comment thread services/libs/test-kit/package.json
Comment thread services/libs/test-kit/package.json
Comment thread services/libs/test-kit/package.json

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces schema-aligned persistence types, DAL batch helpers, and reusable database test factories.

Changes:

  • Adds canonical *DbRow and *DbInsert types.
  • Adds batch DAL inserts and migrates existing callers.
  • Adds test-kit factories, fixtures, and supporting ADRs.

Review note: Blocking issues include removed DAL exports breaking existing workers, malformed empty segment inserts, and factory contracts that do not preserve required inputs. The PR title also lacks the required JIRA key.

Reviewed changes

Copilot reviewed 47 out of 49 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
services/libs/types/src/organizations.ts Removes superseded identity input type.
services/libs/types/src/index.ts Exports database types.
services/libs/types/src/db/segments.ts Defines segment row/insert types.
services/libs/types/src/db/organizations.ts Defines organization row/insert types.
services/libs/types/src/db/organization-identities.ts Defines organization identity types.
services/libs/types/src/db/members.ts Defines member row/insert types.
services/libs/types/src/db/member-segment-affiliations.ts Defines segment affiliation types.
services/libs/types/src/db/member-organizations.ts Defines member-organization types.
services/libs/types/src/db/member-organization-affiliation-overrides.ts Defines override types.
services/libs/types/src/db/member-identities.ts Defines member identity types.
services/libs/types/src/db/index.ts Exports DB type modules.
services/libs/types/src/db/activity-relations.ts Defines activity relation types.
services/libs/test-kit/src/types.ts Removes obsolete PostgreSQL type.
services/libs/test-kit/src/faker.ts Exposes Faker.
services/libs/test-kit/src/factories/segment.ts Adds hierarchical segment factory.
services/libs/test-kit/src/factories/organization.ts Adds organization factory.
services/libs/test-kit/src/factories/organization-identity.ts Adds organization identity factory.
services/libs/test-kit/src/factories/member.ts Adds member factory.
services/libs/test-kit/src/factories/member-segment-affiliation.ts Adds segment affiliation factory.
services/libs/test-kit/src/factories/member-organization.ts Adds member-organization factory.
services/libs/test-kit/src/factories/member-organization-affiliation-override.ts Adds override factory.
services/libs/test-kit/src/factories/member-identity.ts Adds member identity factory.
services/libs/test-kit/src/factories/index.ts Exports factories.
services/libs/test-kit/src/factories/defaults.ts Adds default application utility.
services/libs/test-kit/src/factories/activity-relation.ts Adds activity relation helpers.
services/libs/test-kit/src/db/qx.ts Adds Vitest query-executor fixture.
services/libs/test-kit/src/db/postgres.ts Localizes PostgreSQL configuration type.
services/libs/test-kit/src/db/index.ts Exports database test utilities.
services/libs/test-kit/package.json Adds exports and dependencies.
services/libs/data-access-layer/src/segments/index.ts Adds segment batch insertion.
services/libs/data-access-layer/src/organizations/identities.ts Adds organization identity batching.
services/libs/data-access-layer/src/organizations/base.ts Adds organization batch insertion.
services/libs/data-access-layer/src/old/apps/data_sink_worker/repo/settings.repo.ts Uses targeted integrations import.
services/libs/data-access-layer/src/old/apps/data_sink_worker/repo/member.repo.ts Uses renamed identity helper.
services/libs/data-access-layer/src/members/organizations.ts Adds member-organization batching.
services/libs/data-access-layer/src/members/identities.ts Renames and aligns identity insertion.
services/libs/data-access-layer/src/members/base.ts Aligns member creation with DB types.
services/libs/data-access-layer/src/member-organization-affiliation/index.ts Supports returning override rows.
services/libs/data-access-layer/src/member_segment_affiliations/index.ts Adds affiliation batch insertion.
services/libs/data-access-layer/src/index.ts Updates DAL barrel exports.
services/libs/data-access-layer/src/activities/sql.ts Uses targeted display-service import.
services/libs/data-access-layer/package.json Adds test tooling dependencies.
services/apps/members_enrichment_worker/src/activities/enrichment.ts Batches organization identities.
pnpm-lock.yaml Updates dependency resolution.
docs/adr/README.md Indexes the new ADRs.
docs/adr/0007-test-factory-primitives-and-defaults.md Documents factory design.
docs/adr/0006-database-schema-types-as-source-of-truth.md Documents schema-type policy.
backend/src/database/repositories/organizationRepository.ts Uses batched identity insertion.
backend/src/api/public/v1/members/createMember.ts Uses renamed identity insertion helper.
Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread services/libs/data-access-layer/src/index.ts
Comment thread services/libs/data-access-layer/src/segments/index.ts Outdated
Comment thread services/libs/test-kit/src/factories/defaults.ts Outdated
Comment thread services/libs/test-kit/src/factories/organization-identity.ts Outdated
Comment thread services/libs/test-kit/src/factories/activity-relation.ts
Comment thread services/libs/test-kit/src/factories/member-organization-affiliation-override.ts Outdated
Comment thread docs/adr/0007-test-factory-primitives-and-defaults.md Outdated
Copilot AI review requested due to automatic review settings July 12, 2026 16:33
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 47 out of 49 changed files in this pull request and generated 9 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file
Comments suppressed due to low confidence (1)

services/libs/data-access-layer/src/index.ts:27

  • These removals break existing root imports from this package. For example, packages_worker imports BqStats/OsspckgsJobKind from osspckgs/types, recordDownloadSnapshot from osspckgs/downloads, and the RubyGems helpers from osspckgs/rubygems via @crowd/data-access-layer; those exports disappear after this change and the affected workspaces will no longer type-check. Restore the public exports (or migrate every consumer in this PR).
export * from './osspckgs/stewardships'
export * from './osspckgs/types'
export * from './osspckgs/versions'

Comment thread services/libs/data-access-layer/src/segments/index.ts Outdated
Comment thread services/libs/test-kit/src/factories/defaults.ts Outdated
Comment thread services/libs/types/src/db/segments.ts Outdated
Comment thread services/libs/test-kit/src/factories/segment.ts Outdated
Comment thread services/libs/test-kit/src/factories/member-organization-affiliation-override.ts Outdated
Comment thread services/libs/test-kit/src/factories/member-organization-affiliation-override.ts Outdated
Comment thread services/libs/test-kit/src/factories/organization-identity.ts Outdated
Comment thread services/libs/test-kit/src/factories/member-identity.ts Outdated
Comment thread services/libs/data-access-layer/src/members/base.ts
Copilot AI review requested due to automatic review settings July 12, 2026 16:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 47 out of 49 changed files in this pull request and generated 6 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

Comment thread services/libs/test-kit/src/factories/defaults.ts Outdated
Comment thread services/libs/test-kit/src/factories/organization-identity.ts Outdated
Comment thread services/libs/test-kit/src/factories/activity-relation.ts
Comment thread services/libs/data-access-layer/src/segments/index.ts Outdated
Comment thread services/libs/test-kit/src/factories/member-organization-affiliation-override.ts Outdated
Comment thread docs/adr/0007-test-factory-primitives-and-defaults.md Outdated
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 12, 2026 17:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 48 out of 50 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

Comment thread services/libs/data-access-layer/src/segments/index.ts Outdated
Comment thread services/libs/test-kit/src/factories/member-organization-affiliation-override.ts Outdated
@skwowet skwowet marked this pull request as ready for review July 13, 2026 06:23
skwowet added 2 commits July 13, 2026 15:22
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 13, 2026 10:18
@skwowet skwowet requested review from mbani01 and ulemons July 13, 2026 10:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 50 out of 52 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

Comment thread services/libs/test-kit/src/factories/member-organization-affiliation-override.ts Outdated
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 13, 2026 11:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 55 out of 57 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

Comment thread services/libs/types/src/db/member-identities.ts
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 13, 2026 11:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 56 out of 58 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

Comment thread services/libs/data-access-layer/src/members/identities.ts
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 13, 2026 11:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 56 out of 58 changed files in this pull request and generated 3 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

Comment thread services/libs/types/src/db/member-identities.ts
Comment thread services/libs/types/src/db/organizations.ts
Comment thread services/libs/test-kit/src/factories/activity-relation.ts
…liation inserts

Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 13, 2026 12:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 56 out of 58 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file


## Decision

Treat test setup as **composition of sharp primitives**, not smart world-builders. Keep three layers separate.
TinybirdClient,
} from '@crowd/database'
import { ActivityDisplayService } from '@crowd/integrations'
import { ActivityDisplayService } from '@crowd/integrations/src/integrations/activityDisplayService'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just out of curiosity, is there any reason why we changed this import ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants