Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[yugabyte] SQL Migrations #1138

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

barroco
Copy link
Contributor

@barroco barroco commented Dec 2, 2024

This PR adds migration scripts for yugabyte and adapts the migration CLI accordingly.

Migration

Yugabyte does not support the exact same data types and declaration syntax as CockroachDB, therefore the following adaptations have been made:

Data Types (CockroachDB -> Yugabyte):

  1. INT64 -> BIGINT
  2. STRING -> TEXT

Indexes:

  1. Inverted indexes are implemented using ybgin

Syntax required changes:

  1. INDEX can't be declared in the CREATE TABLE block.
  2. Keyword STORING is replaced by INCLUDE.
  3. Index names must be unique.

Additional notes

  • RID v4.0.0 switch database. This is not possible with Yugabyte and therefore, this migration step is executed directly in the code.
  • Since we want to make sure

CI and Tests

  • A job in the CI has been added to cover changes dedicated to Yugabyte migration.
  • Migrations up and down have been successfully tested locally.

@barroco barroco marked this pull request as ready for review December 3, 2024 10:52
Copy link
Contributor

@mickmis mickmis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review of everything except SQL migrations: LGTM, only minor comments for clarity / control flow.

Question about the SQL migrations: Since with yugabyte DBs we will be starting from scratch anyway, we may have an opportunity here to simplify the definitions by consolidating them into a single migration. Have you considered that?

Comment on lines +91 to +92
isCockroach = ds.Version.Type == datastore.CockroachDB
isYugabyte = ds.Version.Type == datastore.Yugabyte
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return error if !isCockroach && !isYugabyte as sanity check?

Comment on lines +120 to +127
// Reconnect to proper database (Yugabyte does not support cross-database references)
connectParameters = crdbflags.ConnectParameters()
connectParameters.ApplicationName = "db-manager"
connectParameters.DBName = dbName
ds, err = datastore.Dial(ctx, connectParameters)
if err != nil {
return fmt.Errorf("failed to reconnect to database %s: %w", dbName, err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factor this away in a function? (duplicated from above)

@@ -157,25 +173,37 @@ func migrate(cmd *cobra.Command, _ []string) error {

// Ensure SQL session has implicit transactions disabled for CRDB versions 22.2+
sessionConfigurationSQL := ""
if ds.Version.SemVer.Compare(*semver.New("22.2.0")) >= 0 {
if isCockroach && ds.Version.SemVer.Compare(*semver.New("22.2.0")) >= 0 {
sessionConfigurationSQL = "SET enable_implicit_transaction_for_batch_statements = false;\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this inside the below block if isCockroach out of clarity?

Comment on lines +183 to +189
} else {
ds, err = datastore.Dial(ctx, connectParameters)
if err != nil {
return fmt.Errorf("failed to reconnect to database %s: %w", dbName, err)
}
migrationSQL = sessionConfigurationSQL + string(rawMigrationSQL)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While functionally equivalent, shouldn't this be inside a if isYugabyte block to be conceptually correct? IIUC this is required for yugabyte to reconnect to a different DB, this is not required because 'the DB is not cockroach'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants