I have a query with respect to both SST and SEED, ...
# seed
a
I have a query with respect to both SST and SEED, how should I go about running CLI commands pre and post deploy that use env vars from my codebase. Also, the post deploy commands need to be run based on the state of the deployment. I need this to basically run mongodb migrations pre deployment and rollback if the deployment fails and do nothing if the deployment succeeds.
f
hmm.. post deploy doesn’t get triggered if the deploy fails.
a
so how would I go about rolling back my migration if a deployment fails?
f
I can see there’s value in having a way to run something on deploy failure to clean stuff up.
But I think migration rollbacks isn’t a such use case.
Usually you should separate data migration from deployment, since the migration and deployment aren’t done atomicly.
So either: 1. you migrate the data first, and ensure existing code works with the new data, and then deploy; or 2. you deploy first. After the deploy succeeds, you then migrate the data.
I haven’t come across a use case where the two are tightly coupled.
a
I agree but I really don’t want to have a manual deploy step because missing it is too easy. Usually all monolith deployments have a script that deals with migrations.
f
Right for sure.
Here’s a recently discussion on running migrations https://serverless-stack.slack.com/archives/C01JG3B20RY/p1624329822495000
For now, something you can try is running the data migration inside a custom resource.
a
Yep, I’m in that thread.
Oh! Interesting, any links to some docs or if you’ve a guide on how to use this? The prime features I need are to handle deployment failures and have access to env vars. If it’s a cdk construct then env vars shouldn’t be a problem but how would I go about dealing with deployment failures?
f
So when your stack gets updated, the custom resource is going to receive an
Update
event.
And CF deployment failed, the custom resource is going to get called again. And based on the input, you can implement the rollback logic.
a
okay, thank you. I will go through this but I believe this a very common scenario and should not need effort and r&d to achieve. If you could maybe think of including this as a high-level construct within sst itself.
f
The example doesn’t have rollback. Again, I haven’t come across that many ppl that does rollback to be honest.
a
Yeah, I haven’t had to use rollback myself ever but knowing it is possible helps me sleep better. 😅
f
Totally agreed! I actually just opened an issue for supporting DB migrations https://github.com/serverless-stack/serverless-stack/issues/483
a
Wow! That’s great, thank you, I appreciate it. 🙏
f
Just updated it with a proposal on what it might look like :)
a
Ah! interesting indeed. 🙌