Fork me on GitHub
#sql
<
2020-03-27
>
jaime09:03:07

Hi, what is your dev workflow when working with database migrations? 1. What tool do you use? 2. Are you using repl to generate migration scripts, as well as when running them? 3. Do you seed data? How? 4. How do you do it in production?

💯 4
âž• 4
jaime12:03:34

thanks for sharing. I like the idea of using the flyway api instead of command due to file size IMO.

jmayaalv10:03:10

@jaime.sangcap we have beein using migratus for a quiet a while. https://github.com/yogthos/migratus no complains at all We normally run migrations when the app starts in prod. however not when started via the repl.

jmayaalv10:03:33

seed data normally is also part of the migrations for us.

jaime12:03:45

cool. I'm also looking into migratus. do you have the seed data in dev folder?

jaime12:03:43

I'm thinking of creating migration.clj file and expose -main function for cli, since I'm using deps.edn. I Need to read parse-opts though

jmayaalv18:03:26

we have seed data together with migrations, so on resoruces/migrations

jaime06:03:20

got it :thumbsup:

danielglauser22:03:28

Trying to insert a record with org.clojure/java.jdbc into Postgres and it keeps complaining that I’m not specifying an id.

Execution error (PSQLException) at org.postgresql.core.v3.SimpleParameterList/checkAllParametersSet (SimpleParameterList.java:257).
No value specified for parameter 1.
Tried:
(jdbc/execute! db ["INSERT INTO CLASS_SONGS (class_id,track_number,song_id,start_time) VALUES (?, ?, ?, ?)"]
                 {:class_id (:id class)                                         
                  :track_number track                                           
                  :song_id (:id song)                                           
                  :start_time start-time})
And:
(jdbc/insert! db :class_songs {:class_id (:id class)                                         
                  :track_number track                                           
                  :song_id (:id song)                                           
                  :start_time start-time})
The table looks like:
Column    |  Type   | Collation | Nullable |                 Default
--------------+---------+-----------+----------+-----------------------------------------
 id           | integer |           | not null | nextval('class_songs_id_seq'::regclass)
 class_id     | integer |           | not null |
 track_number | integer |           |          |
 song_id      | integer |           | not null |
 start_time   | integer |           |          |
I want Postgres to just trigger the nextval function when I don’t specify and id. This works in other languages. Any ideas?

hiredman22:03:36

do you have the error from the insert! call? the error you posted looks like it is from the execute! call, and that error is because you are not passing in the values for the parameterized query correctly

hiredman22:03:43

for execute! like that you can't pass in a map like that (it is treating that map as the options map) you need to pass in each value matching a '?' in the vector, similar to with query

danielglauser22:03:49

Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2477).
ERROR: null value in column "id" violates not-null constraint
  Detail: Failing row contains (null, 8487, 1, null, 0).

hiredman22:03:35

are you sure your insert! is as shown?

hiredman22:03:42

you have 5 columns and five values in the row there, but insert! doesn't actually know how many a columns a table it is inserting to has (it doesn't do any reading of the table metadata, etc) so it wouldn't know to insert 5 values unless you gave it five

danielglauser22:03:43

Yes, just double checked.

danielglauser22:03:06

Interesting. In Go folks on the team aren’t specifying anything for id and Postgres happily runs the nextval function.

hiredman22:03:21

right, and I think somehow you are specifying an id

hiredman22:03:40

is it a literal map that is being inserted?

danielglauser22:03:45

It’s the song that’s missing the id.

danielglauser22:03:32

So the error is weird, it should complain about song_id being null.

danielglauser22:03:49

Once I generated the map literal it was obvious.

danielglauser22:03:03

Thanks for the help Kevin! 🙂

hiredman22:03:49

do you have fk constraints or triggers or something, it could be reporting the error based on the song table (where presumably the column is named id)

danielglauser22:03:33

Foreign-key constraints:
    "class_songs_class_id_fkey" FOREIGN KEY (class_id) REFERENCES classes(id)
    "class_songs_song_id_fkey" FOREIGN KEY (song_id) REFERENCES songs(id)

danielglauser22:03:36

Yup, that’s it.