ПІДТРИМАЙ УКРАЇНУ ПІДТРИМАТИ АРМІЮ
Uk Uk

Database Change Management with ClickHouse

Database Change Management with ClickHouse

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

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

ClickHouse is a fast open-source column-oriented database management system that allows generating analytical data reports in real-time using SQL queries.

This tutorial will guide you step-by-step to set up database change management for ClickHouse (Cloud and self-managed) in Bytebase. With Bytebase, a team can have a formalized review and rollout process to make ClickHouse schema change and data change.

You’ll have a GUI and the full change history. You can use Bytebase free version to finish the tutorial.

There is also a bonus section about drift detection for those advanced users if needed.

Features included

  • Change Workflow
  • Change History
  • Drift Detection

Prerequisites

Before you start this tutorial, make sure:

Step 1 - Start Bytebase in Docker

  1. Make sure your docker daemon is running, and start the Bytebase docker container.
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
  1. Bytebase is running successfully in Docker, and you can visit it vialocalhost:5678.

    docker
  2. Visit localhost:5678 in your browser. Register the first admin account which will be granted Workspace Owner .

    register

Step 2 - Add ClickHouse in Bytebase

In Bytebase, ​​an Instance could be your on-premises MySQL instance, an AWS RDS instance etc, in this tutorial, a ClickHouse Cloud account or a self-managed instance.

  1. Visitlocalhost:5678and log in asWorkspace Owner.

    login
  2. ClickAdd Instance.

    bb-add-instance
  3. Add aClickHouse instanceand clickCreate.

Pay attention to some fields:

Environment: chooseTest, if you chooseProd, issues will wait for approval by default. In this tutorial, we try to keep it simple. However, it’s all configurable.

If you use Cloud version. Go to your ClickHouse Cloud account, and clickView connection string.

Host or SocketandPortare in the grey box as parameters.

UsernameandPasswordare generated and stored inclickhouse_credentials.txtwhile you registered the ClickHouse Cloud account.

ch-cloud-view-connection-string
ch-cloud-view-host
ch-cloud-view-password

Be sure to allow access to this service fromAnywhere.

ch-cloud-anywhere

ChooseCA Certificatefor SSL Connection, if you use macOS, open the file/etc/ssl/cert.pem, copy the content as a whole then paste it into theCA Certificatefield box. ClickTest Connectionto verify it’s working.

bb-instance-ch-cloud
  1. If you use self-managed version of ClickHouse, fill in instance form as the following image and clickCreate.
    bb-instance-ch-self-managed

Step 3 - Create a project with ClickHouse instance

In Bytebase,Projectis the container to group logically relatedDatabases,IssuesandUserstogether, which is similar to the project concept in other dev tools such as Jira, GitLab. So before you deal with the database, a project must be created.

  1. After the instance is created, clickProjectson the top bar.

  2. ClickNew Projectto create a new projectTestClickHouse, key isTCH, mode isstandard. ClickCreate.

    bb-projects-new

Step 4 - Create a database in ClickHouse via Bytebase

In Bytebase, aDatabaseis the one created by 'CREATE DATABASE xxx'. A database always belongs to a singleProject.Issuerepresents a specific collaboration activity between Developer and DBA such as creating a database, altering a schema. It's similar to the issue concept in other issue management tools.

  1. After the project is created. ClickNew DBon the project top bar.

    bb-project-new-db
  2. Fill the form withName-db_demo,Environment-Test, andInstance-ClickHouse instance. ClickCreate.

    bb-create-db
  3. It will create an issue “CREATE DATABASE ….” automatically. Because it’s forTestenvironment, the issue will run without waiting for your approval by default. ClickResolve, and the issue isDone. The database is created.

    bb-issue-dbdemo-done
  4. Go back to the home page by clickingHomeon the left sidebar. On the home page, you can see the project, the database, and the issue you just resolved.

    bb-home-dbdemo-done

Step 5 - Create a table in ClickHouse via Bytebase

In Step 4, you created an issue to create a database via UI workflow and then executed it. Let’s try to create another issue to alter that database.

  1. Go to projectTestClickHouse, and clickAlter Schema.

    bb-project-alter-schema
  2. Choosedb_demoand clickNext. It could generate a pipeline if you have different databases for different environments.

    bb-alter-schema-select-db

  3. Input SQL as follows, and clickCreate.

CREATE TABLE
 t1 (id UInt64, name String) ENGINE = MergeTree
ORDER BY id;
  1. Bytebase will do some basic checks and then execute the SQL. Since it’s forTestenvironment, the issue is automatically approved by default. ClickResolve issue, and the issue status will becomeDone.

    bb-issue-create-table-done
  2. On the issue page, clickView change. You will see diff for the change.

    bb-dbdemo-change-diff
  3. You can also go toChange Historyunder the project to view the full history. Or go into a specific database to view its history.

    bb-project-change-history
    bb-db-change-history

Bonus Section - Drift Detect

This section requires you to haveEnterprise Plan(you can start 14 days trial directly in the product without credit card).

Now you can see the full change history ofdb_demo. However, what isEstablish new baseline? When should it be used?

By adopting Bytebase, we expect teams to use Bytebase exclusively for all schema changes. Meanwhile, if someone has made ClickHouse schema change out side of Bytebase, obviously Bytebase won’t know it. And because Bytebase has recorded its own copy of schema, when Bytebase compares that with the live schema having that out-of-band schema change, it will notice a discrepancy and surface a schema drift anomaly. If that change is intended, then you should baseline the schema state again to reconcile.

In this section, you’ll be guided through this process.

  1. Go to ClickHouse Cloud, clickOpen SQL console, and add a columnagethere. Make sure the new column is added.
ALTER TABLE t1 ADD COLUMN age UInt8;
ch-cloud-dbdemo-add-age
  1. Wait for about 10 mins for Bytebase to detect the drift. Go back to Bytebase, and you can find theSchema Drifton:

database db_demo

bb-drift-dbdemo

Anomaly Center

bb-drift-ac
  1. ClickView diff, you will see the exact drift.

    bb-view-drift-diff
  2. Go todb_demo>Change Historyand clickEstablish new baselineto reconcile the schema.

    bb-dbdemo-create-new-baseline
  3. It will create a baseline issue. ClickResolveto mark it done.

    bb-issue-dbdemo-baseline-done
  4. Go back todb_demoor Anomaly Center, and you will find the drift is gone.

    bb-dbdemo-no-drift
    bb-ac-no-drift

Summary and Next

Now you have connected ClickHouse with Bytebase, and tried out the UI workflow to do schema change. Bytebase will record the full change history for you. WithEnterprise Plan, you can even have drift detection.

In the next article, you’ll try 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, akaDatabase DevOps- Database as Code .

Ресурс : dev.to


Scroll to Top