sql

All things SQL and JDBC...
NoahTheDuke 2021-06-08T14:21:07.132400Z

are there any fixture/test data generation libraries like factory_bot (https://github.com/thoughtbot/factory_bot/)? (maybe I should ask this in #testing, but the main domain would be interaction with sql)

lukasz 2021-06-08T15:35:15.133500Z

since you're not dealing with an ORM, factory bot approach is an overkill - you can use something like faker: https://github.com/paraseba/faker and generate data sets for inserting via jdbc

seancorfield 2021-06-08T15:36:20.134500Z

We often write Specs for our tables and use those to generate random, conforming data for tests.

seancorfield 2021-06-08T15:37:54.136200Z

The nice thing about that approach is that the Specs serve double-duty: you can use them to validate data before inserting into the DB and you can also use them to drive generative testing, either through generative data production or directly to test exercise code that operates on what could come out of the database.

NoahTheDuke 2021-06-08T16:00:14.140400Z

well, the benefit of factory_bot isn’t so much that it interfaces with the ORM (tho that’s helpful), but that you can define the structure of the data and generate “random” versions while also over-riding specific values. i am particularly fond of the “trait” system that lets you bundle a set of values to a name, so for instance, if an admin user needs to have their “role” set to “admin” and they need to be added to the “admin” table, in the :admin trait you can do that and then just call fb.create(:user, :admin) and it’ll set the role and insert into the admin table

NoahTheDuke 2021-06-08T16:02:45.142400Z

Spec and faker work when you only want to get a random conforming object, but it gets harder/more annoying when you want to construct more elaborate set-up data.

NoahTheDuke 2021-06-08T16:06:22.143700Z

one method would be to write a wrapper function that does that kind of conversion/set up for you, but at that point you’re re-implementing the factory_bot-style logic without the reusability of a factory_bot-style library

practicalli-john 2021-06-08T16:34:05.149200Z

SQL migrations script question for postgresql I'm writing an SQL migration script to update the names of 10 different people in a table. I know the unique id's of each person and their new name. Is there a better approach than just using a migration script with 10 separate update statements ? The project uses ragtime for the subscriptions and .up.sql scripts in resources/migrations. The project doesn't merit anything fancy as it will be replace at some point.

dpsutton 2021-06-08T16:38:05.150Z

a CTE combining id, old name and new name, then update set new name where id = id and name = old_name

practicalli-john 2021-06-08T18:25:49.150700Z

Sounds intriguing, I'll take a look. Thanks

indy 2021-06-08T20:16:26.153200Z

That is not a schema migration. Are the same names in prod, (staging?) and local dbs? If not, a one time update should suffice?

indy 2021-06-08T20:39:13.153400Z

update table t
set name = old.name
from (select id, name from (values (1, 'new-name-1'), (2, 'new-name-2')) as old(id, name)) old
where old.id = t.id;