Skip to content

Add assignAll and copyAll#1623

Open
cdfa wants to merge 2 commits intoyesodweb:masterfrom
chordify:persistfield-in-tabulate
Open

Add assignAll and copyAll#1623
cdfa wants to merge 2 commits intoyesodweb:masterfrom
chordify:persistfield-in-tabulate

Conversation

@cdfa
Copy link

@cdfa cdfa commented Mar 2, 2026

This PR adds 2 functions assignAll and copyAll intended to be used when you need to insert a record into the database, and if it already exists, completely overwrite it. These can be implemented generically if we add a PersistField a constraint in tabulateEntityA. I think it's unlikely that this breaks existing code, so I propose a minor version increment.

The implementation is slightly hacky because the tabulateEntityA wants to build an Entity, but neither assignAll nor copyAll require full entity data. I think adding a method to PersistEntity similar to fieldLens which works over record instead of Entity record is also worth considering, but for now I kept with the minimal change.

I also fixed the nix flake to account for the "new" persistent-postgresql => postgresql-simple-interval>=1 dependency.


Before submitting your PR, check that you've:

  • Documented new APIs with Haddock markup
  • Added @since declarations to the Haddock
  • Ran fourmolu on any changed files (restyled will do this for you, so
    accept the suggested changes if it makes them)
  • Adhered to the code style (see the .editorconfig and fourmolu.yaml files for details)

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Bumped the version number if there isn't an (unreleased) on the Changelog
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

tabulateEntityA
:: (Applicative f)
=> (forall a. EntityField record a -> f a)
=> (forall a. PersistField a => EntityField record a -> f a)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't an acceptable change imo

Copy link
Collaborator

Choose a reason for hiding this comment

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

we should have something like FieldDict in prairie - then use-sites would have FieldDict PersistField rec

Copy link
Author

Choose a reason for hiding this comment

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

This isn't an acceptable change imo

Can you explain why? Even if there is a better way to support functions like assignAll and copyAll, does it hurt to add this constraint?

I'm not familiar with prairie, but it seems doing it properly would involve replacing PersistField with prairie's Field. That's a bigger change then what I'm comfortable taking on at the moment.

Comment on lines +1593 to +1601
copyAll = snd $ runWriter $ tabulateEntityA $ \field ->
-- This assumes the tabulateEntityA implementation is not strict in the
-- returned field value (and the default implementation indeed isn't).
error "copyAll: field value was used"
<$ when
( fieldHaskell (persistFieldDef field)
/= fieldHaskell (persistFieldDef @record persistIdField)
)
(tell [copyField field])
Copy link
Collaborator

Choose a reason for hiding this comment

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

this form feels very strange- i believe there should be a better way to write that 🤔 I know prairie has the right utilities but I'm not sure if persistent has them

Copy link
Author

Choose a reason for hiding this comment

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

I don't know of any persistent utilities that could help here, but I'm open to more specific suggestions.

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.

2 participants