You can get type-safe raw sql if you're using jetbrains IDEs with autocomplete and suggestions because all the ides are well-integrated with databases & its own dbms. They can compared your created tables in the db with your raw sql and highlight any errors in the raw sql.
I wrote my own package for migrations that’s in early development- it’s open source and on my GH. As for the tests, I linked a video in the description which showcases how I was approaching tests, I’ve since changed it a bit, so I’ll make an updated one soon 😊
No databases are optimized for tx and usage of tx is critical since transactions enable data integrity and consistency during insert, update and delete. For instance if you inserting for two related tables and you get an error on the second table, data will still be created for the first table without any related data of the second table resulting in useless inconsistent data. Also If im not mistaken, some if not all databases run sql statements in transactions by default and i think you can rollback in sql server without even starting a transaction(BEGIN TX) as long as you didnt commit the tx. Transactions are not necessary for a reading data (SELECT). Many professionals in big tech and startups have deleted their data costing them millions when it could've been easily prevented by a simple "BEGIN TRANS".
@@lehoangngo8201 weird I don't know, he shouldn't have, it's pointless. A select statement is read-only, and on its own, it 100% doesn't need a transaction. Transactions should only be used with a select if you're also using statements such as insert, update & delete to make changes to the database.
@@ArcticPrimal thanks for clarify, I find myself very struggle when working with sqlx despite people say that it was easy. Have you ever considered sqlx + sqlc, surfed reddit and found that combination but don't know does it worth a try
Yeah. You can use sqlc, then sprinkle your own custom methods in. It's a bit of pain to get the generated files tagged properly, but it's just a bunch of stuff in their config. (the suffix option is broken and does not apply to models.go etc)
For sure! It’s an option, and I’ve used it on projects before. This video is more aimed at those managing legacy or just projects using std lib related code and a nice pattern to manage that 😊
@@dandogamer You can. SQLC only provides some boilerplate for you. You are free to query the database as you desire outside of that, or augment the boilerplate with your own functions.
In my opinion orm seems simple and fun at first when its basic queries however as your project grows and the queries get a lil bit complex plus with associations/relations, its no longer simple to do these complex queries in orm compared to raw sql. ORM can be too abstract
On top of the points the other commenters made, SQL is really easy, and writing SQL queries yourself is just so much simpler and better, and using something like SQLC makes it even better.
Performance is also a concern here, GORM sucks on this aspect. If performance is not important to you, then there are better languages with frameworks that will do everything to you.
For the integration tests what I'm doing is to spin up a new test schema that is a clone of the public schema and execute my migration files. Then while testing I temporarily set the search_path of the database to the test schema. Finally, if the test passed I remove the test schema, this has the added benefit that if a test fails I can keep the test schema and inspect it manually to see on which table structure or data the test failed on.
You can get type-safe raw sql if you're using jetbrains IDEs with autocomplete and suggestions because all the ides are well-integrated with databases & its own dbms. They can compared your created tables in the db with your raw sql and highlight any errors in the raw sql.
Gotta love Jetbrains IDEs
is there something similar in vscode?
Your teaching style is good keep up the good work.
Nice vid. I need to know your color theme tho, looks so fitting for golang (might sound strange, but i really think that)
Hi, thank you! The colour scheme is 'poimandres' :)
@@samvcodes I really need a neovim setup like yours. How can I achieve that?
Using testcontainers (or similar tool) is a game changer. Please don't make integration tests with in-memory dbs if you are not using one
Why would we be using txn if we follow this pattern ?
How do you setup the database in the tests? and how do you handle migrations?
I wrote my own package for migrations that’s in early development- it’s open source and on my GH. As for the tests, I linked a video in the description which showcases how I was approaching tests, I’ve since changed it a bit, so I’ll make an updated one soon 😊
Awesome video but volume of sound is low. I had to put volume to max on my laptop to hear it clearly.
Noted! I’ll fix that for the next video, I have a new microphone so I’m still trying to get it right. Thanks for letting me know! 😊
Thanks for the amazing explanation. I'm currently learning Go. One question though. Isn't using a transaction for everything a bit expensive?
No databases are optimized for tx and usage of tx is critical since transactions enable data integrity and consistency during insert, update and delete. For instance if you inserting for two related tables and you get an error on the second table, data will still be created for the first table without any related data of the second table resulting in useless inconsistent data. Also If im not mistaken, some if not all databases run sql statements in transactions by default and i think you can rollback in sql server without even starting a transaction(BEGIN TX) as long as you didnt commit the tx. Transactions are not necessary for a reading data (SELECT). Many professionals in big tech and startups have deleted their data costing them millions when it could've been easily prevented by a simple "BEGIN TRANS".
@@ArcticPrimal why a select statement in 1:24 use transaction
@@lehoangngo8201 weird I don't know, he shouldn't have, it's pointless. A select statement is read-only, and on its own, it 100% doesn't need a transaction. Transactions should only be used with a select if you're also using statements such as insert, update & delete to make changes to the database.
@@ArcticPrimal thanks for clarify, I find myself very struggle when working with sqlx despite people say that it was easy. Have you ever considered sqlx + sqlc, surfed reddit and found that combination but don't know does it worth a try
Why not use sqlc?
Yeah. You can use sqlc, then sprinkle your own custom methods in. It's a bit of pain to get the generated files tagged properly, but it's just a bunch of stuff in their config. (the suffix option is broken and does not apply to models.go etc)
For sure! It’s an option, and I’ve used it on projects before. This video is more aimed at those managing legacy or just projects using std lib related code and a nice pattern to manage that 😊
You cant do dynamic queries with SQLC
@@dandogamer You can. SQLC only provides some boilerplate for you. You are free to query the database as you desire outside of that, or augment the boilerplate with your own functions.
Nah, if for some reason your schema change you'll have to redo it all again.@@advertslaxxor
Why do you prefer using raw SQL as opposed to using an ORM like GORM? People have various opinions about this, just want to hear you on this one…
In my opinion orm seems simple and fun at first when its basic queries however as your project grows and the queries get a lil bit complex plus with associations/relations, its no longer simple to do these complex queries in orm compared to raw sql. ORM can be too abstract
I completeley agree, used several ORMs in production and they all scale horribly. @@ArcticPrimal
On top of the points the other commenters made, SQL is really easy, and writing SQL queries yourself is just so much simpler and better, and using something like SQLC makes it even better.
Performance is also a concern here, GORM sucks on this aspect. If performance is not important to you, then there are better languages with frameworks that will do everything to you.
For the integration tests what I'm doing is to spin up a new test schema that is a clone of the public schema and execute my migration files.
Then while testing I temporarily set the search_path of the database to the test schema.
Finally, if the test passed I remove the test schema, this has the added benefit that if a test fails I can keep the test schema and inspect it manually to see on which table structure or data the test failed on.
👏👍👏