Microservices in Go - Heroku and PostgreSQL P2 - DB setup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so let's start in on the database modeling okay so let's we're not going to worry about the connection for now um let's make a new file uh let's call this one agent dot go and we're going to make it type i'm going to call it agent and it's a struct leave that blank let's make a new file and let's call it home.go and we'll make a type here home all right so we have two types we're gonna have a home and an agent so the idea behind our model here is homes will be sold in our system so we can create we'll do crud operations on homes and we just kind of want to track who sold the home so what agent so we're we're imagining that we have some kind of like real estate firm right and we're just tracking uh we're tracking properties we sell so our home would have an id it would have like the seller the the price uh maybe an address and a description so just a couple of fields like that an agent will be really straightforward we'll just put an id um we'll put a name and that'll be it for the agent here so let's look at go pg so first things let's get the package here let me close down docker compose down clear and let's get our package okay so things that our home needs so we'll need an id um the type let's say n64 and we'll give this id okay and we can just look and see some examples of how they're doing it here so they're using n64 for these ids uh string array of string etc so this will be so this is a relationship this is how you do relationships and go pg we're going to have a similar relationship for our agent so let's bring this over we'll say agent and this is going to be our agent here so relationship every home should have a selling agent that'll have one selling agent right similarly here they're doing this story every story has one author so that's how we'll do that um let's put an id in here and we'll just make this an n64 as well id and we'll give this a name okay over here um let's see what fields do we want so we want the price we'll just do another n64 this could be a float but i'm just going to keep it simple say price um do a quick description which is just a string and maybe we can do an address okay all right that looks good uh let's do json yep okay he must be separated by space all right so here's our types we have a home we have an agent here so now what we can do is start actually using these in our api in our router okay these will be the same types so we'll talk about this really briefly sometimes you'll see separation of types here so for example you would have database types so agent and home and then also in your api you would have um types different types as well so you would have like an agent type defined in here sometimes you don't want to expose everything from the database through your api so it's typically good practice to have separation of types there's going to be a little bit of duplication maybe maybe some types you won't need a separation and they'll have the exact same fields that's fine but it's still usually good practice to have that separation for us we won't do that we won't be that extreme but we'll have things like um we'll have the api response so delete home by id response is what we can do and we can say struct and we'll determine what that returns maybe something like a success maybe something like that right um similarly we'll have like get home i'm just doing some for an example we'll fill these out later so get homes response this would be a struct and this would say what do we say homes and this would be an array of db dot oh that would be the api response there so we're just going to use the same models the same structs and types as our db and that's that's fine for our case okay so we're working on the db here we have an agent set up and we have a home set up so let's start thinking about migration scripts so if you're not familiar um with migration scripts when we start up when we do docker compose up um or when we're making a deployment for example we need to make sure our database is in the right state maybe we have um maybe we're going to have some database replicas right so we have a big system going on we have three or four databases and we want to make sure they all have the same tables all of the same columns etc so every time you deploy you can run migration scripts so those will run and they're basically basically you can just execute sql files and it'll just do things like insertions table drops table deletions table updates etc and make sure that your database is in a consistent and recreatable state all right to do this we're going to make use of gopg migrations this package we saw previously um so if you haven't pulled it down you can just run this command go get over here i've already pulled it down let's pull it down one more time there we go and let's jump to the godox so gopg migrations has this type called collection and a collection is essentially it's a collection of migrations that can that this package can run and as a nice feature when you make a new collection you can run discover sql migrations to automatically populate that collection with migrations and it picks up all the files of this dot sql extension in a directory that you specify so the first thing let's do let's make a new folder here we'll call it migrations and this is going to hold the migration files so let's create two files we're going to create basically one migration script for agent and one for home so one for each of these here so let's create those we'll call the first one one agents dot up dot sql and we will call the next one two and homes dot up dot sql so we will see in a little bit there are two options we can do we can do up or down and um um go pg migrations that automatic discovery feature will pick up files with that with that um extension so dot sql or dot down dot sql um the up migrations run uh when you do an upgrade so when we go to run let's see here when we go to run our actual migrations there's a few different things we can do we can run up which will actually upgrade the migrations to the latest version or we can run down we'll rarely use these down migration scripts but maybe you'll need it for your individual purposes or businesses needs uh we won't worry about those today we're just going to worry about the obscripts okay so basic post grades basic sql time let's create a table if not exist agent so let's do create table if not exist and we're going to call it agents and here we go and this will have just two columns we're going to do ide which is going to be serial and it's going to be our primary key we'll need a comma and then the next we'll just have an agent name it's a varchar and we'll just say all right so there's agents that looks good and we'll do homes next so same thing create table if not exists i'm not going to talk too much about sql syntax i'm just assuming you're somewhat familiar with it if not there's plenty of good resources to learn so home we had an id which is a serial and this will just be our primary key what else we have we have the price which is we'll just make it a big end here and not null we have our description of the property we'll just say varchar not null and address also varchar null and agent id and this is going to be a big end so this agent id this is the basically going to be the foreign key that'll link us over to the agent table okay so this is set up here and now let's make our connection to the database so um the goal things we're gonna do in here whenever we create a new database we're gonna one connect 2db we're going to run migrations and then finally we can just return the db connection okay so this should return the database so let's see what type is that pg.connect and what does this return this returns a pg.db so we could say pg.dp cool and let's go ahead and just look at the examples they have for connecting let's see we just want pg all right so pg connect let's bring that down there we go and we're going to return our db okay so our user was postgres we had a different password our password was admin but our user was postgres so let's do that password admin and so one other thing we're going to need here is the connection string um the address here so the host 12 by the port so for us this will be db 5432 so 5432 if you look at the documentation for this postgres container this is the default port that it will always listen on so we're using that default port and db is the actual address that we've wired up between our two containers okay so there's our connection so next step let's run the migrations well first uh we can actually run this and just see if this works let's run that really quick um and see if things build and run see if that connection works i just realized we need to call this so new db we can go ahead and say um equals db dot new db there we go so now we'll actually call that inside of our main function so let's dock it forwards down and restart cool so that passed um so we were able to make our connections that's good all right so next step let's get these migrations running so um from the godox we'll make a new collection yeah and this is gonna be migrations.new collection okay and then we can do collection dot what was it describe yep discover sql migrations and our folder is called migrations so we'll do that migrations this returns an error let's return that error return no comma error return db okay so we'll discover the collections if there's no error um there is a function called collections.run and it takes a db and it takes a command so the commands we have available there's one that's up um there's one that's init so we'll actually have to do this init at the start so this admit checks um checks to see if there is a special table and that is the the uh versioning table so whenever you do migrations you need to keep track of the version right so which which one of these scripts is run how do we know that um any uh usually migration libraries will have a versioning table so we need to run that init command first that init and this will set up that table if it doesn't exist for us and then we can run our up command next this returns old version new version we don't care about those okay and if air doesn't mill we'll do that all right so after this um then we can run the up command and let's take a look here at some other examples let's see main.go this runs the examples i believe yeah okay so migrations.run and then they log out the version which is nice so let's copy that over and do that all right so we're gonna do collection dot run what does that take yep db and then it takes the command which is gonna be up and we'll return if there's an error we'll return it um otherwise this is checking to see if the versions match and it's printing based on that okay so we're making our connection we're building our collection we're discovering the migrations we're setting up the versioning table if it doesn't exist and then we're running our migrations here finally we're just returning the database connection that looks good and main this now returns the database and an error so let's handle it um we'll just panic so if we're not able to set up our database connection and run migrations our server is just going to panic and terminate basically all right and this is no longer the first time error is defined so we've got to remove that colon cool let's go ahead and rerun this okay and we have a error here panic error at or near big int all right so it looks like i messed up the type here and that's one word okay there we go all right so we started up and we're running no errors and we see migrated from version zero to two so we ran these two migration scripts and now our version is at two so those tables actually exist now so that's good awesome all right so let's start um let's start wiring up some of our apis here uh thank you so much for tuning in really appreciate it hope you guys were able to pick up some new stuff and learn some learn some new things here um in the next video we're going to cover wiring up all of our models all of our endpoints and we'll continue the series thanks for watching please make sure to like comment and subscribe to the channel uh really helps me out lets me know that you're learning and enjoying the content i'm putting out thanks so much i will see you all next time
Info
Channel: devtopics
Views: 885
Rating: undefined out of 5
Keywords: microservice, golang, microservices, heroku, api, backend, software, docker, docker-compose, docker compose, deployment, deploy, postgresql, postgres, go
Id: AJzqc8BGeao
Channel Id: undefined
Length: 17min 25sec (1045 seconds)
Published: Sun Aug 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.