node.jspostgresqlmigrationnode-pg-migrate

How do I insert records in a node-pg-migrate migration?


I'm trying to use node-pg-migrate to handle migrations for an ExpressJS app. I can translate most of the SQL dump into pgm.func() type calls, but I can't see any method for handling actual INSERT statements for initial data in my solution's lookup tables.


Solution

  • It is possible using the pgm.sql catch all:

    pgm.sql(`INSERT INTO users (username, password, created, forname, surname, department, reviewer, approver, active) VALUES
    ('rd@example.com', 'salty', '2019-12-31 11:00:00', 'Richard', 'Dyce', 'MDM', 'No', 'No', 'Yes');`)
    

    Note the use of backtick (`) to allow breaking the SQL statement across multiple lines.