Skip to content

feat: Set Primary Key(s) for table#20437

Merged
kodiakhq[bot] merged 8 commits intomainfrom
override-pk
Mar 25, 2025
Merged

feat: Set Primary Key(s) for table#20437
kodiakhq[bot] merged 8 commits intomainfrom
override-pk

Conversation

@bbernays
Copy link
Copy Markdown
Collaborator

Summary

⚠️ If you're contributing to a plugin please read this section of the contribution guidelines 🧑‍🎓 before submitting this PR ⚠️

@bbernays bbernays requested review from a team, blesniewski and murarustefaan March 24, 2025 14:32
Comment on lines +107 to +111
for _, oldPk := range oldPks {
if !slices.Contains(newPks, oldPk) {
table.Columns.Get(oldPk).PrimaryKey = false
}
}
Copy link
Copy Markdown
Member

@murarustefaan murarustefaan Mar 24, 2025

Choose a reason for hiding this comment

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

Should we actually have it as a single-source-of-truth for PKs, or rather allow toggling of extra fields as PKs?
It might be better not to remove the PKs set by the plugins, but allow adding more. Wdyt?

cc @erezrokah

Copy link
Copy Markdown
Member

@erezrokah erezrokah Mar 24, 2025

Choose a reason for hiding this comment

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

We probably need both, add PKs and remove PKs directives, also how would this look like in the spec? I don't think this PR exposes it? (For example renovate has both labels and addLabels configurations (https://docs.renovatebot.com/configuration-options/#addlabels)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This would be the spec a user would use:

kind: transformer
spec:
  name: "basic"
  path: "cloudquery/basic"
  version: v2.1.0
  spec:
    transformations:
      - kind: set_primary_keys
        tables: ["*"]
        columns: ["_cq_id", "_cq_source_name"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah exactly, I think providing the ability to add PKs columns is the way to go, rather then specify the only columns that should be PKs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changed to AddPrimaryKeys

Copy link
Copy Markdown
Member

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

@bbernays
Copy link
Copy Markdown
Collaborator Author

461f7dd

Done 461f7dd

for _, newPk := range newPks {
newCol := table.Columns.Get(newPk)
if newCol == nil {
return nil, errors.New("new primary key column not found in schema")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add the missing column name to the error so it's easier to know what caused the error? Maybe the table name too

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also can we make any column type a PK? Maybe support only the primitive types?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I updated the error in b85b61a

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I would stay away from restricting the columns that can be used as a PK as this is an advanced feature... It all depends on how the destination handles a type. For example SQLite stringifies all complex columns so technically they could be used for a PK

Copy link
Copy Markdown
Member

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

One last question is about JSON Path support. We support it for removal and obfuscation, will this work here too? In case someone wants to use an internal JSON property as a PK?

I guess you'd have to flatten the JSON first?

@bbernays
Copy link
Copy Markdown
Collaborator Author

One last question is about JSON Path support. We support it for removal and obfuscation, will this work here too? In case someone wants to use an internal JSON property as a PK?

I guess you'd have to flatten the JSON first?

Yeah I think that is correct

@bbernays bbernays added the automerge Automatically merge once required checks pass label Mar 25, 2025
@kodiakhq kodiakhq bot merged commit 3d92b5d into main Mar 25, 2025
14 checks passed
@kodiakhq kodiakhq bot deleted the override-pk branch March 25, 2025 15:46
kodiakhq bot pushed a commit that referenced this pull request Mar 25, 2025
🤖 I have created a release *beep* *boop*
---


## [2.1.0](plugins-transformer-basic-v2.0.4...plugins-transformer-basic-v2.1.0) (2025-03-25)


### Features

* Set Primary Key(s) for table ([#20437](#20437)) ([3d92b5d](3d92b5d))


### Bug Fixes

* **deps:** Update module github.com/apache/arrow-go/v18 to v18.2.0 ([#20410](#20410)) ([ee081fb](ee081fb))
* **deps:** Update module github.com/cloudquery/plugin-sdk/v4 to v4.74.2 ([#20434](#20434)) ([8db20d6](8db20d6))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Automatically merge once required checks pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants