rel8 icon indicating copy to clipboard operation
rel8 copied to clipboard

[Feature Request] Generate tests that can check against an actual postgres schema

Open JonathanLorimer opened this issue 2 years ago • 8 comments

It would be really nice to be able to be able to generate a test suite that will connect to a local postgres instance and verify that all TableSchema types match the postgres database's schema.

I am imagining that you would register each table schema in a list and maybe provide a pg connection string or something, and then the tests would be generated.

I am happy to help implement this, but I would definitely need lots of direction.

JonathanLorimer avatar Sep 01 '23 22:09 JonathanLorimer

This would be immensely helpful, as it's very easy to make a mistake when writing the Haskell types.

KaneTW avatar Nov 22 '23 01:11 KaneTW

Quick and dirty poc https://gist.github.com/KaneTW/0f321b6ccf0143611c43e2d087e939d3

Basically checks if the types match (i.e. bidirectional casts exists, so varchar and text match, but int8 and int2 don't), all non-nullables are present, and the hs ty has no extraneous types.

KaneTW avatar Nov 24 '23 14:11 KaneTW

Quick and dirty poc https://gist.github.com/KaneTW/0f321b6ccf0143611c43e2d087e939d3

Thanks a ton! This was super helpful.

If only #291 would become a reality. I had to fork Rel8 like you apparently did.

vst avatar May 01 '24 04:05 vst

Glad this helped. Here's an updated version that I ended up using with more detailed error handling (and some other changes that I don't remember): https://gist.github.com/KaneTW/c223d4da042f5d66e41ee27a46bcefc5

And a way to extract the schemas using the GHC API (at least as of writing that code half a year ago, this was not possible with any other method). Can also list them manually, but I found that too error-prone. https://gist.github.com/KaneTW/d8c897a86a7e6049e607f5c4246d677e

Runs like this: https://gist.github.com/KaneTW/586cd308736803efeca37fa6cb59f2d0

KaneTW avatar May 01 '24 13:05 KaneTW

That's getting better :smiley: Thanks!

vst avatar May 02 '24 11:05 vst

@KaneTW is this correct for tables which contain HLists? I'm trying to clean it up to make a PR to close the issue, as well as #186 and #311, and noticed while testing it on a table which contains an HList f (SomeRecord f) that it seems to directly recurse into the names of SomeRecord, when I think it should wrap the types of each contained name in an array. Like, it reports types like int4 not null for the Haskell side and _int4 for the database.

I'm still working through figuring out the internals though, so I'm not sure if it's a bug in your gist, or a bug in the way rel8 reports the information, or a bug in my own code, though I don't think it's the latter, simply because I've been relying on this schema for a while, and everything has been working fine (as far as I can tell).

abigailalice avatar Jul 29 '24 15:07 abigailalice

Honestly not sure. It was correct when I used it, but it could have been a case I hadn't encountered.

It's been too long so I'd have to dig into the code as well to understand it again.

KaneTW avatar Jul 29 '24 15:07 KaneTW

I think it might be an issue of TypeInfo only representing the type by a QualifiedName, while possibly being better to use Rel8.Type.Name.TypeName, which includes more information. Provided I can figure out how the HTable class works I think I might be able to fix it, assuming I'm right that this case is a bug.

abigailalice avatar Jul 29 '24 16:07 abigailalice