Install Alembic In Python
Last Updated :
08 Feb, 2024
Alembic is a lightweight database migration tool for Python. It is widely used in conjunction with SQLAlchemy to manage database schema changes. Database migrations are crucial when developing applications that evolve, as they allow you to make changes to your database schema without losing existing data. In this article, we’ll explore the process of installing Alembic in Python.
How To Install Alembic In Python?
Method 1: Install Alembic using PIP
First, open the command prompt with the administrative user on your system and execute the below command in the prompt to install Alembic using PIP.
pip install Alembic
This command will download and install the latest version of Alembic along with any dependencies it requires.
Once the installation is completed, our next task is to verify the successful installation. So we can verify it by checking the information about the library. Execute the below command in the prompt to verify.
pip show Alembic
Output:
Method 2: Install Alembic using Conda
We can also install the library by using Conda. So to install the Alembic using conda, execute the below command in the terminal.
conda install alembic
To verify that alembic was successfully installed on the system with conda, run a command in the command window.
conda list alembic
Output:Â
Check ‘Alembic’ is Imported using Code
In this example, we defines an Alembic migration script. It creates a table named ‘users‘ with three columns (‘id’, ‘username’, and ’email’) using SQLAlchemy’s table creation functions in the upgrade() function. The downgrade() function is defined to drop the ‘users’ table. To execute this migration, you would typically run Alembic commands such as alembic upgrade head to apply the migration and alembic downgrade -1 to revert it.
Python3
from alembic import op
import sqlalchemy as sa
def upgrade():
op.create_table(
'users' ,
sa.Column( 'id' , sa.Integer, primary_key = True ),
sa.Column( 'username' , sa.String( 50 ), nullable = False ),
sa.Column( 'email' , sa.String( 100 ), nullable = False ),
)
def downgrade():
op.drop_table( 'users' )
|
Output:
INFO [alembic.runtime.migration] Context impl SQLite.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade None -> 2ee23e7320d3, empty message
The output would vary depending on your database and Alembic configuration. This output indicates that the migration was successfully applied, transitioning from an empty state to the migration identifier 2ee23e7320d3.
Conclusion
In conclusion, Alembic is a valuable tool for managing database migrations in Python applications, particularly when paired with SQLAlchemy. This article provided two methods for installing Alembic using PIP or Conda. By demonstrating a simple Alembic migration script, we highlighted the ease of defining and executing schema changes. Adopting Alembic facilitates the seamless evolution of database schemas over time, ensuring data integrity throughout the development lifecycle.
Share your thoughts in the comments
Please Login to comment...