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

Add more support for non-id primary keys #1925

Merged
merged 25 commits into from
Mar 27, 2024

Conversation

MonaMayrhofer
Copy link
Contributor

@MonaMayrhofer MonaMayrhofer commented Mar 2, 2024

These are my efforts to tackle the problems described in #1924.

This PR is still a work in progress.

Goals:

  • Create unit tests for relevant code in SchemaCompiler
  • full support for arbitrary (composite) primaryKeys in SchemaCompiler
  • deleteRecord for composite primarykeys
  • deleteRecords for composite primarykeys
  • genericFetchIds for composite primarykeys
  • fix collectionFetchRelated

Other stuff that has to be done to truly support non-id pks, but isn't part of this PR, include (among others?):

  • Fix creating migrations with foreign keys to weird primary keys inside the ide
  • Create unit test for FromRow, when a composite foreign key is present
  • Implement compileFromRow support for composite foreign keys

more goals may get added when i stumble over something that doesnt work with weird primary keys.

Open questions:
- Is there currently any simple way to test database code, like (deleteRecord) either via mocking or using a testing postgres instance?

@mpscholten
Copy link
Member

Is there currently any simple way to test database code, like (deleteRecord) either via mocking or using a testing postgres instance?

The easiest way here might be to create a test IHP app with some CRUD controllers and then load the framework into the app. Check out https://github.com/digitallyinduced/ihp/blob/master/CONTRIBUTING.md#running-an-application-against-a-local-copy-of-the-framework

Replace primaryKeyCondition with a version that takes the id instead of the whole record
@MonaMayrhofer
Copy link
Contributor Author

Okay, but there's nothing that i could commit to prevent regressions, right?

IHP/ModelSupport.hs Outdated Show resolved Hide resolved
This way it supports tables with arbitrary primary keys
IHP/ModelSupport.hs Outdated Show resolved Hide resolved
IHP/ModelSupport.hs Outdated Show resolved Hide resolved
IHP/ModelSupport.hs Outdated Show resolved Hide resolved
@MonaMayrhofer
Copy link
Contributor Author

I can't find any information about formatting guidelines, and the repository doesn't seem to match ormolu or fourmolu's formatting... is there an official formatter / formatting config, or shall I format stuff by hand to match the rest as good as possible?

@mpscholten
Copy link
Member

There's no official formatting config, so you can format how it fits best 👍

IHP/ModelSupport.hs Outdated Show resolved Hide resolved
IHP/QueryBuilder.hs Outdated Show resolved Hide resolved
@MonaMayrhofer
Copy link
Contributor Author

  • Fix creating migrations with foreign keys to weird primary keys inside the ide
  • Create unit test for FromRow, when a composite foreign key is present
  • Implement compileFromRow support for composite foreign keys

I am tempted to leave these two for a seperate PR in the future, as this one is already getting quite big and convoluted...

The first one should be an easy fix, but it doesn't really fit the spirit of this PR as its in the IDE, so it belongs in a seperate PR imo...

And the last two are a) a rather weird case, which shouldn't come up all too often (a many-to-many relation with unique properties which itself has many of something else...), and b) also doesn't really fit the spirit of this PR as they are more dealing with foreign keys...

@mpscholten
Copy link
Member

I am tempted to leave these two for a seperate PR in the future, as this one is already getting quite big and convoluted...

👍

Fix incomplete instances in QueryBuilderSpec with primaryKeyConditionForId
Fix pk-columns in SchemaCompilerSpec to be unique and non-nullable for completeness
@MonaMayrhofer
Copy link
Contributor Author

I think as soon as these tests run through this is pretty much done and ready for review :D

@MonaMayrhofer MonaMayrhofer marked this pull request as ready for review March 18, 2024 10:35
IHP/ModelSupport.hs Outdated Show resolved Hide resolved
IHP/QueryBuilder.hs Outdated Show resolved Hide resolved
Test/QueryBuilderSpec.hs Show resolved Hide resolved
@MonaMayrhofer
Copy link
Contributor Author

Oh whoops, that wasn't what I meant to do

@MonaMayrhofer
Copy link
Contributor Author

That's looking better^^

Can you take a look if #dc62630 has addressed your concerns?

IHP/ModelSupport.hs Outdated Show resolved Hide resolved
@mpscholten
Copy link
Member

Can you take a look if #dc62630 has addressed your concerns?

Looks great, thanks :)

IHP/ModelSupport.hs Outdated Show resolved Hide resolved
pure ()
deleteRecordByIds :: forall record table. (?modelContext :: ModelContext, Show (PrimaryKey table), Table record, GetTableName record ~ table, record ~ GetModelByTableName table) => [Id' table] -> IO ()
deleteRecordByIds [] = do
pure () -- If there are no ids, we wouldn't even know the pkCols, so we just don't do anything, as nothing happens anyways
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment is outdated, we can know the pkCols now that we have primaryKeyConditionColumnSelector. I'll bring this up to date again

@MonaMayrhofer
Copy link
Contributor Author

It's done! Please have a look at the new changes :D

Copy link
Member

@mpscholten mpscholten left a comment

Choose a reason for hiding this comment

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

Looks like we're almost ready, just found a few small things :)

IHP/ModelSupport.hs Outdated Show resolved Hide resolved
IHP/ModelSupport.hs Outdated Show resolved Hide resolved
IHP/ModelSupport.hs Outdated Show resolved Hide resolved
@mpscholten mpscholten merged commit c88406b into digitallyinduced:master Mar 27, 2024
2 checks passed
@mpscholten
Copy link
Member

Thanks 👍 great work here!

@amitaibu
Copy link
Collaborator

@MonaMayrhofer well done 🥳

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