Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert / pre-populate migrations schema to reflect migrations manually applied before Postgrator first use #91

Open
joelbernstein opened this issue Sep 19, 2019 · 5 comments

Comments

@joelbernstein
Copy link

Assume a real-world scenario where the production database has had migrations applied by hand.

Does Postgrator expose enough interface to be able to use it to enter ros in the migration schema as though they had been inserted by running the migrations? That is, assuming I certify that the migration has been manually applied, how can I tell postgrator that this is the case, and have it recorded with md5sums in the appropriate db table?

I would be willing to make the necessary modifications, if this isn't already possible, if someone could give me some pointers.

@joelbernstein
Copy link
Author

joelbernstein commented Sep 19, 2019

Does this make sense? I mean:

Assuming I have:
migrations/000.do.schema.sql
migrations/001.do.migrate-foo.sql
migrations/002.do.migrate-something-else.sql

And I have applied 000 and 001 to an existing database, without using Postgrator. Only 002 is pending. But Postgrator can't tell this because the migrations schema is empty. What can I do to avoid this?

How would I tell Postgrator that the first have already been applied, so that it will not attempt to run them again? I presume I need to insert some rows into the migration table. I think this should probably be a Postgrator feature. Can you advise how to implement it please?

@rickbergfalk
Copy link
Owner

In this case you'd need to populate the necessary rows in the migrations table. Postgrator itself doesn't expose an interface for these manual updates at all.

Is this just a one-off case or are you frequently applying migrations manually?

In my experience, applying migrations out-of-band like that kind of limits the benefit of using a migration framework/tool like Postgrator. By only updating a database via migration tooling you are guaranteed to have a known schema and increase confidence in future changes.

I know that's a pretty strict stance to take and not all organizations can make that commitment.

I've worked in both kinds of orgs though, and I just want to share that it is so much better when a database schema is consistently reproducible in any environment you need it to be without manual intervention. :)

@joelbernstein
Copy link
Author

joelbernstein commented Sep 19, 2019 via email

@rickbergfalk
Copy link
Owner

rickbergfalk commented Sep 19, 2019

Ah - that does.

The function to log a migration is persistActionSql on the common client. When running migrations that is called here https://github.com/rickbergfalk/postgrator/blob/master/postgrator.js#L206

What migrations exactly should be logged as already migrated will be up to you to provide I'd imagine. If I had some databases already created manually I'd likely manually insert the rows necessary as well.

Depending on how you plan on running migrations you might be interested in https://www.npmjs.com/package/postgrator-cli

@joelbernstein
Copy link
Author

joelbernstein commented Sep 19, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants