Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
DevOps for Databases

You're reading from   DevOps for Databases A practical guide to applying DevOps best practices to data-persistent technologies

Arrow left icon
Product type Paperback
Published in Dec 2023
Publisher Packt
ISBN-13 9781837637300
Length 446 pages
Edition 1st Edition
Concepts
Arrow right icon
Author (1):
Arrow left icon
David Jambor David Jambor
Author Profile Icon David Jambor
David Jambor
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Preface 1. Part 1: Database DevOps
2. Chapter 1: Data at Scale with DevOps FREE CHAPTER 3. Chapter 2: Large-Scale Data-Persistent Systems 4. Chapter 3: DBAs in the World of DevOps 5. Part 2: Persisting Data in the Cloud
6. Chapter 4: Cloud Migration and Modern Data(base) Evolution 7. Chapter 5: RDBMS with DevOps 8. Chapter 6: Non-Relational DMSs with DevOps 9. Chapter 7: AI, ML, and Big Data 10. Part 3: The Right Tool for the Job
11. Chapter 8: Zero-Touch Operations 12. Chapter 9: Design and Implementation 13. Chapter 10: Database Automation 14. Part 4: Build and Operate
15. Chapter 11: End-to-End Ownership Model – a Theoretical Case Study 16. Chapter 12: Immutable and Idempotent Logic – A Theoretical Case Study 17. Chapter 13: Operators and Self-Healing Data Persistent Systems 18. Chapter 14: Bringing Them Together 19. Part 5: The Future of Data
20. Chapter 15: Specializing in Data 21. Chapter 16: The Exciting New World of Data 22. Index 23. Other Books You May Enjoy

Database version control and CI/CD

As the digital realm evolves, the significance of cohesive workflows becomes ever more apparent. The intersection of software development and databases brings forth challenges, requiring meticulous management. Beyond the realms of managing lines of code lies the vast and intricate world of databases. A slight alteration in the structure can set off a domino effect, impacting an entire application. To ensure the integrity and efficiency of this domain, the principles of version control, a mainstay in software development, are increasingly being applied to databases. Delve into this section to understand the essence of database version control and witness its practical implementation using tools such as Liquibase.

Importance of database version control

Version control systems are fundamental to modern software development, providing a way to track changes, manage code, and coordinate work among multiple developers. However, it’s not just source code that can benefit from version control; database schemas and changes can also be version controlled, offering similar advantages.

Database version control is crucial for several reasons:

  • Synchronization: It ensures that everyone is working with the same database structure, reducing inconsistencies and bugs.
  • Traceability: It keeps a historical record of all changes, allowing developers to understand why a particular change was made and when.
  • Coordination: It helps multiple developers work on the same database without overwriting each other’s changes.
  • Deployment: It makes it easier to manage deployments and roll back changes if something goes wrong. You can recreate the exact state of the database at any point in time.
  • Compliance: In some cases, database version control can help meet compliance requirements by providing an audit trail of changes.

Despite its importance, database version control can be challenging to implement because databases are stateful and because changes can affect existing data. Fortunately, tools such as Liquibase can help manage database changes and provide version control-like capabilities for databases.

Practical example – using Liquibase to manage database schema changes

Liquibase is an open source tool that helps manage database schema changes. It works by applying a series of changesets to a database, which are stored in XML, YAML, JSON, or SQL files. Each changeset contains a change to be made to the database and is identified by a unique ID.

Here’s a step-by-step guide to setting up and using Liquibase:

  1. Install Liquibase: Download the Liquibase installer from the official website and follow the installation instructions for your operating system.
  2. Create a database: Before you can use Liquibase, you need a database. This example assumes you have a MySQL database named mydatabase running on localhost with the username root and password password.
  3. Create a Liquibase project: A Liquibase project is simply a directory that contains all your changeset files. You can organize your changesets in any way you want, but a common approach is to create a separate directory for each version of your application, such as the following example:

BASH

   mkdir -p ~/myproject/1.0.0
   cd ~/myproject/1.0.0
  1. Create a changeset: A changeset is a file that describes a change to the database. For example, to create a table, you might create a changeset like this:

XML

   <?xml version="1.0" encoding="UTF-8"?>
   <databaseChangeLog
     xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
             http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
     <changeSet id="1" author="bob">
       <createTable tableName="person">
         <column name="id" type="int">
           <constraints primaryKey="true" nullable="false"/>
         </column>
         <column name="firstname" type="varchar(50)">
           <constraints nullable="false"/>
         </column>
         <column name="lastname" type="varchar(50)">
           <constraints nullable="false"/>
         </column>
       </createTable>
     </changeSet>
   </databaseChangeLog>

Save this file as 1.0.0.xml in your 1.0.0 directory.

  1. Run the changeset: To apply the changeset to your database, run the following command:

BASH

  liquibase --driver=com.mysql.cj.jdbc.Driver \
          --classpath=/path/to/mysql-connector-java-8.0.19.jar \
          --url="jdbc:mysql://localhost/mydatabase" \
          --changeLogFile=1.0.0.xml \
          --username=root \
          --password=password \
          update

