Avoid a Database MELTDOWN: How to use Flyway with Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to teach you how to use FlyAway with spring boot because let's face it if you're using a database in production and you're not managing the database schema time is ticking because it's going to go horribly wrong that is unless you choose to use something like database migrations so let me show you how to use Flyway so when your application's domain changes maybe you have a new variable one of your classes the database needs to be updated as well so you'll need to put a new column on that table of course you can manage this manually by logging into your database and running your SQL scripts but that's pretty error prone to make life easier for you spring boot does offer the schema.sql and the data.sql files that you can put in your Source main resources to create your schema in populated with data but this approach does have its limitations a much better way is to use nice testable database migrations and of course we're going to use FlyAway for that so let me show you how to do it so first things first you're going to want to go to start.spring.io and this is going to generate for us a spring boot project so let's take a look we don't want to use grade on groovy for this one we're going to be using Maven and we're going to be using Java we're going to select the general release version 3.1.3 at the time recording the group will call that com Dev tiro and the artifacts we'll call this one Flyway demo project for spring Boot and Flyway great we're going to select the packaging of a jar file and we're going to use Java 17 now for dependencies dependencies we're going to need for this one is spring day to jpa we're going to be using a real postgres SQL database for this one so let's get the postgres SQL driver of course we're going to need Flyway so let's not forget that and finally I'm just going to add lombok to the mix because we're going to be creating some data classes and that will make things simpler for us so let's generate this and open up in our IDE so here we are in IntelliJ but before we start we are going to need a database and we're going to use postgres for this one and we're going to start it up using Docker so I'm going to create for us here a file which is called Docker hyphen compose Dot yaml and I'll now populate this with what we need to run a postgresql database this is about as simple as it gets so this Docker compose file is going to create for us a container a postgres container running on the usual ports and the postgres password is going to be change Marion prod with an exclamation mark and we're going to connect to this using our application so first things first let's start this up open up a terminal I'm going to type Docker hyphen compose and up you can run this with the Dash D flag if you want to do it in the background but we're just going to have it here so we can see it the database system is ready to accept connections now the next thing we're going to do is connect to this in a way that I can show you it visually so we're going to use dbiver for that so here we are in D Beaver we're going to go up to the top left here we're going to select for ourselves the postgres SQL database it's running on localhost and we'll type in the password Here of Change Me In prod exclamation mark let's test the connection connected looking good let's click on finish so we now have a connection to our database let's just make sure we have what we expect Empty Tables looking good so now we can actually start on our application so here we are back in IntelliJ and we're going to do the absolute bare minimum here to connect to the database and see our application fail because none of the tables exist and at that point we'll use FlyAway to create them so we're going to need some domain objects for this one so let's go Source main Java and we'll create for ourselves here a package and we'll call this one domain now if you've watched any of my content you know I love books so we are going to create for ourselves a domain object which is simply going to Model A book so we'll create a book class in domain and we're going to use the long back annotations at data uh All Eyes Constructor at noaz Constructor and build out why not and if you want any more information on what these annotations actually mean in more detail we do have a full Lombard course so do check that one out but the tldr is that this is going to create all of the Getters Setters equals and hash code all of the boilerplate stuff that we want in addition to creating the constructors for all of the arguments none of the arguments and hey if we're going to use a builder pattern a little bit later on we have the option for that as well so what does a book do um well a book is going to have an ISBN which is a string and its unique identifier and it's going to have a title which uh it's just going to be a string so this gives us a plain old Java object or a pojo we're now going to turn this into a spring data jpa entity and the way we're going to do that is with the at entity annotation and why not we're going to put the at table annotation on there as well and specify the name of the table that we want this to represent which we're going to call books Let's import that and every entity must have a unique identifier so we're going to put at ID on the ISBN there and that's to confined to me so this is going to be a very simple table that we would want in our database with the ISBN and the title for the time being both of them going to be represented by let's call it text but just this class existing by itself isn't going to be enough so I'm going to create for ourselves and hear a package called repositories foreign and we're going to create a new interface in here and we're going to call this one Book Repository now this is going to allow us to interact with the database for more information on repositories we have a full spring boot course that you can check out we'll put a link in the description below now this is going to extend the crud repository and then we need to say the type this deals with in this case going to be book and it's going to be string as the second argument in the generics and that's going to be the type of the ID which is ISBN that's a string okay so I'm going to have a crud repository so that enables us to have create read update and delete functionality we're also going to put the app repository annotation on the top here to mark this as a repository and to allow us to dependency inject that where it needs to go so let's go over to our application here spring bit application so we're going to turn this into a command line application so we're going to say implements command line Runner and then we're going to implement the run here now we're going to need access to our book repositories we're going to say private final Book Repository let's call that Book Repository seems like a good enough name to me and this will need to be injected and we're going to Constructor injection for this so if we just specify a Constructor on this class that'll be enough to inject our Book Repository into it when our application starts up now we're simply going to say Book Repository and we're going to say find all to get all of the books in the database it's not really going to matter that there's no books in the database for the time being but because of the way that spring Auto configures this is going to put everything in place to allow me to show you the application starting up connecting to a database and then ultimately failing so we're going to find all not find all by ID and we're simply going to go for each taking the book and now we're just going to go system.out.printlearn and we're going to say book dot to string just like that and that will just print out the book for us so now that we have the bare minimum of an application which has some spring data jpa entities in there and is actually using them in some way in this case just printing out all of the books in the database we have enough that spring is going to go ahead and do everything that it needs to do to allow me to show you fly away so next step is to actually get this connected to our database and we're going to do that by going into our resources over here and open up application.properties now let me populate this with all of the details that need to know in order to connect to postgres so here we go quick run through so this is the URL that we can find our database on localhost on the usual ports forward slash postgres our username for this is going to be postgres and we already know that our password is changed me in prod because that was in our Docker compose file from earlier we're going to be using the postgres driver to connect to a postgres database no surprise there so we're the only thing that's really worth mentioning is this last one the implementation of spring dates jpa that spring boot uses has this concept of Auto ddl so it's able to create the database schema for you based on the entities that it finds now of course we don't want that to take place we want to use Flyway instead because with FlyAway we can manage the database migrations over time but with auto ddl it runs a risk of doing things like deleting the entire database or updating when it's not supposed to now this is only if it's misconfigured mind you but obviously if we can reduce risk we reduce risk so with the value of none this is completely disabled we're only going to be using Flyway here so I'm now going to go ahead and start my application bearing in mind we expect this to connect to the database But ultimately fail because none of the tables exist let's give it a go okay we have failed application so let's just check it's failing for the reasons that we expect it to and not just because I misconfigured something so if we take a look up here right to the top of the stack Trace we will see that we failed to execute the command line Runner and it tried to execute some SQL but hey books does not exist of course it doesn't exist it's a completely blank database and we can double check that by going over to dbiver and we'll just refresh this so we do have one table but we don't have the books table so more on what the FlyAway scheme history means in a moment so let's get back to our application so now here comes the good bit we can now use FlyAway to create the schema in our database so if we take a look at our source my resources here we can see we've already got this directory called db. migration so the spring initializer was kind enough to create this for us and this is exactly where we're going to put our FlyAway database migrations so we'll go in here and we're going to create ourselves a new file now Fly Away has a very particular file naming convention but it is pretty straightforward so it goes like this we have the version which is usually V in this case it's the first one in the list so we're going to say V1 and now you need to put a double underscore and I'll say again it's a double underscore and the reason being that if it's a single underscore you don't have yourselves a FlyAway database migration it's not going to be picked up and it's not going to be run and of course we want it to be run so we're going to go double underscore and now we can put a description so for this one I'm just going to say it's initializing the schema so initializing the schema we're going to use underscores as delimiters here and it's a SQL file so dot SQL and behold our first FlyAway database migration a anti-sql file so what are we going to do in here well let's populate this with all of the SQL that it needs in order to create this bookstable for us so here we are it's creating a table it's called books it's going to have an ISBN column which is text and that can't be null because that is the primary key and we have another column which is title again that's text so bearing in mind that D Beaver is showing that we have no tables let's see if it creates this for us so we're going to start our application up by clicking this play button here okay so it looks like it started up and it's exited with exocode zero so it was a success so let's go over to D Beaver see if this actually was a success and see if it's crate for us our books table so here we are let's click on F5 for refreshing and there we go we have ourselves a books table so there's our ISBN and there's our title get a little diagram there we go so that's about the most basic example of how to use Flyway now it's time we talk about that fly away schema history tables let's take a look so if we look in the FlyAway schema history table here we can see that it has a single entry and this single entry has the installed rank 1 the version of one and its description is initializing schema hello we recognized that from earlier and we've got things like the script in here a checksum and uh install buy installed on so on so forth so this FlyAway schema history is a table in your database that fly away uses in order to keep track of database migrations and if one of the database migrations was to to change for example if you were to change a database migration one when you had two in play then uh yeah flower would pick up on that and it would fail it would tell you about it so in order to demonstrate that we're going to need a second database migration so let me show you how to do that so you can see here we've got V1 well naturally what comes after V1 we're going to have V2 so we're going to say V two and we're going to say add a column to books let's call it category so add category to books dot SQL great and I'll just put the SQL in here to add that column and there we go so we're going to alter the table books and we're going to add the category column which is just going to be text like a category A Book Like fiction non-fiction maybe book stuff categories so that is all well and good now let's rename this but a double underscore not a single underscore in that and now if we run our application we're expecting for the books table to have the category column on there and uh also for that table to have another entry in there for v2 let's see if it works okay we've got xcode zero so that was a success apparently let's double check it okay so here we are let's refresh and let's refresh over here okay looking good so first things first let's check books and there's our category column looking good and let's check that fly away scheme history and we can see here we've got the installed rank we've got number two now at category to book there's the script there's a check so I'm installed by installed on looking good and really that's just how Flyway works you would just add another database migration to change the database to be how you want it to be you can run this on your local test it all out get it working and then you can deploy it to production and production will be exactly the same as your local and then of course your local is production and in this way multiple people can work on the same database making schema changes when necessary a very good way to manage things so you get the gist right it all happens in chronological order V1 V2 V3 so on so forth so what happens when somebody tries to change an old migration so let's go back and change V1 so this is the initialization one right so create the table books ISBN title but I don't know let's say the book also has it's a let's say publish date and let's call that a time stamp cool okay so it's going to try and add a publish date to to the table books or is it let's run the application and see what we get we have a massive stacked race so let's go to the top of this and try and make sense of it so it's saying here migration checks are mismatch for migration version one so that was what was applied to the database and this is what we have locally so remember that checksum in the database that thing that was uh uh over here well that is that is a checksum of what was applied and we can see here that we've changed it so it's completely different at this point so FlyAway is not going to apply it and that right there is a great safety feature to make sure the old database migrations can't be changed after they've been applied because who would really know what to do with that and there we go how you would use Flyway with spring boot now if you want to learn more about spring Boot and why wouldn't you it's a super powerful tool then be sure to check out our full spring boot tutorial right here taking you from all the theory building your first application right up to a finished rest API and deploying it to the cloud see you over there
Info
Channel: Devtiro
Views: 12,420
Rating: undefined out of 5
Keywords:
Id: IuKXPx3WLbg
Channel Id: undefined
Length: 15min 42sec (942 seconds)
Published: Sat Sep 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.