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

Database migration in credhub-release 2.12.94 fails. #813

Closed
3 tasks done
jochenehret opened this issue Nov 11, 2024 · 1 comment
Closed
3 tasks done

Database migration in credhub-release 2.12.94 fails. #813

jochenehret opened this issue Nov 11, 2024 · 1 comment

Comments

@jochenehret
Copy link

What version of the credhub server you are using?
We are trying to upgrade to credhub-release 2.12.94 with MySQL as database.

What version of the credhub cli you are using?
Not relevant, server-side problem.

If you were attempting to accomplish a task, what was it you were attempting to do?
Upgrade cf-deployment from v44.6.0 to v44.7.0. The credhub-release version is upgraded from 2.12.93 to 2.12.94.

What did you expect to happen?
Upgrade of credhub server succeeds.

What was the actual behavior?
Upgrade failed because of a flyway migration error. Output from credhub.log is:

2024-11-08T19:26:29.643Z [main] ....  INFO --- BaseDatabaseType: Database: jdbc:mariadb://sql-db.service.cf.internal:3306/credhub (MySQL 8.0)
2024-11-08T19:26:30.014Z [main] ....  WARN --- FlywayMigrationStrategyConfiguration: Validation failed: "Validate failed: Migrations have failed validation
Migration checksum mismatch for migration version 58
-> Applied to database : -1214956128
-> Resolved locally    : 1764759980. Either revert the changes to the migration, or run repair to update the schema history.
Detected resolved migration not applied to database: 57. To ignore this migration, set -ignoreIgnoredMigrations=true. To allow executing this migration, set -outOfOrder=true.
Need more flexibility with validation rules? Learn more: https://rd.gt/3AbJUZE".
2024-11-08T19:26:30.014Z [main] ....  INFO --- FlywayMigrationStrategyConfiguration: Attempting to repair...
2024-11-08T19:26:30.052Z [main] ....  INFO --- VersionPrinter: Flyway Community Edition 7.15.0 by Redgate
2024-11-08T19:26:30.109Z [main] ....  INFO --- JdbcTableSchemaHistory: Repair of failed migration in Schema History table `credhub`.`flyway_schema_history` not necessary. No failed migration detected.
2024-11-08T19:26:30.154Z [main] ....  INFO --- JdbcTableSchemaHistory: Repairing Schema History table for version 58 (Description: index lowercase credential name, Type: SQL, Checksum: 1764759980)  ...
2024-11-08T19:26:30.159Z [main] ....  INFO --- JdbcTableSchemaHistory: Repairing Schema History table for version 58 (Description: index lowercase credential name, Type: SQL, Checksum: 1764759980)  ...
2024-11-08T19:26:30.171Z [main] ....  INFO --- DbRepair: Successfully repaired schema history table `credhub`.`flyway_schema_history` (execution time 00:00.086s).
2024-11-08T19:26:30.192Z [main] ....  INFO --- FlywayMigrationStrategyConfiguration: Repair succeeded.
2024-11-08T19:26:30.193Z [main] ....  INFO --- FlywayMigrationStrategyConfiguration: Running FlyWay migration....
2024-11-08T19:26:30.214Z [main] ....  INFO --- VersionPrinter: Flyway Community Edition 7.15.0 by Redgate
2024-11-08T19:26:30.331Z [main] .... FATAL --- FlywayMigrationStrategyConfiguration: FlyWay migration failed. Crashing.
2024-11-08T19:26:30.333Z [main] ....  WARN --- AnnotationConfigServletWebServerApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation
Detected resolved migration not applied to database: 57. To ignore this migration, set -ignoreIgnoredMigrations=true. To allow executing this migration, set -outOfOrder=true.
Need more flexibility with validation rules? Learn more: https://rd.gt/3AbJUZE
2024-11-08T19:26:30.334Z [main] ....  INFO --- HikariDataSource: HikariPool-1 - Shutdown initiated...
2024-11-08T19:26:30.360Z [main] ....  INFO --- HikariDataSource: HikariPool-1 - Shutdown completed.
2024-11-08T19:26:30.369Z [main] ....  INFO --- StandardService: Stopping service [Tomcat]
2024-11-08T19:26:30.403Z [main] ....  INFO --- ConditionEvaluationReportLoggingListener:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-11-08T19:26:30.451Z [main] .... ERROR --- SpringApplication: Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation
Detected resolved migration not applied to database: 57. To ignore this migration, set -ignoreIgnoredMigrations=true. To allow executing this migration, set -outOfOrder=true.
Need more flexibility with validation rules? Learn more: https://rd.gt/3AbJUZE
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.39.jar!/:5.3.39]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.39.jar!/:5.3.39]

Last entries in flyway_schema_history table:

+----------------+---------+-------------------------------------------------------------------------------+--------+----------------------------------------------------------------------------------------+-------------+--------------+---------------------+----------------+---------+
| installed_rank | version | description                                                                   | type   | script                                                                                 | checksum    | installed_by | installed_on        | execution_time | success |
+----------------+---------+-------------------------------------------------------------------------------+--------+----------------------------------------------------------------------------------------+-------------+--------------+---------------------+----------------+---------+
(...)
|             72 | 56      | change expiry date in certificate credential                                  | SQL    | V56__change_expiry_date_in_certificate_credential.sql                                  |  -969791477 | credhub      | 2024-03-27 12:31:05 |             59 |       1 |
|             73 | 57.2    | delete related encrypted value when credential is deleted                     | SQL    | V57_2__delete_related_encrypted_value_when_credential_is_deleted.sql                   | -1930144391 | credhub      | 2024-03-27 12:31:05 |             97 |       1 |
|             74 | 57.2    | delete related encrypted value when credential is deleted                     | DELETE | V57_2__delete_related_encrypted_value_when_credential_is_deleted.sql                   | -1930144391 | credhub      | 2024-04-16 08:18:47 |              0 |       1 |
|             75 | 58      | index lowercase credential name                                               | SQL    | V58__remove_triggers.sql                                                               |  1764759980 | credhub      | 2024-04-16 08:18:48 |             83 |       1 |
|             76 | 58      | remove triggers                                                               | DELETE | V58__remove_triggers.sql                                                               | -1214956128 | credhub      | 2024-04-16 09:23:41 |              0 |       1 |
|             77 | 57.2    | delete related encrypted value when credential is deleted                     | SQL    | V57_2__delete_related_encrypted_value_when_credential_is_deleted.sql                   | -1930144391 | credhub      | 2024-04-16 09:23:41 |            105 |       1 |
|             78 | 57.2    | delete related encrypted value when credential is deleted                     | DELETE | V57_2__delete_related_encrypted_value_when_credential_is_deleted.sql                   | -1930144391 | credhub      | 2024-04-16 10:16:39 |              0 |       1 |
|             79 | 58      | index lowercase credential name                                               | SQL    | V58__remove_triggers.sql                                                               |  1764759980 | credhub      | 2024-04-16 10:16:40 |             82 |       1 |
+----------------+---------+-------------------------------------------------------------------------------+--------+----------------------------------------------------------------------------------------+-------------+--------------+---------------------+----------------+---------+

The problem is introduced with PR #803. It adds a new migration file V57__add_lowercase_credential_name_column.sql. However, a previous migration with version V57.2 was executed some time ago. Could this issue be fixed by assigning a higher version number to the add_lowercase_credential_name_column migration file?

Please confirm where necessary:

  • I have included a log output
  • My log includes an error message
  • I have included steps for reproduction

If you are a PCF customer with an Operation Manager (PCF Ops Manager) please direct your questions to support (https://support.pivotal.io/)

peterhaochen47 added a commit that referenced this issue Nov 11, 2024
This reverts commit 23315c7.

reverting for now due to: #813
peterhaochen47 added a commit that referenced this issue Nov 11, 2024
This reverts commit 274d5ce.

reverting for now due to: #813
peterhaochen47 added a commit that referenced this issue Nov 11, 2024
This reverts commit 56a4b42.

reverting for now due to: #813
peterhaochen47 added a commit that referenced this issue Nov 11, 2024
- due to issue #813 (with mysql) we have rolled back all DB related changes:
030302a
873bc6d
34afece
which we previously released.

- since theere is no issue with postgres, it safer to NOT remove
the already-released DB migrations so that various consumers's
postgres DB states do not diverge. So this commit brings back
all those already-release postgres DB migration.
peterhaochen47 added a commit that referenced this issue Nov 12, 2024
- mysql migration v58 failed flyway validation because
there was a prior migration also numbered v58
(082fd5e) which had since
been removed but nevertheless was recorded in flyway
history for those users who have installed credhub versions
containing the old v58, hence creating a conflict with the new v58.
Hence, this commit removes the new v58 for now to fix
the issue for those users.

- leaving v57 alone because there was never a v57 before,
only v57_2, which flyway treats as distinct from v57. See
code history here: https://github.com/cloudfoundry/credhub/commits/main/applications/credhub-api/src/main/resources/db/migration/mysql

- we will try to add back the index (in this removed v58)
later in a safe way.

- also: leaving postgres & H2 migrations intact because
there is not a problem there.
peterhaochen47 added a commit that referenced this issue Nov 12, 2024
- mysql migration v58 failed flyway validation because
there was a prior migration also numbered v58
(082fd5e) which had since
been removed but nevertheless was recorded in flyway
history for those users who have installed credhub versions
containing the old v58, hence creating a conflict with the new v58.
Hence, this commit removes the new v58 for now to fix
the issue for those users.

- leaving v57 alone because there was never a v57 before,
only v57_2, which flyway treats as distinct from v57. See
code history here: https://github.com/cloudfoundry/credhub/commits/main/applications/credhub-api/src/main/resources/db/migration/mysql

- we will try to add back the index (in this removed v58)
later in a safe way.

- also: leaving postgres & H2 migrations intact because
there is not a problem there.
@peterhaochen47
Copy link
Member

Fixed by #815

peterhaochen47 added a commit that referenced this issue Nov 12, 2024
- context: the performance improvement brought by indexing (#803)
was reverted (#815) due to migration error (issue #813) for some users.
This commit brings back that index, but doing so safely to avoid issues
like #813 (#813 happened to some users due to migration file numbering conflict,
this commit uses a fresh migration number (v59) to avoid that).

- Unfortunately, for the users who happened to have installed
credhub v2.12.94 successfully (aka did not encounter #813),
their mysql DB would already have an index called
"credential_name_lowercase". So in order for the migration in this
commit to work for both those who have installed credhub v2.12.94
and those who have not, this commit's migration instead creates
the index under a different name: "credential_name_lowercase_index"
(mysql does not support the "CREATE INDEX...IF NOT EXIST" syntax).
This would create some DB inefficiency (due to the presence of two
functionally identical indexes) for those who have installed credhub v2.12.94
(which we think is a smaller subset of users), but the system
would still work and likely a net improvement compared to before
due to the presence of indexing. And we would instruct these users
to manually drop the old, duplicate index ("credential_name_lowercase").
peterhaochen47 added a commit that referenced this issue Nov 13, 2024
- context: the performance improvement brought by indexing (#803)
was reverted (#815) due to migration error (issue #813) for some users.
This commit brings back that index, but doing so safely to avoid issues
like #813 (#813 happened to some users due to migration file numbering conflict,
this commit uses a fresh migration number (v59) to avoid that).

- Unfortunately, for the users who happened to have installed
credhub v2.12.94 successfully (aka did not encounter #813),
their mysql DB would already have an index called
"credential_name_lowercase". So in order for the migration in this
commit to work for both those who have installed credhub v2.12.94
and those who have not, this commit's migration instead creates
the index under a different name: "credential_name_lowercase_index"
(mysql does not support the "CREATE INDEX...IF NOT EXIST" syntax).
This would create some DB inefficiency (due to the presence of two
functionally identical indexes) for those who have installed credhub v2.12.94
(which we think is a smaller subset of users), but the system
would still work and likely a net improvement compared to before
due to the presence of indexing. And we would instruct these users
to manually drop the old, duplicate index ("credential_name_lowercase").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants