2.4 How to add a database migration

Similar to configuration, from time to time, the GoCD database needs to be migrated as well, to accommodate new features or other changes. GoCD uses DBDeploy to perform this operation.

Explaining by example, I'll take the case of how the database migration 1701001 was added

2.4.1 Introducing a new database migration

~/projects/go$ ./gradlew server:createDbMigration -PmigrationName=remove_gadget_oauth_tables -q
Creating new migration:
    ~/projects/go/server/db/migrate/h2deltas/1701001_remove_gadget_oauth_tables.sql

The above file will create a file with the following convention:

<2-digit-major-release-version><2-digit-minor-release-version><3-digit-migration-sequence-number>_<description-of-the-migration>.sql

For example, for the first migration of 17.1 release, the migration was

~/projects/go$ touch ./server/db/migrate/h2deltas/1701001_remove_gadget_oauth_tables.sql
  • 2-digit-major-release-version : 17
  • 2-digit-minor-release-version : 01
  • 3-digit-migration-sequence-number: 001
  • description-of-the-migration: remove_gadget_oauth_tables

2.4.2 Writing the DML/DDL

Following the DBDeploy semantics, the 1701001 migration was created as


DROP TABLE IF EXISTS gadgetOauthAuthorizationCodes;
...

--//@UNDO

CREATE TABLE gadgetOauthClients (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
  oauthAuthorizeUrl VARCHAR(255) UNIQUE NOT NULL,
  clientId VARCHAR(255) NOT NULL,
  clientSecret VARCHAR(255) NOT NULL,
  serviceName VARCHAR(255) UNIQUE NOT NULL,
);

If you now run

./gradlew prepare

and then bring up a Development Server, you will see that the migration runs.

results matching ""

    No results matching ""