r/SQL • u/Oh_Another_Thing • Jun 17 '26
Discussion Best way to avoid environment mixups?
There are a ton of horror stories about people running the updates in the wrong environment, is there a best practice for avoiding this? Can you add a check of an environment variable or the server you should be on against a hard coded text of the environment you should be in?
5
Upvotes
4
u/SootSpriteHut Jun 17 '26
For me on AWS everything is on the same instance and what I used to think of databases are schemas. For this reason I always am fully explicit about the table in from and joins (SCHEMA_NAME.TABLE_NAME)
or you can start every script with USE.
if you're in SQL server IIRC you can still put the database name in front of the schema
3
u/venkat_deepsql Jun 18 '26
What actually saved me after I once ran an update against prod thinking it was UAT: every write script now starts with a guard that aborts if it's on the wrong box. In Postgres that's a DO block checking current_database() that RAISE EXCEPTIONs on a mismatch; in SQL Server, IF @@SERVERNAME <> N'EXPECTED' RAISERROR + RETURN. Costs nothing and it doesn't depend on me noticing the tab color when I'm fried at 6pm.
Bigger lever though is creds. Your everyday prod login should be read-only, and writes go through a separate role you consciously switch into. That makes 'wrong window' physically harmless for DML instead of just discouraged. Color-coded tabs and wrapping things in BEGIN ... check rowcount ... COMMIT are good habits on top, but they're still you-being-careful, and being-careful is exactly the thing that fails at the wrong moment.
1
u/Eleventhousand Jun 17 '26
Well, environments aren't always delineated the same way. For some on-prem solutions, folks may use separate servers. Whether on-prem or in cloud, some may use separate databases or schemas. During the times that I used SQL Server on-prem, I used perform these types of updates in a transaction with the following framework:
3
u/russbii Jun 17 '26
I use SQL Prompt or another plug-in to color tabs for me. So I always know when I’m in prod.
I think registered servers might do it now too.
2
u/dafugiswrongwithyou Jun 17 '26
There are probably quite elegant ways to deal with this depending on your specific setup, but a simple kludgy one is: have a table on an environment that doesn't exist on the others, just containing a single record with a single field, and have your scripts check for that table in some useless-but-technically-valid way. Scripts intended for that environment will work fine. Run them in another, they'll complain about the missing table and fail.
2
u/Achsin Jun 17 '26
As much as possible I try to only work on one thing at a time, especially if I’m doing things that are potentially destructive. Everything gets closed out and disconnected from when I’m done.
1
u/drinkmoredrano Jun 17 '26
Its a rite of passage and knowing how to unfuck something you fucked up is what separates the men from the boys.
1
u/nl_dhh Jun 17 '26
In SQL Server Management Studio, you can have tab colours for different connections. Besides that, I try to make it a habit to log off from Prod when I don't need it: look up stuff in dev or test if possible.
Also, I use Visual Studio and push changes from dev -> test -> prod via Azure DevOps rather than developing directly on the databases.
This, along with coffee, has worked out okay for me so far. Oh... and backups.
1
u/rire0001 Jun 17 '26
We used to build environment-agnostic deployment bundles, and manage the implementations with a home grown but highly effective staging script. It simply wouldn't let you run on FIT if the SAT deployment hadn't completed. Also, passwords for each env were different.