What’s the best way to validate that the grails domain classes are in sync with a database? It’s legacy database and i can’t build it from the domain classes. An interesting idea here which implies fetching one row of each of the domains. However, it doesn’t feel like a complete solution mainly because the test database against which I validate may not be so data rich as to have data in all tables.
Thanks in advance for taking time to read/reply.
That’s a nice approach and must work even for empty tables – if a table is empty, you have no legacy data to worry about validating, right? Or, if you want to test Grails constraints for compatibility with DB constraints, create a new instance of the class and try to
save()it in transaction – and always roll the transaction back.If the database is small, I’d even go and remove
max:1fromlist()– to validate every record, because only some of the records may violate constraints.I’d also replace
println "${it}"withassert it.validate().One last optimization, I’d limit the classes tested only to those that I know can violate some constraints. This will save a good part of such a test – and the test is going to take a plenty of time, you know – reading all the database with GORM overhead.