4 ms·
> It is possible to keep database functions in git. It requires some discipline but it's not hard. My experience differs. How do you check that the stored proc
by idoubtit 4y ago
> It is possible to keep database functions in git. It requires some discipline but it's not hard.
My experience differs. How do you check that the stored procedures in the DB are exactly as defined in Git?
I remember spending several hours on a bug that was caused by an altered stored procedure. One SQL function was not as it should have been. I don't know if someone had patched it live, or if there had been an unnoticed failure in a DB migration script.
With a compiled executable, consistency is guaranteed. With interpreted code (Python, PHP), consistency check is done with a command like `git status`. But there is no standard way to compare the state of store procedures in the DB with their expected code in versioned files.
- xupybd 4y agoThe solution is that nothing gets to production unless it gets migrated in via the CI pipeline. Admittedly it's still a problem during development. But a tool like Flyway versions your DB. You know if you are out of date as the migration table is behind. It's not as good as compiled code but it works when you need it.
- yobbo 4y ago> How do you check that the stored procedures in the DB are exactly as defined in Git? How do you know that a binary is produced by some code in git? Because you built it. Same principle applies. But also, the db can list its code.
- Smeevy 4y ago> I remember spending several hours on a bug that was caused by an altered stored procedure. One SQL function was not as it should have been. I don't know if someone had patched it live, or if there had been an unnoticed failure in a DB migration script. If you have everything under version control you can just reapply that version's stored procedures off the database scripts. If someone or something is not handling failures in deployment scripts (not just database migrations) then you get that sort of problem.
- cafard 4y agoI have done this with both T-SQL and PL/SQL. The stored code is all available in system views and easily extracted with a script. If you set up the script to run automatically at a given time, you know that the version in git is at most n hours out of date. If you leave the control up to the scheduled job, then your commit messages may not be what you want. But there's nothing keeping you from running your extraction script off schedule and then writing a good commit message.
- gifflar 4y agoWe store our database functions in SQL scripts that are automatically executed with every Flyway migration, so-called callback scripts. In the callback script every function is declared to be created or recreated. As Flyway is executed with each application start, the database functions are recreated with every application start, even if they didn’t change. This approach has its pros and cons but it ensures that database functions always match the version of the application code. Because the callback script of a database function is stored in a file that is edited when needed, the Git history works the same as for application code.