Database CI/CD and Schema Migration with MySQL and GitHub

Estimated: 30 mins
Database CI/CD and Schema Migration with MySQL and GitHub

A series of articles about Database CI/CD and Schema Migration with MySQL


Overview

In the last article Database CI/CD and Schema Migration with MySQL, you have tried UI workflow in Bytebase.

This tutorial will bring your MySQL schema change 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.

Prerequisites

Before you start this tutorial, make sure you have the following ready:

Step 1 - Run Bytebase in Docker and set the External URL generated by ngrok

ngrok is a reverse proxy tunnel, and in our case, we need it for a public network address in order to receive webhooks from VCS. ngrok we used here is for demonstration purposes. For production use, we recommend using Caddy.

ngrok-reverse-proxy

  1. Login to ngrok Dashboard and follow its Getting Started steps to install and configure.

  2. Run ngrok:

    ngrok http 8080

    and obtain the public URL https://b725-103-197-71-76.ap.ngrok.io: terminal-ngrok

  3. Run Bytebase in Docker with the following command:

    docker run --rm --init \
      --name bytebase \
      --publish 8080:8080 \
      --volume ~/.bytebase/data:/var/opt/bytebase \
      bytebase/bytebase:2.14.1
  4. Bytebase is running successfully in Docker, and you can visit it via localhost:8080. Register an admin account and it will be granted the workspace admin role automatically.

  5. Click the gear icon (Settings) on the top right. Click General under Workspace. Paste https://b725-103-197-71-76.ap.ngrok.io as External URL under Network section and click Update.

    external-url

  6. Bytebase is running successfully in Docker, and you can visit it via https://b725-103-197-71-76.ap.ngrok.io.

Step 2 - Find your MySQL in Bytebase

  1. Visit Bytebase Console through the browser via your ngrok URL. Log in using your account created from the previous tutorial. login

  2. If you followed the previous tutorial, you should see that the project and database created are still in your workspace. home

Step 3 - Connect Bytebase with GitHub.com

  1. Go to Settings and choose Version Control from the left sidebar. vc-settings-1

  2. Choose GitHub.com and Click Next.

  3. In this tutorial, we will need your GitHub personal account instead of an organization account STEP 2 - OAuth application info. The configuration is similar.

  4. Go to your GitHub account's Settings page.

  5. Click Developer Settings at the bottom of the left sidebar. Click OAuth Apps, and add a New OAuth App. github-oauth-apps

  6. Fill Application name and then copy the Homepage and Authorization callback URL in Bytebase and fill them in. Click Register application. vc-settings-2 github-new-oauth

  7. After the OAuth application is created, click Generate a new client secret. Copy the Client ID and the newly generated Client Secret, paste them back into Bytebase's Application ID and Secret. github-client-id-secrets bb-app-id-secrets

  8. Click Next. You will be redirected to the confirmation page. Click Confirm and add, and the Git provider is successfully added. settings-vc-git-provider-added

Step 4 - Enable GitOps workflow with MySQL

  1. Go to your project, click Version Control, and choose GitOps Workflow. Click Configure GitOps. enable-gitops

  2. Choose GitHub.com - the provider you just added. It will display all the repositories you can manipulate. Choose the one you'd like to make changes to. For the sake of this tutorial, let's keep the rest of the settings default, and click Finish. gitops-enabled

Step 5 - Change schema for MySQL by pushing SQL schema change files to GitHub

  1. In your GitHub repository MySQL-test-bb-local, create a folder bytebase, and create a SQL file using the naming convention {{ENV_ID}}/{{DB_NAME}}##{{VERSION}}##{{TYPE}}##{{DESCRIPTION}}.sql. It is the default configuration for the file path template setting in the last step. The full file path is bytebase/test/uni##202302071000##ddl##create_table.sql:
  • test corresponds to {{ENV_ID}}
  • uni corresponds to {{DB_NAME}}
  • 202302071000 corresponds to {{VERSION}}
  • ddl corresponds to {{TYPE}}
  • create_table corresponds to {{DESCRIPTION}}

Let's create a table subject.

CREATE TABLE subject
(
   id BIGINT NOT NULL,
   course VARCHAR(255)
);

create-table

  1. Commit and push this file.

  2. Go to your project in Bytebase. You’ll find there is a push event and an auto-created issue [uni] Alter schema.

bb-push-notification-only

  1. Go to the issue page, you’ll see:
  • The issue is created via GitHub.
  • The issue is completed without manual approval because it applies the schema change to a database from the Test environment, which doesn't require manual approval.
  • The SQL is exactly the one we have committed to the GitHub repository.
  • The Assignee is Bytebase, because it’s automatic. If the github user you use to commit the change has the same email address found in the bytebase member list, we will use that member as the assignee.

issue-alter-schema

  1. Click View change, you can see the differences. diff

  2. Go to your GitHub repository, you will see besides your committed SQL, there is a .uni##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. latest-schema

  3. Let’s create another SQL file uni##202302072000##ddl##add_lecturer.sql to see how that latest schema file will be updated after applying a new schema change. Paste the SQL script in it.

ALTER TABLE subject ADD lecturer VARCHAR(255);
  1. After pushing and committing the new SQL file, go back to Bytebase and you should find another newly generated issue. issue-add-column

  2. Click View change and see the difference. diff-columns

  3. Go back to your GitHub repository and you will find the LATEST SQL has been updated to reflect the latest schema. latest-schema-lecturer

Summary and What's Next

Now that you have tried the GitOps workflow, which stores your MySQL schema in GitHub and trigger the change upon committing change to the repository, to bring your MySQL change workflow to the next level of Database DevOps - Database as Code.

You can check out GitOps docs to learn more configuration details.

In the real world, you might have separated feature and main branches corresponding to your development and production environment, you can check out GitOps with Feature Branch Workflow to learn the setup. Have a try and look forward to your feedback!

Edit this page on GitHub

Subscribe to Newsletter

By subscribing, you agree with Bytebase's Terms of Service and Privacy Policy.