FastAPI (Python Framework) - Auto-Generating Migrations with Alembic & SQLAlchemy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to describ today we are going to autogenerate migration using the SQL Alchemy and lmic so let's see that in action so for the simplicity so that you can also follow me throughout this video tutorial Ive created this Docker setup so it's a very simple to setup and you can see here in the description it includes MySQL reminder and mail pit so MySQL is the database ad manner is that we will use to browse the database in the browser and mail pit is if you are working with the mail okay and you want to test that in in your local machine so you can use this but for this tutorial it is not required but I have added here so let's set up this repository so it says clone this repository in your local Machine by typing this so instead of typing you can copy and paste so let me just uh open the terminal and let me go on my desktop let me paste this code okay so this has clone this repository you can see it here I have got this fast past stter git let me go inside the first API started kit repository The Next Step it says open the terminal and navigate to the project so if we are there we need to create this Docker volume okay so we can say we can copy and paste this Docker volume and uh just for just to let you know if you don't have Docker installed in your system right so you can go here dockerhub and here uh sorry Docker install you can you can search and you will find this link here so you can install Docker in Mac OS Linux or Windows whatever operating system you are using and once the installation is done then run this command otherwise it won't work so once this is done we can build the project so we can just copy this command do Docker compose build so let's go to the terminal and just type this so This build this will build the code that we have that we copied here and after that we can run Docker compos up so this will start the application okay so we can if I let me just make it bigger so you can see here SMTP server is started on 1025 Port my SQL is booting up ad man is started at 8080 and fast API is rning at 8,000 okay so we can go in the browser and we can open the 8,000 so let me open this you can see it says setup is done and working so that means our setup is done we can open the docs by saying SL docs So currently we don't have anything we have only root uh URL that say that setup is done and working you can see it here apart from this apart from from that if you want to browse the database so you can open the Local Host 8080 we saw that right and we can use the creden here so host is MySQL and you can see the username is root and password is this one so you can copy and paste this here so you will see that there is a database fast API so this is already created for us so once we create the models and migrations we can run the migration and those tables will be created here inside this database okay so apart from this we have also installed this mail pit if you want to see how the mail pit looks so you can open this local host and 8025 so you can see if you are working with your local uh any email application right that sends a email so that email will appear here if you configure this here I'm in the project that that we cloned previously so inside this project there is basic very simple setup so we have a Docker file that builds our code into the do Docker container and then we have requirements.txt so we have some fast API identic settings UVB SQL Alem and P MySQL installed already okay and then we have a Docker compos so that actually combines all the services together Docker services to be precise and then we have a ENB so here we have my SQL configuration and then app name and all okay apart from this inside the app folder we have main.py so that is our root URL that we saw and it creates the fast TP application so there is nothing in it and we have a config file so here I have a uh configured the database configuration okay so you can see it's using the database URI from setting okay so if you check the database URI so this is nothing but my SQL user my SQL host that we are getting from the environment variable okay and there is nothing fancy in this uh configuration in this project so let's now create the models okay so here inside the F folder I will create another folder called models and inside the models I will create a file user. py okay you can name it user model.py but I will name it user. pii only let me copy and paste the user model code okay because if I type it individually it will take some time so I have prepared it for you so you can see I have import the date date time and from SQL Alchemy Boolean column date time integer listing that I have used it here okay and from database that we have configured here inside the config I have imported the base so you can see this base okay so this is declarative base so our model is ready now let's start config let's configure the almic now so here I'm in lmic documentation official documentation so if I go to the installation page you can see we can install the P install lmic okay but I want to add this into the requirement. txt so if I go here you can see 1.2.0 okay so we can just add this in our requirements.txt okay like this and we can add the version now again we need to build the uh our Docker image when we build the docker image right almic wi not there and this is the dependency in the container so we need to rebuild the docker image so for that let me go to the terminal and by pressing the contrl C two times you can stop it or if you press one time it will stop it slowly but if you press two time it will be little quick so for building this we can say Docker compose build and we can say fast API service okay so this fast API service is coming from here if you are not aware I will just show you let me go to the here so this first API service is coming from here so this is the name that we need to put okay so let me go to the terminal again and run this okay so build is done now we can uh run the code again so we can say Docker compose up so application is running now the lmb is there inside the docker container so let's go to the documentation again and and after the installation we need to do the setup so let's go to here so you can see our project and then almic then env. pi and something like this okay [Music] so we can initialize the almic by this command almic IIT init almic okay so for this to run this what I will do uh I will split this screen here and let me just run this so I will say docker compose run and then service name so fast API service and SL bin SL sh and hyph c and here inside this double codes right whatever command we put that command will be executed inside the container directly okay so if I run this you can see it has created the almic and almic versions okay so if I go inside our code right right so this should be created there as well so you can see LM is created here okay so it's created here if you open this almic do inii so basically this is the inii configuration for the almic and uh if you go here script.py so using this uh this is not py actually this is py. Meo file so this file actually used to create the migration whenever we run okay and apart from this we have this env. py okay so this is the environment file for the lmb so you can see SQL al. URL so from here it's getting the uh login URL so we need to modify this so before that I just want to walk you through so whenever we run the migration right so this particular uh block of code will be executed so this will be in online mode and there is another offline mode okay so here we will be including our models and uh connection Ling to get the connection string like uh if you remember we have created our connection string inside the settings right and here is a method settings that we can call to get the setting information so for that uh let me import the setting here okay so I will import it here so I will say from typ do config do settings import get settings and then I can initialize the setting by saying settings equals to get setting okay so let me put it before this so here we are importing everything so let me put it here okay so we got our settings so now we need to set this uh SQL Alchemy URL okay so for this if you see here this URL is coming from this config okay and this config is here so if we set the URL inside this config using the same name that it's trying to fetch we will get I mean it will be set there right so we can set this by saying config do set main option and inside the main option we can pass uh this name SQL al. URL and then we can say setting do database. U this this will work but there is just there is a case that I have seen uh like whenever your password contains a let's say uh at theate or some symbols right so that gets converted into percentage value okay like something like this so for that we can use the uh let's say replace here so we can replace that uh single person sign with double person sign so that it works in all the cases let me put it in double quotes although that doesn't matter okay so what whenever a particular special character is encoded in uh perent C right if we put one here then what will happen this uh this almic will not consider that as a encoded okay so we need to make it like this so now this is uh done apart from this we need to import our model that we have created inside this okay and also we need to import our base model uh okay so for that we can include it here so we can say from app.config do database import base okay and we can import our models here so we can say import all models so we can say from app do models do user I will say import everything and this base we need to configure okay so if you see here it says the target metadata so this is our base model right uh where is this so we need metadata from this base so we can say you can see there is a example given here my model. base. metadata so I want to include this for all the models I don't want to put a specific model here so I will say base dot metadata okay so our configuration is done now and our model is also done so we can run the command Now to create a migration uh let's browse this so in setup is done here okay I I we did not needed to change anything apart from this migration script so we can create a migration using this so lmic revision hym uh then we can uh pass some text here for what we are creating the migration right so let's try to do this so let me copy this command and let me go inside the terminal so I can say Docker compos run fast API service okay so bin slsh hyph C and then then we can put our Command here so I will say create user table create user table but we want this to if I run this command you will let me show you what will happen Okay so if I run this command you can see this command is generated a migration here and let's go inside the code so inside the versions you can see it has generated a migration but currently there is no code okay we did not tell the almic to generate migration using our models okay so let me delete this migration actually and let's generate the migration using our model okay so for that uh let's go to the terminal again and I will just run the same command but now I will put hyphen iPhone Auto generate here now the almic know knows that I need to autogenerate the migration and for for autogenerating the migration alic is going to look into this EnV file and it will see all the inputs that we have done here okay and then it will go through inside this user model and it will find out all the columns and their data types that we have defined and based on this it will generate the migration file so if I run the command again okay so there is some error it says date time has no attribute now okay so let's see let's go to the code okay so it should be like this from date time import date time okay you can see currently migration is not generated because of this error and uh if we go into the terminal and run it again you can see the migration is not generated and you can see detected uh added table users and detected added index user email on email so it has already detected something okay so let's see that so here if you see the email we have added a index here right and this is also your unique index we have added so if you go to our migration now you will see all the columns is added automatically okay and index is already added here and unique is true so whatever we had defined here inside this right it has automatically generated so now our migration is ready we need to apply this into our database okay so for that if we go to the documentation and just scroll down little bit running our first migration so we can use this command alumic upgrade head and this will run the command Okay so for this we can go into the terminal again and let me just remove the previous command that we had there and let me paste this so almic upgrade head this will create the users table so if I run this you can see here we will assume non transactional DD and running upgrade so this particular ID is created now so if we go to the browser and open our database uh we have it here now right so if I refresh this you can see there is a alumic version so using this table it tracks the migration that we apply and you you can see the users table here and you can see your primary key is ID unique index is added as email and all the fields is added here if you want to rever this so for that if we go to this uh let's say running or second migration we'll see that later okay so for reverting we can say LM downgrade and then we can put uh either minus1 or min-2 how many migrations we we want to rever so we want to rever only one migration because we have only one so for that we can go here and we can say lmic downgrade and minus one if I run this command you will see that running downgrade and this is downgraded now if we go to our database and if I refresh this you will see the user table is gone currently we have only lmb and there is no data inside it so let me run it again upgrade this so that we have our users table here so we have you can see this uh uh version and user table so let's assume on scenario that you have migrated your table right and suddenly you remember that you forgot to add one column and in our case let's assume that we forgot to add our mobile number column here and I want to add this again so for that we can go to our code and uh here we have email so let me just duplicate this and I will say mobile and I will put 20 character for this this is again unique and I want to Index this also okay let's remove the unique attributes here it's up to you if you want to keep it I will just remove it so we have added our mobile column here and just now I remembered that I did not needed this delete add column so if you want to remove a column you can also remove it from here and save this model and if we go to our terminal again okay and we run Docker compose run fast API uh service SL bin slsh hyph C and for this we can again run our same migration command that we saw earlier that is our almic revision hyphen M let me paste this and hyphen M and we can say here updated user table columns if I press enter it will generate another migration file okay and if I go to this file you will see it has added a column mobile inside the users table okay and for the users table it has created Ed one index also okay and for deleted it that we removed it has added a command to drop that column and let's apply this so if I run alumic upgrade head so this will apply this mag in our database let's go to the database now you can see here I have deleted it but I don't have mobile number right now but if I refresh the page you will see that deleted it is gone and I can see the mobile columns here and and mobile index also here so this is how you can keep the change of your database similar to what you can do with your code using the git so using the lmic you can keep the revision of your migration files and all there is one more command for almic I want to discuss let's see if you want to drop all the tables that we you have so you can use this command almic downgrade base and it will drop all the tables and using this head you can again apply all the migration file that you have so let me try to run this and drop everything that I created so uh let's delete this and if I run this command you can see it has dropped both the migration files and if I go to our database if I run this you will see user tables is not there we have only lmb revision table and it doesn't have any data I will post this this code on GitHub and I will paste the link inside the description so that if you need you can have a look on the code thank you so much for watching this video tutorial and if you have any doubt or questions regarding this tutorial please comment it down I will try to reply as soon as I can if you haven't subscribed the channel or like the video please do and I will see you in the next one
Info
Channel: Describly
Views: 2,571
Rating: undefined out of 5
Keywords: fastapi-migration, migration in fastapi, generating migration in fastapi, sqlalchemy, alembic, fastapi alembic, fastapi migration, sqlalchemy alembic, generating migration for product in fastapi, Auto-Generating Migrations with Alembic & SQLAlchemy
Id: HuOG7VS8qvE
Channel Id: undefined
Length: 23min 37sec (1417 seconds)
Published: Fri Oct 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.