Replace /path/to/mysql-connector-java-8.0.19.jar with the path to your MySQL JDBC driver.

  1. Create more changesets: As your application evolves, you’ll need to make more changes to your database. For each change, create a new changeset file in the appropriate directory, incrementing the changeset ID.
  2. Roll back changes: If something goes wrong, you can use Liquibase to roll back changes. For example, to roll back the last change, run the following:

BASH

  liquibase --driver=com.mysql.cj.jdbc.Driver \
          --classpath=/path/to/mysql-connector-java-8.0.19.jar \
          --url=”jdbc:mysql://localhost/mydatabase” \
          --changeLogFile=1.0.0.xml \
          --username=root \
          --password=password \
          rollbackCount 1

Liquibase provides a robust, flexible way to manage database schema changes and enables database version control. It’s a valuable tool in the DevOps DBA’s toolkit, enabling you to manage databases in the same systematic, controlled way you manage source code.

Role of the DevOps DBA in CI/CD pipelines

The role of the DevOps DBA in CI/CD pipelines is to ensure that database changes are seamlessly integrated and deployed as part of the software release process. The DevOps DBA collaborates with development, operations, and release management teams to create an automated, efficient, and error-free release pipeline that includes database elements.

Key responsibilities of the DevOps DBA in CI/CD pipelines include the following:

  • Schema management: Managing database schema changes and ensuring they are version-controlled, tested, and deployed in sync with application code
  • Automated migrations: Automating database migrations to ensure that schema changes and data updates are applied correctly and consistently across environments
  • Performance testing: Ensuring that database changes do not degrade performance by incorporating database performance tests into the CI/CD pipeline
  • Security: Ensuring that database changes comply with security best practices and that sensitive data is protected in all environments
  • Disaster recovery and backups: Making sure that backups are taken before deployments and that there’s a plan in place for quick recovery in case of failure
  • Monitoring and alerts: Implementing monitoring tools to check the health of the database as changes are deployed, and setting up alerts for any issues
  • Coordination and communication: Coordinating with various stakeholders involved in the release process to ensure database changes are reviewed and approved before deployment

Practical example – Jenkins pipeline with database migrations using Flyway

Flyway is an open source database migration tool that makes it easy to version control and migrate your database schema. Jenkins is an automation server used for implementing continuous integration and delivery pipelines. The following is an in-depth walk-through of setting up a Jenkins pipeline that includes database migrations using Flyway:

  1. Prerequisites: Before you start, you will need to have Jenkins and Flyway installed, and a database (such as MySQL) that you want to run migrations against.
  2. Set up Flyway configuration: Create a Flyway configuration file named flyway.conf with your database connection details:
       flyway.url=jdbc:mysql://localhost:3306/mydatabase
       flyway.user=myuser
       flyway.password=mypassword

    Also, create a directory named sql to store your SQL migration scripts.

  3. Create a Jenkins pipeline: In Jenkins, create a new pipeline. You can do this by selecting New Item from the dashboard, then choosing the Pipeline option.
  4. Configure the pipeline: In the pipeline configuration page, scroll down to the Pipeline section. You’ll be entering a script here that defines your pipeline.
  5. Write the pipeline script: In the Pipeline section, choose Pipeline script and enter a script that defines your pipeline. The following is an example script:

GROOVY

   pipeline {
       agent any
       environment {
           FLYWAY_HOME = '/path/to/flyway'
       }
       stages {
           stage('Checkout Code') {
               steps {
                   // Checkout code from your repository
                   git 'https://github.com/your-repo.git'
               }
           }
           stage('Database Migration') {
               steps {
                   script {
                       // Run Flyway migrations
                       sh "${FLYWAY_HOME}/flyway -configFiles=flyway.conf migrate"
                   }
               }
           }
           stage('Build') {
               steps {
                   // Your build steps go here
               }
           }
           stage('Deploy') {
               steps {
                   // Your deployment steps go here
               }
           }
       }
   }

This script defines a pipeline with four stages:

  • Checkout Code: This stage checks out the code from your repository. Replace the URL with the URL of your repository.
  • Database Migration: This stage runs Flyway migrations against your database.
  • Build: This builds your application. Replace the comment with the actual steps for your build process.
  • Deploy: This deploys your application. Replace the comment with the actual steps for your deployment process.
  1. Run the pipeline: Save the pipeline and run it. You can do this by clicking Build Now on the pipeline page.

This Jenkins pipeline allows for the seamless integration of database migrations into the CI/CD process. When the pipeline is run, Flyway applies any pending migrations to the database, ensuring the database schema is up to date and in sync with the application code.

In conclusion, as a DevOps DBA, working with CI/CD pipelines allows for a smooth, automated, and efficient process, managing database schema changes, automated migrations, and ensuring database performance, security, and disaster recovery in alignment with the application’s release process. This elevates the role of the DBA from a background role to a critical part of the development, deployment, and release life cycle.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image