-
Notifications
You must be signed in to change notification settings - Fork 198
feat: add pov of maintain scheama version in git #11232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sonichigo
wants to merge
2
commits into
harness:main
Choose a base branch
from
Sonichigo:dbops-434
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
docs/database-devops/get-started/gitops/maintaining-database-schema.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
--- | ||
title: Maintaining Versions of the Database Schema in a Git Repository | ||
sidebar_label: Maintaining Versions of the Database Schema in a Git Repository | ||
description: Learn how to maintain versions of your database schema in a Git repository using Harness DB DevOps, including best practices for version control and collaboration. | ||
sidebar_position: 21 | ||
keywords: | ||
- database schema deployment | ||
- deploy schema changes | ||
- db schema updates | ||
- liquibase deployment | ||
- database devops | ||
- harness dbops | ||
- schema migration | ||
- ci cd database updates | ||
- changeset deployment | ||
- rollback and versioning | ||
tags: | ||
- harness-db-devops | ||
- schema-deployment | ||
- changeset-management | ||
- ci-cd | ||
- rollback-strategy | ||
--- | ||
|
||
Harness Database DevOps enables you to manage database schema changes as version-controlled .yml changelogs and changesets in a Git repository. This approach ensures consistency, traceability, and collaboration across development, staging, and production environments. | ||
|
||
## Why Version Your Database Schema? | ||
Version-controlling database schema changes offers several advantages: | ||
1. **Auditability**: Every schema change is tracked in Git history. | ||
2. **Collaboration**: Multiple developers can work on database changes without conflicts. | ||
3. **Rollback Support**: Historical versions make it easier to revert unintended changes. | ||
4. **CI/CD Alignment**: Database deployments can follow the same GitOps principles as application code. | ||
|
||
## How Harness Database DevOps Manages Schema Versions | ||
Harness DB DevOps uses Liquibase-compatible .yml changelogs to define and track changes to your database schema. | ||
- **Changelog File**: A .yml file that lists all schema changes in sequence. | ||
- **Changeset**: An individual change unit inside the changelog. | ||
- **Contexts**: Tags that let you control which changes apply in which environment. | ||
- **Folder Structure**: Changelogs and changesets can be organized by module, environment, or release. | ||
|
||
## 1. Initialize Your Changelog in Git | ||
### Option A – Generate from Existing Database | ||
If you do not have your schema tracked in Git yet: Use the Harness UI to generate an initial `.yml` changelog from your live database schema. Learn More about [Generate Changelog](https://developer.harness.io/docs/database-devops/use-database-devops/get-started/build-a-changelog/#setup-changelog) feature in Harness. Then commit the generated changelog to your Git repository. | ||
|
||
### Option B – Use an Existing Files | ||
If you already have your changelog files: | ||
- Organize them following your chosen Harness DB DevOps repository structure (for example, one root changelog.yml including multiple changeset files). | ||
- Commit your current schema state to Git. | ||
|
||
## 2. Commit and Push Changes | ||
In trunk-based development, database changes are committed directly to the shared main branch or short-lived branches that are merged the same day. | ||
1. Create or update the relevant .yml changeset in your changelog. | ||
2. Use descriptive commit messages: | ||
```bash | ||
git add . | ||
git commit -m "db: add user table and related indexes" | ||
git push origin main | ||
``` | ||
3. If your team enforces pull requests, open a PR against main and ensure it is reviewed and merged within hours, not days. | ||
4. Keep branches small and focused — ideally containing a single logical change. | ||
|
||
## 3. Deploy Changes via Harness Pipelines | ||
Harness executes your .yml changelogs in sequence during pipeline runs, ensuring every environment stays consistent. The pipeline automatically checks your Git repository for unapplied changesets and executes only the new ones, maintaining idempotency. | ||
How it works: | ||
|
||
- **Contexts**: Instead of maintaining separate changelog files for each environment, you can annotate changesets with contexts (e.g., dev, staging, prod). | ||
- **Selective Deployment**: During pipeline execution, Harness applies only the changesets whose contexts match the target environment stage in the pipeline. | ||
- **Rollback Support**: Pipelines can invoke rollback commands automatically if a deployment fails, reverting to the last known good state. | ||
|
||
For example, | ||
```yaml | ||
databaseChangeLog: | ||
- changeSet: | ||
id: 001-create-users-table | ||
author: john-doe | ||
context: dev,staging | ||
changes: | ||
- createTable: | ||
tableName: users | ||
columns: | ||
- column: | ||
name: id | ||
type: int | ||
constraints: | ||
primaryKey: true | ||
- column: | ||
name: name | ||
type: varchar(255) | ||
``` | ||
In this example: | ||
- When deploying to dev or staging, the changeset is applied. | ||
- When deploying to prod, it is skipped until explicitly marked for that context. | ||
::: note important | ||
Keep contexts environment-specific for controlled rollouts, but avoid creating too many context tags to prevent complexity. | ||
::: | ||
|
||
## Best Practices | ||
|
||
* **One Logical Change per Changeset** – Keep each changeset focused on a single atomic change for easier rollbacks. | ||
* **Consistent Naming Conventions** – Use descriptive IDs and filenames (e.g., `2025-08-12-add-users-table.yml`). | ||
* **Never Edit Applied Changesets** – Create a new changeset for any modification to maintain audit history. | ||
* **Use Liquibase compatible Contexts for Environment Targeting** – Tag changesets (e.g., `dev`, `staging`, `prod`) to control execution scope. | ||
* **Keep Contexts Simple** – Limit to environment-level tags unless there’s a strong reason for finer granularity. | ||
* **Align with Git Strategy** – Follow trunk-based or release-branch workflows for schema changes. | ||
* **Commit Early and Test Often** – Validate in lower environments before promoting upstream. | ||
|
||
**Context Tagging Patterns** | ||
|
||
| Context Tag | Purpose | Example Usage | | ||
| ----------- | ---------------------------- | -------------------------- | | ||
| `dev` | Development environment only | Early feature testing | | ||
| `staging` | Pre-production validation | QA and integration testing | | ||
| `prod` | Production environment only | Final approved changes | | ||
| `common` | Runs in all environments | Core schema objects | |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
context support syntax specifying lists of contexts, and nots (basically full boolean logic). it might be worth having a doc on how to do that, and linking to it here.