Database Change Management with ClickHouse and GitHub
Tags: clickhouse, github, devops, cicd
Published: 2023-03-03 09:48:54

This is a series of articles about Database Change Management with ClickHouse

- [Database Change Management with ClickHouse](https://dev.to/adela_bytebase/database-change-management-with-clickhouse-3aob)
- Database Change Management with ClickHouse and GitHub (this one)

---

In the last article [Database Change Management with ClickHouse](https://dev.to/adela_bytebase/database-change-management-with-clickhouse-3aob), you have tried UI workflow in Bytebase.

This tutorial will bring you to the next level by introducing the GitOps workflow, where you commit the schema change script to the GitHub repository, which will in turn trigger the schema deployment pipeline in Bytebase.

You can use Bytebase free version to finish the tutorial.

## Features included

- GitOps Workflow
- Change History

## Prerequisites

Before you start this tutorial, make sure:

- You have followed our previous UI-based change tutorial [Database Change Management with ClickHouse](https://dev.to/adela_bytebase/database-change-management-with-clickhouse-3aob).
- You have a [ClickHouse Cloud](https://clickhouse.cloud/) account or a ClickHouse self-managed instance running.
- You have a [GitHub](https://github.com/) account.
- You have a public GitHub repository, e.g  `clickhouse-test-bb-local`.
- You have [Docker](https://www.docker.com/) installed locally.
- You have a [ngrok](http://ngrok.com) account. ngrok is a reverse proxy tunnel, and in our case, we need it for a public network address in order to receive webhooks from GitHub.com. We use ngrok here for demonstration purposes. For production use, we recommend using [Caddy](https://caddyserver.com/).
![ngrok](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/ngrok.webp)

## Step 1 - Run Bytebase in Docker with URL generated by ngrok

To make local-running Bytebase visible to GitHub, we’ll pass ngrok generated URL to [--external-url](https://www.bytebase.com/docs/get-started/install/external-url).

1. Login to [ngrok Dashboard](https://dashboard.ngrok.com/) and follow its [Getting Started](https://dashboard.ngrok.com/get-started/setup) steps to install and configure.

2. Run
```bash
ngrok http 5678
```
and obtain the public URL:
![terminal-ngrok](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/terminal-ngrok.webp)

3. Make sure your Docker daemon is running, if it’s running Bytebase container for [the previous tutorial](/blog/database-change-management-with-clickhouse), stop and remove it. The data created in the last tutorial is stored under `~/.bytebase/data` by default and will be restored if the system restarts.
   
4. Start the Bytebase Docker container by typing the following command in the terminal. Pay attention to the last parameter `--external-url https://b16b-103-197-71-76.ap.ngrok.io`, which is generated by ngrok.
```bash
docker run --init \
--name bytebase \
--platform linux/amd64 \
--restart always \
--publish 5678:8080 \
--health-cmd "curl --fail http://localhost:5678/healthz || exit 1" \
--health-interval 5m \
--health-timeout 60s \
--volume ~/.bytebase/data:/var/opt/bytebase \
bytebase/bytebase:1.13.0 \
--data /var/opt/bytebase \
--port 8080 \
--external-url https://b16b-103-197-71-76.ap.ngrok.io
````

5. Bytebase is running successfully in Docker, and you can visit it via `https://b16b-103-197-71-76.ap.ngrok.io`.
![docker](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/docker.webp)

## Step 2 - Find your ClickHouse instance in Bytebase

1. Visit `https://b16b-103-197-71-76.ap.ngrok.io` in your browser, and log in using your admin account created from the previous article.
![bb-login](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-login.webp)

2. If you have followed the last article, you should have a project `TestClickHouse` and a database `db_demo`.
![bb-home](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-home.webp)

## Step 3 - Connect Bytebase with GitHub.com

1. Click **Settings** on the top bar, and then click **Workspace** > **GitOps**. Choose **GitHub.com** and click **Next**.
![bb-gitops-github-step1](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-gitops-github-step1.webp)

2. Follow the instructions within **STEP 2**, and in this tutorial, we will use a personal account instead of an organization account. The configuration is similar.

3. Go to your GitHub account. Click **Settings** on the dropdown menu.
![gh-settings-dropdown](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/gh-settings-dropdown.webp)

4. Click **Developer Settings** at the bottom of the left side bar. Click **OAuth Apps**, and click **New OAuth App**.
![gh-oauth-apps](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/gh-oauth-apps.webp)

5. Fill **Application name** and then copy the **Homepage** and **Authorization callback URL** in Bytebase and fill them. Click **Register application**.
![gh-register-oauth](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/gh-register-oauth.webp)

6. After the OAuth application is created successfully. Click **Generate a new client secret**. Copy **Client ID** and this newly generated client secret and paste them back in Bytebase.
![gh-copy-client-id](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/gh-copy-client-id.webp)
![bb-gitops-github-step2](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-gitops-github-step2.webp)
![gh-auth](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/gh-auth.webp)

7. Click **Next**. You will be redirected to the confirmation page. Click **Confirm and add**, and the Git provider is successfully added.
![bb-gitops-github-step3](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-gitops-github-step3.webp)

## Step 4 - Enable GitOps workflow with ClickHouse
1. Go to project `TestClickHouse`, click **GitOps**, and choose **GitOps Workflow**. Click **Configure GitOps**.
![bb-project-gitops-gitops-workflow](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-project-gitops-gitops-workflow.webp)

2. Choose `GitHub.com` - the provider you just added. It will display all the repositories you can manipulate. Choose `clickhouse-test-bb-local`.
![bb-project-gitops-github](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-project-gitops-github.webp)
![bb-project-gitops-github-repo](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-project-gitops-github-repo.webp)

3. Keep the default setting, and click **Finish**.

## Step 5 - Change schema for ClickHouse by pushing SQL schema change files to GitHub
1. In your GitHub repository `clickhouse-test-bb-local`, create a folder `bytebase`, then create a subfolder `Test`, and create an sql file following the pattern 
```
{{ENV_NAME}}/{{DB_NAME}}##{{VERSION}}##{{TYPE}}##{{DESCRIPTION}}.sql
```
It is the default configuration for file path template setting under project GitOps.
   
   `db_demo##202303020000##ddl##create_t2.sql`
   - `Test` corresponds to {{ENV_NAME}}
   - `db_demo` corresponds to {{DB_NAME}}
   - `202303020000` corresponds to {{VERSION}}
   - `ddl` corresponds to {{TYPE}}
   - `create_t2` corresponds to {{DESCRIPTION}}
  
   Paste the sql script in it.

```
CREATE TABLE
 t2 (id UInt64, name String) ENGINE = MergeTree
ORDER BY id;

```
![vsc-test-sql](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/vsc-test-sql.webp)

2. Commit and push this file.
3. Go to Bytebase, and go into project `TestClickHouse`. You’ll find there is a new `Push Event` and a new `issue 106` created.
![bb-project-push](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-project-push.webp)

4. Click `issue/106` and go the issue page. Click **Resolve issue**, and the issue will be `Done`. You’ll see
   - The issue is created via GitHub.com
   - The issue is executed without approval because it’s on `Test` environment where manual approval is skipped by default. The Assignee is `Bytebase`, because the execution is automatic, and requires no manual approval.
   - The SQL is exactly the one we have committed to the GitHub repository.
   - The Creator is `A`, because the GitHub user you use to commit the change has the same email address found in the Bytebase member list.

![bb-issue-create-t2-done](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-issue-create-t2-done.webp)

5. Click **View change**, and you could view the schema diff.
![bb-db-change-diff](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/bb-db-change-diff.webp)

6. Go to GitHub repository, and you will see besides your committed sql, there is a `.db_demo##LATEST.sql` file. Because you have configured `Schema path template` before, Bytebase will write back the latest schema to that specified path after completing the schema change. Thus you have access to an update-to-date full schema at any time.
![gh-LATEST](https://www.bytebase.com/static/blog/database-change-management-with-clickhouse-and-github/gh-LATEST.webp)


## Summary and Next

Now you have tried out GitOps workflow, which will store your ClickHouse schema in GitHub and trigger the change upon committing the change to the repository, to bring your ClickHouse change workflow to the next level of Database DevOps - [Database as Code](https://www.bytebase.com/blog/database-as-code).

In real world scenario, you might have separate features and main branches corresponding to your dev and production environment, you can check out [GitOps with Feature Branch Workflow](https://www.bytebase.com/docs/how-to/workflow/gitops-feature-branch) to learn the setup. Have a try and look forward to your feedback!