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

Build scripts: check database upgrade before deploy #383

Closed
php-coder opened this issue Apr 13, 2016 · 10 comments
Closed

Build scripts: check database upgrade before deploy #383

php-coder opened this issue Apr 13, 2016 · 10 comments
Assignees
Labels
ADR/needed A decision has been made and it needs to be documented area/build scripts complexity/hard
Milestone

Comments

@php-coder
Copy link
Owner

No description provided.

@php-coder
Copy link
Owner Author

php-coder commented Apr 15, 2016

After updating password in src/main/resources/liquibase/liquibase.properties, creating SSH tunnel (ssh -L 3306:localhost:3306 my-stamps.ru) and run mvn liquibase:status I got the following error:

59 change sets have not been applied to mystamps@localhost@jdbc:mysql://localhost:3306/mystamps
     /Users/coder/git/mystamps.git/src/main/resources/liquibase/initial-state.xml::create-users_activation-table::php-coder
     /Users/coder/git/mystamps.git/src/main/resources/liquibase/initial-state.xml::create-users_table::php-coder
...

It turns out that the path to the migration is also very important. When we run migration during application startup we have prefix classpath:/ (example: classpath:/liquibase/version/0.4/2016-02-19--csrf_events.xml). When I run it locally it has prefix /Users/coder/git/mystamps.git/src/main/resources/ (example: /Users/coder/git/mystamps.git/src/main/resources/liquibase/version/0.4/2016-02-19--csrf_events.xml). Looks like we should unify changeLogFile parameter in liquibase-maven-plugin with liquibase.change-log from application-prod.properties.

@php-coder
Copy link
Owner Author

We should only modify changeLogFile parameter in liquibase-maven-plugin because classpath: prefix will be ignored (since 3.0.6 version). See for details: http://stackoverflow.com/questions/16605099/using-liquibase-file-paths-via-both-maven-and-spring

@php-coder php-coder self-assigned this Apr 15, 2016
@php-coder php-coder added this to the 0.4 milestone Apr 15, 2016
@php-coder
Copy link
Owner Author

Command for checking for updates should be mvn liquibase:status -Dliquibase.password=xxx

php-coder added a commit that referenced this issue Apr 16, 2016
Allow to migrate db that previously migrated by our application.

Addressed to #383

[ci skip]
@php-coder php-coder modified the milestones: 0.5, 0.4 May 3, 2016
@php-coder php-coder modified the milestones: next, 0.5 Jul 29, 2016
@php-coder php-coder removed their assignment Jul 29, 2016
@php-coder
Copy link
Owner Author

@php-coder
Copy link
Owner Author

Here is how to disable starting of Web server: https://www.baeldung.com/spring-boot-no-web-server

@php-coder
Copy link
Owner Author

This regression might not occur: #1557

@php-coder php-coder changed the title Build scripts: try to check database upgrade before deploy Build scripts: check database upgrade before deploy Jan 30, 2022
php-coder added a commit that referenced this issue Jan 30, 2022
Now the application can be run as
    java -jar target/mystamps.war liquibase validate
or
    ./mvnw spring-boot:run -Dspring-boot.run.arguments='liquibase,validate'
to validate that all migrations and their checksums are correct. This should prevent the case when
the application is failing to start after deploy as now we have a possibility to check that
migrations are valid prior the application is run.

Part of #383
php-coder added a commit that referenced this issue Jan 30, 2022
@php-coder php-coder self-assigned this Jul 11, 2022
@php-coder php-coder removed this from the next milestone Jul 11, 2022
@php-coder php-coder added this to the 0.4.7 milestone Jul 11, 2022
@php-coder
Copy link
Owner Author

php-coder commented Sep 5, 2022

The output of Ansible playbook when everything is fine:

...
TASK [Ensuring whether Liquibase migrations are valid] *************************************************************************************************************************************************************************************
ok: [my-stamps.ru]

