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

LB-2131: databasechangeloglock table creation not threadsafe #2198

Merged
merged 8 commits into from Nov 18, 2021

Conversation

StevenMassaro
Copy link
Contributor

Environment

Liquibase Version:

Liquibase Integration & Version: <Pick one: CLI, maven, gradle, spring boot, servlet, etc.>

Liquibase Extension(s) & Version:

Database Vendor & Version:

Operating System Type & Version:

Pull Request Type

  • Bug fix (non-breaking change which fixes an issue.)
  • Enhancement/New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

The creation of the databasechangeloglock table was not threadsafe. If the creation of the table failed, Liquibase would exit. This change introduces a retry loop with a random wait between failed iterations so that multiple instances of Liquibase can be run concurrently on a fresh database.

Steps To Reproduce

This can be reproduced with a shell script that calls the liquibase command line multiple times concurrently, when run against a database that does not already contain the databasechangelog table:

liquibase update --changelogfile sql.sql &
liquibase update --changelogfile sql.sql &
liquibase update --changelogfile sql.sql &
wait

The changelogfile should contain something that can be run multiple times successfully, like this:

update name set id = 2;

assuming that the table was created with a script like this:

create table name(id int unique);
insert into name values(1);

Actual Behavior

Caused by: liquibase.exception.DatabaseException: ERROR: relation "databasechangelog" already exists [Failed SQL: CREATE TABLE public.databasechangelog (ID VARCHAR(255) NOT NULL, AUTHOR VARCHAR(255) NOT NULL, FILENAME VARCHAR(255) NOT NULL, DATEEXECUTED TIMESTAMP WITHOUT TIME ZONE NOT NULL, ORDEREXECUTED INT NOT NULL, EXECTYPE VARCHAR(10) NOT NULL, MD5SUM VARCHAR(35), DESCRIPTION VARCHAR(255), COMMENTS VARCHAR(255), TAG VARCHAR(255), LIQUIBASE VARCHAR(20), CONTEXTS VARCHAR(255), LABELS VARCHAR(255), DEPLOYMENT_ID VARCHAR(10))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:103)
at liquibase.changelog.StandardChangeLogHistoryService.init(StandardChangeLogHistoryService.java:240)
at liquibase.Liquibase.checkLiquibaseTables(Liquibase.java:1124)
at liquibase.Liquibase.listUnrunChangeSets(Liquibase.java:1186)
at liquibase.Liquibase.listUnrunChangeSets(Liquibase.java:1176)
... 74 more
Caused by: org.postgresql.util.PSQLException: ERROR: relation "databasechangelog" already exists
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:323)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:322)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:308)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:279)
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:307)
... 85 more

Expected/Desired Behavior

If the table already exists, Liquibase sleeps for a random amount of time and then tries again, trying a maximum of ten times to create the databasechangeloglock table if it does not exist.

Screenshots (if appropriate)

Additional Context

This closes issue #2083

Fast Track PR Acceptance Checklist:

Need Help?

Come chat with us on our discord channel

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.

databasechangelog* table creation not thread/multiprocess safe
4 participants