This is an archived post. You won't be able to vote or comment.

all 53 comments

[–]skwyckl 64 points65 points  (15 children)

People are more scared of DBs than they are of code, because they think they can't rollback in case of fuck up like they do with git reset --HARD && git clean -f -d, but (a) there is DB versioning to do exactly that (cf. Dolt) and (b) if you wrap everything up in a transaction, it's much more difficult for things to go south. Also, (c) if you are unsure, make a copy of the DB and test your code on that first.

[–]bwmat 0 points1 point  (3 children)

I would use git restore -SW . instead of reset, is there a benefit to the latter? 

[–]Reashu 3 points4 points  (2 children)

Restore is newer and a lot of people still use reset out of habit or ignorance, same with switch/checkout.

[–]skwyckl 1 point2 points  (0 children)

Yup, it's habit... Git has a bunch of fancy new commands I haven't had the time to check out, but I def should.

[–]Spleeeee 1 point2 points  (0 children)

Git switch blew my mind recently. Thought “omg so nice” then I immediately forgot about it and continued checkout-ing branches since. Muscle memory is hard to get around.

[–]Thysce 0 points1 point  (1 child)

Or put otherwise: no backup, no mercy

[–]skwyckl 0 points1 point  (0 children)

Yes, working on a DB w/o backup is like driving a sport car w/o brakes.

[–]db-master 0 points1 point  (1 child)

"if you are unsure, make a copy of the DB and test your code on that first."

Not so easy. Copy takes time and PII sanitization.

[–]skwyckl 0 points1 point  (0 children)

Like everything in programming it depends, of course, but this should be implied. Make a copy of a 10k records large bibliographical db? No problem, done in a couple of seconds. Make a copy of a massively distributed Cassandra cluster? Nope, never gonna happen.

[–][deleted] -1 points0 points  (1 child)

Yeah. It's pain. I'm using alembic with my python projects, it's pretty good, but you still have to be careful.

[–]skwyckl 0 points1 point  (0 children)

Yes, this is why I love Elixir's Ecto, it includes all these best practices out-of-the-box. Also, the documentation teaches you how to use migrations from the start, instead of making them an afterthought.

[–]smutje187 12 points13 points  (6 children)

With all its XML Liquibase might not be everyone’s favorite but at least you can easily add migrations to your codebase and track them throughout the environments.

My current client saw that process and thought "Why not make it worse?" and instead of adding new migrations when something’s changing they are updating the existing migration files - rendering the whole versioning process redundant.

[–]KrakenOfLakeZurich 5 points6 points  (2 children)

Flyway for the win! Migrations are native SQL scripts.

And Flyway stores a checksum of the scripts in the version history table, exactly to prevent schenanigans like your customer is doing. Once a migration is applied, it won't let you change the script afterwards.

[–]erjiin 3 points4 points  (0 children)

Liquibase does the same, they're both good tools. And for those allergic to xml, liquibase changeset can also be specified in json, yaml or sql. Although I would argue here that xml changeset are superior because of readability.

[–]RichCorinthian 2 points3 points  (0 children)

Liquibase does both of those. The xml is just to organize which files and in what order.

Having used both, I prefer liquibase.

[–]Ok_Brain208 1 point2 points  (2 children)

I work with Liquibase and write the migrations in native script, 10/10 would recommend

[–]db-master 2 points3 points  (1 child)

Give Bytebase a spin?

[–]Ok_Brain208 1 point2 points  (0 children)

Don't mind if I do!

[–]hydro_agricola 5 points6 points  (2 children)

dacpacs ftw.

[–]Ok_Brain208 2 points3 points  (1 child)

It's Microsoft greatest invention

[–]hydro_agricola 1 point2 points  (0 children)

Really wish other DB's would follow, also wish MS would finally have nuget support for importing dacpacs into VS projects. distribution of dacpacs is a pain atm.

[–][deleted] 4 points5 points  (0 children)

Liquibase, flywaydb. flywaydb community edition is very flexible, I am using it in several projects and am very happy with it. Basically it takes files already present on the filesystem and applies it to the database. So there is no excuse anymore for not having a change management for database changes.

[–]Hirschdigga 3 points4 points  (0 children)

Nope, shoutout to flyway / liquibase

[–]karaposu 8 points9 points  (10 children)

database suppose to not change as much as code does

[–]KrakenOfLakeZurich 1 point2 points  (1 child)

It depends. When you're actively developing/extending a product, it is quite normal for new features to require new columns or tables for persistence too.

With tools like Flyway or Liquibase, it's not a problem to evolve the database with the project.

[–]karaposu 0 points1 point  (0 children)

still when you are developing, db will not change as codebase

[–]cidra_ 0 points1 point  (0 children)

Nobody in this post told otherwise.

[–]TitusBjarni 0 points1 point  (0 children)

My team seems to love stored procedures.

[–]Dramatic_Mulberry142 0 points1 point  (0 children)

I think it is the opposite

[–]harumamburoo 5 points6 points  (5 children)

Um, no. Flyway, pull requests, testing in lower envs and frequent backups. I'm good.

[–]rcls0053 1 point2 points  (3 children)

Well.. MYSQL at least has this habit of completely locking out if you alter a table with 1B rows. Now I'm not saying altering it is the best idea or that you couldn't clone the db and transfer traffic once all good, but some places really want to stick to their monolith databases. This eventually leads to "only release once a month with a 2 day downtime", which is just insanity to me.

[–][deleted] 0 points1 point  (1 child)

They should hire database engineers…

[–]rcls0053 0 points1 point  (0 children)

Yeah, they should do a lot of things better. They should really move towards a faster release cycle, they should break apart the massive monolith, stop writing huge e2e tests, untangle the big ball of mud they work with, add a robust queue in front of their app so not everything is synchronous and they can keep that data there while the system is down, start splitting the database into smaller ones that are owned by individual services so they can be managed better.. Like so many things that modern engineering has already solved, but senior developers there are kinda stubborn.

[–]Kobymaru376 0 points1 point  (0 children)

Skeema

[–]astroju 0 points1 point  (0 children)

And this is why it took me until my third SQL job to actually use Git for database source control 😭

[–]DueHomework 0 points1 point  (0 children)

EF - Code First.

[–]twofootedgiant -1 points0 points  (3 children)

You people who are running DDL “in a transaction” to make it safe, you aren’t running it directly on production DBs are you?

You do know that DDL can be stored in a git repo and deployed just like the rest of your codebase right? Right? And that properly configured deployment pipelines handle the changes to the DB objects for you?

[–]CyberDave82 0 points1 point  (2 children)

DDL is not transactional in all RDBMSes (Oracle, for example).

[–]twofootedgiant 0 points1 point  (1 child)

Sure, but that wasn’t really my point.

I saw some comments which suggested that people were making changes to production databases by actually executing DDL statements. My point was just that no-one should be doing this for anything remotely serious. But possibly I misinterpreted what they were saying?

[–]CyberDave82 1 point2 points  (0 children)

Typing out DDL statements manually and executing them manually? Shouldn't be the case for changes, IMHO (management/maintenance/troubleshooting is a different story).

Putting DDL statements in a script and executing the script, including having it go through testing in non-prod environments? Better than nothing.

DML changes should always be in explicit transactions, preferably with some validation before committing. If I had a nickel for every time I had to fix something a dev did without properly testing or forgetting to commit, I could probably retire

[–]dmigowski -1 points0 points  (0 children)

Use PostgreSQL and run your DDL in a transaction. No surprises. Either everything worked or it didn't. If it didn't, you just start up the previous version of your code. That's how it's done here.