An open API service indexing awesome lists of open source software.

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.

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)