TASK [Show stdout of liquibase validate] ***************************************************************************************************************************************************************************************************
ok: [my-stamps.ru] => {
    "liquibase_validate.stdout_lines": [
        "2022-09-05 07:27:22.180 [user:  ]  INFO 27674 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : Starting ApplicationBootstrap v0.4.7-SNAPSHOT on my-stamps.ru with PID 27674 (/data/mystamps/mystamps-candidate.war started by mystamps in /data/mystamps)",
        "2022-09-05 07:27:22.200 [user:  ] DEBUG 27674 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : Running with Spring Boot v2.1.18.RELEASE, Spring v5.1.20.RELEASE",
        "2022-09-05 07:27:22.201 [user:  ]  INFO 27674 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : The following profiles are active: test",
        "2022-09-05 07:27:24.268 [user:  ]  INFO 27674 --- [           main] liquibase.integration                    : Liquibase did not run because System property 'liquibase.shouldRun' was set to false",
        "2022-09-05 07:27:24.336 [user:  ]  INFO 27674 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : Started ApplicationBootstrap in 3.814 seconds (JVM running for 4.867)",
        "2022-09-05 07:27:24.339 [user:  ]  INFO 27674 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...",
        "2022-09-05 07:27:24.909 [user:  ]  INFO 27674 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.",
        "2022-09-05 07:27:25.106 [user:  ]  INFO 27674 --- [           main] liquibase.database                       : Set default schema name to PUBLIC",
        "2022-09-05 07:27:25.127 [user:  ]  INFO 27674 --- [           main] r.m.w.s.liquibase.LiquibaseSupport       : Validating the migrations",
        "2022-09-05 07:27:25.141 [user:  ]  INFO 27674 --- [           main] liquibase.servicelocator                 : Cannot load service: liquibase.parser.ChangeLogParser: Provider liquibase.parser.core.json.JsonChangeLogParser could not be instantiated",
        "2022-09-05 07:27:25.154 [user:  ]  INFO 27674 --- [           main] liquibase.servicelocator                 : Cannot load service: liquibase.parser.ChangeLogParser: Provider liquibase.parser.core.yaml.YamlChangeLogParser could not be instantiated",
        "2022-09-05 07:27:29.901 [user:  ]  INFO 27674 --- [           main] r.m.w.s.liquibase.LiquibaseSupport       : Migrations are valid",
        "2022-09-05 07:27:29.905 [user:  ]  INFO 27674 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...",
        "2022-09-05 07:27:29.914 [user:  ]  INFO 27674 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed."
    ]
}

TASK [Show stderr of liquibase validate] ***************************************************************************************************************************************************************************************************
ok: [my-stamps.ru] => {
    "liquibase_validate.stderr_lines": []
}

@php-coder
Copy link
Owner Author

The output of Ansible playbook when one of migration is invalid:

TASK [Ensuring whether Liquibase migrations are valid] *************************************************************************************************************************************************************************************
fatal: [my-stamps.ru]: FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "cmd": "set -eu\neval \"$(sed -n '/^[A-Z]/s||export &|p' mystamps.conf)\"\njava $JAVA_OPTS -jar mystamps-candidate.war liquibase validate\n",
    "delta": "0:00:10.400534",
    "end": "2022-09-06 06:06:29.660387",
    "rc": 1,
    "start": "2022-09-06 06:06:19.259853"
}

STDOUT:

2022-09-06 06:06:22.237 [user:  ]  INFO 3325 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : Starting ApplicationBootstrap v0.4.7-SNAPSHOT on my-stamps.ru with PID 3325 (/data/mystamps/mystamps-candidate.war started by mystamps in /data/mystamps)
2022-09-06 06:06:22.255 [user:  ] DEBUG 3325 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : Running with Spring Boot v2.1.18.RELEASE, Spring v5.1.20.RELEASE
2022-09-06 06:06:22.256 [user:  ]  INFO 3325 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : The following profiles are active: prod
2022-09-06 06:06:24.483 [user:  ]  INFO 3325 --- [           main] liquibase.integration                    : Liquibase did not run because System property 'liquibase.shouldRun' was set to false
2022-09-06 06:06:24.543 [user:  ]  INFO 3325 --- [           main] r.m.w.s.s.boot.ApplicationBootstrap      : Started ApplicationBootstrap in 4.205 seconds (JVM running for 5.276)
2022-09-06 06:06:24.546 [user:  ]  INFO 3325 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-09-06 06:06:25.137 [user:  ]  INFO 3325 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-09-06 06:06:25.347 [user:  ]  INFO 3325 --- [           main] r.m.w.s.liquibase.LiquibaseSupport       : Validating the migrations
2022-09-06 06:06:25.389 [user:  ]  INFO 3325 --- [           main] liquibase.servicelocator                 : Cannot load service: liquibase.parser.ChangeLogParser: Provider liquibase.parser.core.json.JsonChangeLogParser could not be instantiated
2022-09-06 06:06:25.411 [user:  ]  INFO 3325 --- [           main] liquibase.servicelocator                 : Cannot load service: liquibase.parser.ChangeLogParser: Provider liquibase.parser.core.yaml.YamlChangeLogParser could not be instantiated
2022-09-06 06:06:29.000 [user:  ]  INFO 3325 --- [           main] liquibase.changelog                      : Reading from mystamps.DATABASECHANGELOG
2022-09-06 06:06:29.614 [user:  ] ERROR 3325 --- [           main] r.m.w.s.liquibase.LiquibaseSupport       : Failed to validate migrations

