Declarative schema migrations with Atlas: An introduction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] in this video I'm going to show you Atlas an open source database schema management tool that we're building at a Riga and I'm going to show you well how you can use it to manage your database schema let's start by installing it we have a nice shell script you can use to download with curl and execute on your machine and well Atlas is sometimes called by members of our community it's sort of a terraform for databases and I want to show you in this video and hopefully by the end of it you understand why uh it's called This Way so for the purpose of this demonstration we're going to use docker when I use Docker run to run a local mySQL database on our machine and we're going to work against it as if it was our production database okay now this started running the next step is to Define our database schema I'm going to do this in a few steps okay so let's call our schema s dot HCL HCL stands for hashicorp configuration language it's the infrastructure for configuration languages that was created at hashicorp the company that built terraform and well you're starting to see some similarities between terraform and Autos okay so we start by defining our database schema this is the name database containing our our table and this schema by the way already exists from our Docker run command I'm just going to take the schema and go over it so you can see we Define a user's table that's defined to be placed inside the example schema it has an ID column and a name column and the primary key is defined to be the ID column now if you've ever used terraform this part is going to be feels similar to you and we're going to go Atlas schema apply and we're going to give it the URL to the database which is MySQL and um root it's located at localhost and let's see this now and I'm going to give it the file okay the file defines the desired state of the database and I'm gonna fire this command now what happens now is that Atlas connect to the database it inspects the local database schema and then a diff it creates a different comparison between the desired state and creates a plan the plan is basically a sequence of uh statements that need to be executed to reconcile the difference between the desired State and the actual state of the database and just because ddl statements or schema changes are dangerous then Atlas verifies that we are sure that we want to execute this cool this is run and now our database schema fits the same a schema we defined in our schema file now let's add another table let's say we are building a blogging platform like WordPress or something like that and once we have our users table we want to add the blog post table and it's going to be a bit more complex so let's go over it cool so our blog post table also plays in the example schema it has an ID column a title column a body column you see each of them has a different type this is text the previous one of those varchar of 100 and here we have the author ID which as you can guess is a foreign key meaning it references the users table and we also expect that we're going to do frequent searches by author which is why we added a non-unique index on the author ID column now we can see what plan uh Atlas is going to generate for our new schema so as you can see a atlas plant a new create table statement to reconcile the difference between our desired schema and the physical schema the the actual schema of the database let's apply this [Music] now what do you think will happen if we rerun Atlas with the same desired schema I get the same database let's see when we run this we see that Atlas is outputting schemaizing no changes to be made and this shows you the similarity between terraform and Atlas which is the declarative model in the declarative model which is very popular with infrastructure software like there or kubernetes what the user does is provide the system the desired State and you let the tool compute the plan how to reconcile the difference between the desired and the actual State and once that state is equal no changes are to be made cool so in this video we showed uh what we call in Atlas the declarative migration workflow in which you kind of in a terraform-like way Define the desired state in HCL you can do it in SQL as well by the way and you let uh Atlas compute the plant runtime scheme apply and you approve the plan and then you run it but for many teams this is not going to work and they want more control and they want to code review every change and I'm going to use the more traditional version migration flow which you get a migration directory with scripts which you run in sequence and you have each schema migration as an actual revision and we're going to show that flow which Atlas supports as well in the next recording so if you want to get that why not subscribe to our Channel thanks and goodbye
Info
Channel: Ariga
Views: 2,903
Rating: undefined out of 5
Keywords: database, devops, terraform, ariga, atlas, software engineering
Id: kzMkDxL95UI
Channel Id: undefined
Length: 6min 37sec (397 seconds)
Published: Mon Dec 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.