4 ms·
Just use stored procedures? Then you lose the ability to do unit testing without a database dependency, it's a lot easier to rollback code than to rollback code
by scarface74 9y ago
Just use stored procedures? Then you lose the ability to do unit testing without a database dependency, it's a lot easier to rollback code than to rollback code and stored procedures as one and you don't get full visibility on what the code is doing just by looking at the source code.
- ZenoArrow 9y ago> "Then you lose the ability to do unit testing without a database dependency" Not really, you just mock the database calls in the code you're unit testing.
- scarface74 9y agoIf all of your business logic is in the stored procedures, what are you actually testing? And I realize that being able to test queries without database dependencies, only really applies to a few languages that treat queries as a first class citizen in the language like C# and Linq where you can mock out your actual Linq provider - replace the EF context with in memory List<T> - and still test your Linq queries.
- ZenoArrow 9y ago> "If all of your business logic is in the stored procedures, what are you actually testing?" Depends on what you want to test. Can either write unit tests for the stored procedures or unit tests for the code that makes use of those stored procedures.
- scarface74 9y agoAnd then when you write "unit tests" for stored procedures with a lot of developers you get slow "unit tests" that don't scale across multiple developers because of Comte toon issues.
- ZenoArrow 9y ago> "because of Comte toon issues" Qué?
- scarface74 9y agoDamn auto correct. That should have been "contention issues".
- spdionis 9y agoI think GP meant that you can't/it's hard to test the stored procedures themselves. In this case if you mock the database calls you will not test the database logic.
- ZenoArrow 9y agoYou can unit test stored procedures. For example, with T-SQL can use the open source unit testing framework tSQLt: http://tsqlt.org/ http://tsqlt.org/ tSQLt is used by the commercial SQL Test product from Red Gate if you wanted a more polished user experience: https://www.red-gate.com/products/sql-development/sql-test/index https://www.red-gate.com/products/sql-development/sql-test/i...
- walshemj 9y agoNot in my experience though our DBA was very good (oh my first boss was Dijkstra he mentioned down the pub one lunch time) And would you not have your IDE on one monitor and your SQL IDE in another so you could look at both sets of code.
- scarface74 9y agoUsually with most modern automated deployments, you keep your build artifacts in a package (zip file, tar, etc.) and run a script to deploy it to your target system. Rolling back is a simple matter of installing the previous archive. That doesn't just apply to code anymore. You can treat "infrastructure as code" also. You can do A/B upgrades, rollbacks, etc. There is so much better tooling around regular code than sql/stored procedures. How many times have you seen stored procedures with hundreds of lines, duplicated stored procedures with V1,V2, etc appended to it, commented out logic etc? I've had to wade through some hairy code to but at least with a code, I can do some automated guaranteed safe refactoring, find dependencies, keep the interfaces backwards compatible, etc.
- walshemj 9y agoSounds like you have been working at companies with poor procedures and employees without the required skills. Avoiding SQL in favour of an ORM or inline sql is 95% of the time a sign of laziness.
- scarface74 9y agoYou did see me preach about all of the capabilities for unit testing without a database dependency, type safety, flexibility (with Linq I can switch back and forth between an RDMS and NoSql without any code changes)?
- hodgesrm 9y agoHow would you test data access in a meaningful way without a DBMS? It does not really matter whether you use tables or SPROCs. You'll still need a DBMS instance available. For PostgreSQL and MySQL you can bring up the DBMS in Docker. That is not a built-in fixture obviously but easy enough to do locally as well as in CI/CD systems like Travis. You'll need to load SQL into the DBMS as a prerequisite to testing. That has the benefit of testing your load/upgrade sequence.
- scarface74 9y agoHow would you test data access in a meaningful way without a DBMS? The beauty of Linq and Expression Trees. In the real world: Linq (c# code) -> compiler -> expression tree -> run time Linq provider -> destination language (sql, Mongo Query etc.) When you are unit testing you switch out the Linq provider for in memory Linq to objects provider. https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113).aspx https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113)....