liquibase.exception.ValidationFailedException: Validation Failed:
     2 change sets check sum
          liquibase/version/0.4.4/2020-05-23--modify_condition_field.xml::modify-collections_series-condition-field::php-coder was: 8:04ed13bc0d22dbc6d8b228421c46cabe but is now: 8:e68d35a7c2773ef7e4f064a6b365a115
          liquibase/version/0.4.4/2020-05-23--modify_condition_field.xml::modify-series_sales-condition-field::php-coder was: 8:293900b116eb46f9f4134cca74d50056 but is now: 8:26084de457d9fbc7274e8828a9a9ecfc

	at liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:296) ~[liquibase-core-4.11.0.jar!/:na]
	at liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:272) ~[liquibase-core-4.11.0.jar!/:na]
	at ru.mystamps.web.support.liquibase.LiquibaseSupport.validate(LiquibaseSupport.java:113) [classes!/:0.4.7-SNAPSHOT]
	at ru.mystamps.web.support.liquibase.LiquibaseSupport.performLiquibaseValidate(LiquibaseSupport.java:96) [classes!/:0.4.7-SNAPSHOT]
	at ru.mystamps.web.support.liquibase.LiquibaseSupport.validate(LiquibaseSupport.java:78) [classes!/:0.4.7-SNAPSHOT]
	at ru.mystamps.web.support.spring.boot.ApplicationBootstrap.main(ApplicationBootstrap.java:52) [classes!/:0.4.7-SNAPSHOT]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [mystamps-candidate.war:0.4.7-SNAPSHOT]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [mystamps-candidate.war:0.4.7-SNAPSHOT]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:51) [mystamps-candidate.war:0.4.7-SNAPSHOT]
	at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:58) [mystamps-candidate.war:0.4.7-SNAPSHOT]

2022-09-06 06:06:29.628 [user:  ]  INFO 3325 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-09-06 06:06:29.638 [user:  ]  INFO 3325 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.


STDERR:

Exception in thread "main" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
	at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:58)
Caused by: liquibase.exception.ValidationFailedException: Validation Failed:
     2 change sets check sum
          liquibase/version/0.4.4/2020-05-23--modify_condition_field.xml::modify-collections_series-condition-field::php-coder was: 8:04ed13bc0d22dbc6d8b228421c46cabe but is now: 8:e68d35a7c2773ef7e4f064a6b365a115
          liquibase/version/0.4.4/2020-05-23--modify_condition_field.xml::modify-series_sales-condition-field::php-coder was: 8:293900b116eb46f9f4134cca74d50056 but is now: 8:26084de457d9fbc7274e8828a9a9ecfc

	at liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:296)
	at liquibase.changelog.DatabaseChangeLog.validate(DatabaseChangeLog.java:272)
	at ru.mystamps.web.support.liquibase.LiquibaseSupport.validate(LiquibaseSupport.java:113)
	at ru.mystamps.web.support.liquibase.LiquibaseSupport.performLiquibaseValidate(LiquibaseSupport.java:96)
	at ru.mystamps.web.support.liquibase.LiquibaseSupport.validate(LiquibaseSupport.java:78)
	at ru.mystamps.web.support.spring.boot.ApplicationBootstrap.main(ApplicationBootstrap.java:52)
	... 8 more


MSG:

non-zero return code

php-coder added a commit that referenced this issue Sep 6, 2022
The error was:
306 Shells that use pipes should set the pipefail option
src/main/scripts/ci/ansible/deploy.yml:41
Task/Handler: Ensuring whether Liquibase migrations are valid

Should be in 6162b3c commit.

See https://ansible-lint.readthedocs.io/en/latest/rules/risky-shell-pipe/

Relate to #383
php-coder added a commit that referenced this issue Sep 6, 2022
…version of ansible-lint

See ansible/ansible-lint#663

It seems like it's fixed in 5.0.5 version but we have old version (#1515)

Relate to #383
@php-coder
Copy link
Owner Author

Fails in TravisCI as expected (because of #1557): https://app.travis-ci.com/github/php-coder/mystamps/jobs/581907556

@php-coder php-coder added the ADR/needed A decision has been made and it needs to be documented label Dec 4, 2023
@php-coder
Copy link
Owner Author

ADR: d9a3700
ADR: 3c9c6a7 (no easy way to execute liquibase validate programatically)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ADR/needed A decision has been made and it needs to be documented area/build scripts complexity/hard
Projects
None yet
Development

No branches or pull requests

1 participant