Create a User Authentication Model (Database Migrations With Alembic) - FastAPI Beyond CRUD (Part 7)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone welcome back at this point in the series we've been able to create pretty much all of our CR functionality but CR is not the only thing that you're going to be looking at in this course so let us Implement some features that go beyond the crowd now the first feature we going to implement is one for creating user accounts so any application requires some sort of authentication or authorization Authentication being the act of letting users identify who they are within our application and authorization allowing users to give them access to various parts of their application based on the specific roles and permissions they have now we pretty much have a simple web application set up and let us go ahead and set up our user authentication model so to do that the first thing I'll do is to go within ourour directory right here so I'll create a new fold and I'm going to call this o standing for our authentication and inside here to make it as a python package I'm going to add a danda init.py file now inside here I'm going to also go ahead and create a m.p file and this is going to be for our user authentication model now to begin we're going to go ahead and import the esql model class just like we've done for the previous videos now let me go ahead and close some of stuff stuff that I have right here so let us start by importing SQL model so to do that we are going to go ahead and save from SQL model we are going to go ahead and import our SQL model now after importing our SQL model we're going to go ahead and pretty much set up our database model for users now let us go ahead and simply look at the fields we're going to add to this class so we're going to create a simple class and that is going to be our user class this class is going to have the following field so it will have a u ID just like we had so this will be of type uid do uid just like we discussed in the previous videos we also going to have a username for a user so this will be a string we shall have an email and then we can have other attributes such as the first name which is the string as well as the last name so this will also be a string we can also have the is verified field so this will be the one to basically show that the user has been verified so this will be a Boolean and it will have a value or false at the beginning of the account creation so we may carry out things like email verification that may help us to verify an account and stuff like that so once you've done that then you're also going to finalize this by simply going ahead and uh adding the created act as well as the updated at times STS so to do that we're going to say created at and this is going to be a DAT time object and then we can finally have our updated at and this is going to be still a date time object now remember these are poql timestamps and we looked at how we could set them up in the previous video so to begin we're going to go ahead and create this class so I have the freedom I'll just go ahead and copy what we have here or I'll even just cut it then remove what we have here oh sorry for this so I'm just simply going to go ahead and remove this doc string and then I'll just simply paste what we have there so we're going to begin by first of all importing U ID so I'll just go right here at the top and say import U ID and once we've imported U ID we've taken care of our type for our U ID now another thing we may want to do is to go ahead and specify the name of this table as you want it in the database so to do that you shall just come and provide the special D table name attribute but remember we also have to go ahead and set this up so that it actually stands for a table uh inside our database so we can first of all call this our users table and then we can finally specify that this is going to be an SQL model sub class another thing to make this is a table to make this a database table we shall just say that this is going to be a table so shall add table equals two and this will be our database table now let's go ahead and complete what we have right here so we've specified that our U ID is going to be of type uu ID but for us to make it a u ID we're going to follow the same concept that we looked at in the previous videos so what I'll do is to just simply come right here I'll copy what we have and then paste right here so I'm just going to add an addition to this and this is going to be our field but it's going to be a poql ID type so I have to go ahead and import our field so I'll just come right here and say import field and we I'll also go ahead and import our column function so once we've been able to do that now let's go ahead and also import our poql dialect so I'll say from or let's say import SQL so it's going to be import SQL Alchemy what some of you call SQL Alchemy so I'll just say import SQL Alchemy do dialects do postgressql as PG so once that is done then this will Mark our column as poql ID tab so once that is done now let's go ahead and just copy paste what we had for the time stamps and we need head over back to our books model and then I'll copy what we have here and I'll paste it just straight here so I'll just go ahead and paste this uh now one thing we can do is to also go ahead and import our daytime class so I'll just that by saying from date time we are going to go ahead and import our date time so this going to be our dat time and uh we see that this is a Boolean so it should be false so it's going to have a default or false just in case we do not have it being true at the beginning and you can also do this by using the field function so we can say something like field and inside here we can say that this is going to have a default of false so just like that we've been able to create our user account model Now to create a string representation of our models of our model objects you can simply go ahead and create a Dand RI method so this will be Dand RI and it will take in then it will return a string representation of a user object so for us to do that we shall just simply come and say user and in this case shall just say user do username so that's how we shall be visually seeing these objects of type user all right so now our database model for users has been created hoping you don't have any mistakes uh we've been able to create this and we need a way for us to go ahead and reflect this table within our data datase now in the previous videos if you looked at chapter 5 we simply created our databases in the table using a lifespan event if you go back right to our Dand unit. within our source folder we created this lifespan event right here that was necessary for going ahead and simply creating a connection to our database and creating any database tables that are going to be available within our SQL model metad dat now this approach is a good one but we are going to need one way to basically perform migrations to our database and that's where almic comes in so almic is a tool that allows us to work with SQL Alchemy and orms that use SQL Alchemy for us to be able to carry out database migrations on our SQL databases now if you're wondering the importance of migrations what migrations help help us do is to make changes to our database without having to lose data so we now have an existing database and that database has book objects but want to go ahead and make changes to this database without having to drop any tables or modify or or delete anything within our database so this is where Al comes in so we're going to go ahead and set it up and let's begin by installing it so I'm going to head over right here and I'll begin by installing almic so I'm going to say pep install and then I'll say almic and what this will do is to add almic within our virtual environment so once almic has been installed we can confirm whether it has been installed by running almic D Das help and so we now see almic has been installed but this is going to be D D help so when we run d d help we're going to see Al limic and the different arguments the different commands that are attached to alic so almic has been installed now the next thing we're going to do is to create what is called a migration environment so think of a migration environment as an environment to help us run our migrations inside this migration environment we get to create versions or files that describe the changes we've been able to do on our dat database with time so whenever we create a change to our database we're going to create a migration and that migration is going to be reflected inside a file this file is going to be called a version file and inside that file will be the changes to the structure of the database that we are suggesting and once we've been able to suggest those changes then we shall finally go ahead and make those changes reflect within our database so since we using a DB API I also help us helps us to create a migration environment by using templates and it provides a very good template for using an async DB API so let's go ahead and do that so right here now time you know the first thing we're going to do is to run the command almic and in this case we shall say in it then to specify a template we are going to use the dash T option and then we are going to say that we're going to be using a sync template now right after specifying that we need to provide a name for where our migration environment is going to be created now let us call this the migrations folder and shall be created within the root of our project so I'm going to go ahead and press enter and just like that we shall see that a new folder has been created called migrations uh some files have been created inside there as well as anic file so when you go back to our project right here we shall now notice that we have a new file called alic we also have a migrations folder created right here so let us look at what is inside here so the migration folder is our migrations environment so when you enter this folder we have four items the first one is going to be the versions folder or the directory versions and this is where the versions of our changes to the database are going to be kept now think of versions as files that track migrations or database changes at a specific point in time we also have the EnV file which is the entry point to aling so this is the main working file that almic is going to be using to carry out migrations to our database we have a rme MD file it's actually readme file and it's just to describe whatever is going on within our migration environment and a script. Pym file which is just simply a template for AIC to do it thing of creating the migrations or establishing changes to our database via migrations now right after looking at the migrations environment let us go ahead and also look at almic so almic is the main configuration file that almic is going to be using to do whatever it wants to our database so in here is where I will go ahead and write things such as the target database we are targeting to make changes to and so on so if you want to carry out any sort of migrations you do it all inside here now once this is done we are going to make changes to our migration environment so that you can be able to finally adopt to our I think DB API as well as to introduce the available models that we have within our database so the first thing we're going to do is to head over to our env. py within our migration so I'm going to go right there and inside here is where we're going to first of all import our models as well as to point to the database that we want to Target so we're going to begin by importing our user model so we're going to say from source. o we are going to go ahead and import our user model as well as our book model so we shall say from source. books we are going to go ahead and also import our books model and right after doing that we are now going to also import the SQL model class that we have used to create these models so shall say from SQL model we are going to go ahead and get access to the SQL model class so that's going to go ahead and import SQL model now right after doing that then we have a couple of things happening right here but let us look at what is going on here so we have this config object that is going to be one that's going to provide access to the values within the file so by using this config object we can go ahead and try to make settings to everything that's within our alic right here so the first thing we may want to do is to go ahead and Target our database so when you look inside our alic file right here we have the SQL Alchemy URL variable and what this is is going to be the name of the database that we are going to be using or the URL to our database now for us to be able to use our environment variables what we're going to do is to go within our env. piy and we're going to use this object by saying config Dot and in this case we're going to call the set main option function what this is going to do is to take in the specific variable that you want to change and that's going to be our SQL Alchemy URL and then the value that you want to modify that specific variable with so we're going to say this going to be SQL Alchemy URL so I'm going to change this to SQL alemy URL and then we are going to go ahead and set this up to be the database URL for my environment variables so to do that we're going to first of all import the config object that we created within our config file so that's going to be within source and then config so this is what we actually need to access our setting so I'm going to go within our env. py and then I'll come and say from source. config we are going to go ahead and access the config object or config object so once we've imported this config remember this is in uppercase so this is going to allow us to access our database URL now what we can do is to just simply say that our database URL is going to be equal to config dot database URL and once you've been able to do that now let's go ahead and substitute this for with our database URL or provide this argument for the value that you want to make this SQL Alchemy URL be to our database URL just like we've done right there now once that is done let us go ahead and also specify the target meta data now remember every time we want to access that Target metad data object we shall have to do that via the SQL model class so we are going to go ahead and do that so shall just come right here and say SQL model Dot and in this case we shall access the metad DAT object so once you've been able to access the metadata object then that will be it for the envp file so once we've been able to set that up now let's head over to script. p.m and inside that is where we're going to go ahead and import our SQL model so shall just come and say import remember this is the template that alic is going to use to create our migration so shall say import SQL model and once that is done we are now ready to get our migrations done so for us to create a migration we going to be using alic and then it will go ahead and create a versions file that is going to keep track of the migrations that we've created so let us go ahead and do that so I'll pull up my terminal and then I run a liic and this case if we need to create a version or a migration we are simply going to say alicevision and in this case we are going to specify a message for that version but we also need to go ahead and describe how we want to create that migration so most of the times when you run a aric revision you are going to create a version file and then you can edit it so that you specify the changes you've done to your database but in this case we want to go ahead and use the autogenerate flag to basically go ahead and create those changes based off the state of our model so we're going to automatically generate our migrations based on how our models are looking for now so once you've been able to run auto generate then let us go ahead and provide a message for our migration so to do that we're going to go ahead and say- M and then we shall call this init so I go ahead and press enter so we now have an error here saying we cannot import the name user from source. so I'm going to go ahead and fix that we shall head over to our emvp and then we shall say from Source do o do models so I think I did the same mistake for books so I'll save from source. books. models we shall go ahead and import our book which is actually book not books then this is also going to be user so we going to have book and then we also going to have user so when you head over back to our or folder in modos we have a user class all right so let us go ahead and run our migration once again so I clear my control and L and then when I run this command we're now going to see that a new migration has been created so to check out that migration all we're going to do is to go within our verion folder right here and now we shall notice that a version with this specific version number has been created now inside that version we are going to have uh some things uh described so the first thing we shall notice is we have some information about our revision or our migration and this case it will give the message of that specific migration which we have given us in it and then the revision ID which is going to be this random kind of uh hexad decimal value then it will specify the data which you created in migration which is this specific date and then a lot of inputs then we didn't have the revision the down revision so just in case we are to revert a certain change then we're going to have a down revision Branch labels uh depends on so in case have any depend if in case a migration depends on some other change then that's where we're going to specify that so shall specify a string for that migration it depends on and then none just in case it doesn't exist so the most important things we need to see within our migration here are going to be the upgrade function as well as the downgrade function so this is where I want you guys to pay attention the upgrade fun function is one that's going to be the one to introduce the changes to our database and in this case since we have our book model already created it is going ahead and neglecting that to create the table users so it will go ahead and create this table called users within our database and it will specify the columns within our database as well as the different attributes that are associated with those rules so for example our uid is going to be an SQL Alchemist mu uid type and it shall be it shall be a column that's not going to beable so it to go ahead and specify all those types depending on the model that we created for user and then it also add the constraints just like you see right here it's going to mark this as a primary key field and just in case we are to undo that specific migration or revision we have a downgrade function right here and it simply has drop table user so inside here we have create table users because we are going to create the table users as well as the fields that are contained in that table and then we also have downgrade which has drop table users so the op is for alic so alic is going to run a CR table function as well as a drop table function in case we are to undo that specific operation so now we've been able to Simply create a revision or a migration now the next thing we're going to do is to go ahead and apply this migration to our database so that this table is created within our database so for us to do that I'm going to go ahead and simply pull up our Termino once again and in this case shall just come and say alic upgrade and in this case we shall say head so what this is going to to do is to go ahead and look at our latest migration and then apply to the database so going to press enter and now we shall see that our upgrade is going to be run and it's going to be applied to our database now let us go ahead and look at the structure of our database and see if this has been applied so I'm going to pull up my terminal and I'm going to make this a little bit bigger so that you guys can see so I already have psql and I'm going to connect to our database which we called bookly DB so inside there I'm going to look at the structure of the database so I'm just simply going to say uh DT to show the table so we have an Olympic version table we have the books table we have the users table so the Olympic version is what is going to keep the versions of our tables or the versions of our migrations that we've been carrying out in fact If We R something like select star from let's say our Olympic version in this case we're going to see that the current revision is the one that is going to have so it's going to have this version number and it will be the version of that specific migration that we have run and this is our migration right here so now that we see that this almic version has been created another thing that we want to see is the table of our users so I'm going to just simply WR and describe on that users table and then we shall see that our table has been created and all the keys or all the rows have been created with their respective types and constraints just like you see right here so now successfully created our database table for Authentication
Info
Channel: Ssali Jonathan
Views: 342
Rating: undefined out of 5
Keywords:
Id: jPTJ0i1JM3I
Channel Id: undefined
Length: 26min 31sec (1591 seconds)
Published: Thu Jun 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.