postgres-migrations icon indicating copy to clipboard operation
postgres-migrations copied to clipboard

Allow JS migrations to be asynchronous

Open PaulKiddle opened this issue 6 years ago • 9 comments

Just added some async/await keywords and updated unit tests.

PaulKiddle avatar Jul 03 '18 15:07 PaulKiddle

Hi,

Thanks for this, and sorry I didn't get back to you earlier.

Could you give me a quick overview of your use case for this?

Thanks!

ThomWright avatar Nov 19 '18 10:11 ThomWright

I have a large csv of records that I want to import into the database. The file is read as an async stream to save memory and converted into SQL statements during the migration.

PaulKiddle avatar Nov 19 '18 11:11 PaulKiddle

How large are we talking?

Does it get run as one migration? If so, streaming to save memory might not help if it gets turned into one big SQL string anyway.

Could you run this CSV > SQL transformation once, offline, and save the .sql file?

The reason I ask is I'm hesitant to add more features, and I worry about what adding async support would encourage. I'd like to keep this as simple as possible.

ThomWright avatar Nov 19 '18 17:11 ThomWright

That's certainly a possibility! Though in that case I wonder why you have this feature at all, as surely all use cases could be solved this way? It strikes me that if you're going to allow the use of a js migration, you might as well allow it to be asynchronous, especially considering how small this change is, otherwise this particular feature just feels half-finished to me.

PaulKiddle avatar Nov 21 '18 16:11 PaulKiddle

That's certainly a possibility! Though in that case I wonder why you have this feature at all, as surely all use cases could be solved this way?

Yes, I do slightly regret allowing JS migrations! I wouldn't encourage using it, and I think most use cases are better served with SQL files.

Every time the migrations are run, the generated SQL from previous migrations will be hashed and compared with the hashes in the database. If these don't match, then the migration will fail.

This means:

  • the conversion from CSV to SQL will be run every time
  • the CSV file should be treated as immutable
  • the code that does the conversion must be repeatable, care needs to be taken with e.g. dependency changes

That said, if you're still sure you want async migrations, I'll merge this in.

I need to do a major version bump some time soon because of some recent hashing changes (only affecting JS migrations), so let me know and I'll get this included with that release.

ThomWright avatar Nov 22 '18 11:11 ThomWright

Personally I don't think I'll need this feature again -- though if you decide not to accept this PR it might be worth deprecating the JS migrations with a warning and adding a note to the docs.

PaulKiddle avatar Nov 22 '18 17:11 PaulKiddle

it might be worth deprecating the JS migrations

This feature is a major selling point for me, personally. Some things, like, for example, database-level auditing, can be extremely repetitive, and there are gains in having a generator with parameters, rather than doing the copy-pasting by hand.

On the other hand, though, can be done in SQL too, just clumsier.

alecmev avatar Nov 27 '18 12:11 alecmev

Is there a chance for this change or something similar to this to get merged as well? Postgrator does something very similar allowing to call HTTP request during migrations for example

bamdadd avatar Apr 09 '21 13:04 bamdadd

Is there a chance for this change or something similar to this to get merged as well? Postgrator does something very similar allowing to call HTTP request during migrations for example

The idea of that fills me with dread :sweat_smile:

Could you explain your use case for this feature?

ThomWright avatar Apr 09 '21 17:04 ThomWright