https://github.com/bhuvaneswaranmurugesan/alembic-sqlalchemy-mssql-setup
Step-by-step setup of Alembic and SQLAlchemy with MS SQL Server - demonstrating database version control, environment-based configurations, and migration workflows.
https://github.com/bhuvaneswaranmurugesan/alembic-sqlalchemy-mssql-setup
alembic database database-migrations learning-by-doing mssql orm python sqlalchemy
Last synced: 3 months ago
JSON representation
Step-by-step setup of Alembic and SQLAlchemy with MS SQL Server - demonstrating database version control, environment-based configurations, and migration workflows.
- Host: GitHub
- URL: https://github.com/bhuvaneswaranmurugesan/alembic-sqlalchemy-mssql-setup
- Owner: BhuvaneswaranMurugesan
- Created: 2025-11-08T18:10:58.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-11-08T19:00:27.000Z (8 months ago)
- Last Synced: 2025-11-08T20:27:42.167Z (8 months ago)
- Topics: alembic, database, database-migrations, learning-by-doing, mssql, orm, python, sqlalchemy
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# alembic-sqlalchemy-mssql-setup
π§© Alembic + SQLAlchemy + MS SQL Setup
This repository demonstrates how to set up Alembic with SQLAlchemy for Microsoft SQL Server (MS SQL) to manage database schema migrations efficiently and securely.
π What is Alembic?
Alembic is a lightweight database migration tool used with SQLAlchemy.
It lets you version control your database schema β just like Git does for your code.
Whenever your models change, Alembic tracks those changes and generates migration scripts that can upgrade or downgrade your database automatically.
πΉ Why We Use Alembic
- Keeps schema in sync across development, staging, and production.
- Avoids manual SQL ALTER statements.
- Works seamlessly with SQLAlchemy models.
- Enables rollback in case of issues.
- Easy to integrate in CI/CD pipelines.
βοΈ Advantages
β
Tracks schema changes automatically.
β
Version control for database structure.
β
Safe rollbacks using downgrade commands.
β
Works with many databases (Postgres, MySQL, MS SQL, etc.)
β οΈ Disadvantages
β Requires careful merge handling when multiple developers create migrations at once.
β Must ensure models are imported correctly into Alembicβs env.py
π§ What is SQLAlchemy?
SQLAlchemy is a powerful Python ORM (Object-Relational Mapper).
It lets you define your database tables and relationships in Python classes instead of raw SQL.
Alembic depends on SQLAlchemy to read these models and detect changes.
πΉ Role of SQLAlchemy in this Setup
- Defines tables using Python classes (Base, Column, Integer, String, etc.).
- Provides Base.metadata that Alembic uses for autogeneration.
- Creates the engine connection to MS SQL.
- Simplifies queries and database interactions.
π§° Project Structure
alembic-sqlalchemy-mssql-setup/
```
β
βββ alembic/ # Migration scripts & env configuration
β βββ versions/ # Auto-generated migration files
β βββ env.py # Alembic environment setup
β
βββ app/
β βββ core/
β β βββ database.py # SQLAlchemy engine & Base setup
β βββ models/
β βββ user.py # Example SQLAlchemy model
β
βββ .env # Database URL (ignored in Git)
βββ alembic.ini # Alembic config file
βββ README.md
βββ requirements.txt
```
### Note : The required packages and migration commands are provided in the migration-setup-alembic folder.
### π‘ Author
Created with β€οΈ by [Bhuvaneswaran Murugesan](https://www.linkedin.com/in/bhuvaneswaranmurugesan/)
π§ Email: bhuvaneshabi.5@gmail.com
π GitHub: [BhuvaneswaranMurugesan](https://github.com/BhuvaneswaranMurugesan)