* revert changes * revert changes * revert changes * revert changes * revert changes * Prepare release * Add pull request to release notes * Automated Commit - Formatting Changes --------- Co-authored-by: jdpgrailsdev <jdpgrailsdev@users.noreply.github.com>
How to Create a New Database
Check io.airbyte.db.instance.configs for example.
Database Instance
- Create a new package under
io.airbyte.db.instancewith the name of the database. - Create the database schema enum that defines all tables in the database.
- Write a SQL script that initializes the database.
- The default path for this file is
resource/<db-name>_database/schema.sql.
- The default path for this file is
- Implement the
DatabaseInstanceinterface that extends fromBaseDatabaseInstance. This class initializes the database by executing the initialization script.
Database Migration
- Implement the
DatabaseMigratorinterface that extends fromBaseDatabaseMigrator. This class will handle the database migration. - Create a new package
migrationsunder the database package. Put all migrations files there. - Add the migration commands in
build.gradlefor the new database.- The three commands are
new<db-name>Migration,run<db-name>Migration, anddump<db-name>Schema.
- The three commands are
jOOQ Code Generation
- To setup jOOQ code generation for the new database, refer to
airbyte-db/jooqfor details. - Please do not use any jOOQ generated code in this
libmodule. This is because thejooqmodule that generates the code depends on this one.
How to Write a Migration
- Run the
newMigrationcommand to create a new migration file inio.airbyte.db.instance.<db-name>.migrations.- Configs database:
./gradlew :airbyte-db:db-lib:newConfigsMigration. - Jobs database:
./gradlew :airbyte-db:db-lib:newJobsMigration.
- Configs database:
- Write the migration using
jOOQ. - Use the
runMigrationcommand to apply your newly written migration if you want to test it.- Configs database:
./gradlew :airbyte-db:db-lib:runConfigsMigration. - Jobs database:
./gradlew :airbyte-db:db-lib:runJobsMigration.
- Configs database:
- Run the
dumpSchemacommand to update the database schema.- Configs database:
./gradlew :airbyte-db:db-lib:dumpConfigsSchema - Jobs database:
./gradlew :airbyte-db:db-lib:dumpJobsSchema
- Configs database:
Migration Filename
- The name of the file should follow this pattern:
V(version)__(migration_description_in_snake_case).java. - This pattern is mandatory for Flyway to correctly locate and sort the migrations.
- The first part is
V, which denotes for versioned migration. - The second part is a version string with this pattern:
<major>_<minor>_<patch>_<id>.- The
major,minor, andpatchshould match that of the Airbyte version. - The
idshould start from001for each<major>_<minor>_<patch>combination. - Example version:
0_29_9_001
- The
- The third part is a double underscore separator
__. - The fourth part is a brief description in snake case. Only the first letter should be capitalized for consistency.
- See original Flyway documentation for more details.
Sample Migration File
/**
* This migration add an "active" column to the "airbyte_configs" table.
* This column is nullable, and default to {@code true}.
*/
public class V0_29_9_001__Add_active_column extends BaseJavaMigration {
@Override
public void migrate(Context context) throws Exception {
DSL.using(context.getConnection()).alterTable("airbyte_configs")
.addColumn(field("active", SQLDataType.BOOLEAN.defaultValue(true).nullable(true)))
.execute();
}
}
How to Run a Migration
- Automatic. Migrations will be run automatically in the server. If you prefer to manually run the migration, change
RUN_DATABASE_MIGRATION_ON_STARTUPtofalsein.env. - API. Call
api/v1/db_migrations/listto retrieve the current migration status, and callapi/v1/db_migrations/migrateto run the migrations. Check the API documentation for more details.
Schema Dump
- The database schema is checked in to the codebase to ensure that we don't accidentally make any schema change.
- The schema dump can be done manually and automatically.
- To dump the schema manually, run the
dumpSchemacommand, as mentioned above. - The
<db-name>DatabaseMigratorTestdumps the schema automatically for each database. Please remember to check in any change in the schema dump.