Liquibase has an execute-sql command, here's a summary from the liquibase execute-sql docs
Use the execute-sql command to directly run SQL queries without changing and applying changelog files with changesets
I want to run the execute-sql command from maven but I don't think there's a maven goal for this.
The available maven goals are listed in the liquibase maven goals docs
changelogSync, changelogSyncSQL, changelogSyncToTag, changelogSyncToTagSQL, checks.run, checks.show, clearCheckSums, dbDoc, deactivateChangeLog, diff, dropAll, flow, flow.validate, futureRollbackSQL, generateChangeLog, help, history, listLocks, registerChangeLog, releaseLocks, rollback, rollbackOneChangeSet, rollbackOneChangeSetSQL, rollbackOneUpdate, rollbackOneUpdateSQL, rollbackSQL, status, syncHub, tag, tagExists, unexpectedChangeSets, update, updateSQL, updateTestingRollback, validate
Is there any way to run execute-sql via maven?
Related
I have a Spring Boot Gradle application and many MySQL servers and databases.
In different scenarios, I want to start the application with different databases or create them if they do not exist. I want to use this mechanism with command line arguments.
The URL for database is stored in application.properties file of Spring:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb1?createDatabaseIfNotExist=true&useSSL=true
I want to run gradle with command line arguments that will apply to the application.properties file so in my head will be something like this.
application.properties :
spring.datasource.url=jdbc:mysql://${linkNewDB}?createDatabaseIfNotExist=true&useSSL=true
And to run the program as :
gradle bootRun -Pargs=--linkNewDB="someNewDB:3309"
Does anybody know how can I achieve this mechanism? I tried different options but none of them worked. Thanks!
How about changing the whole URL, like this:
gradle bootRun --args='--spring.datasource.url=jdbc:mysql://someNewDB:3309?createDatabaseIfNotExist=true&useSSL=true'
reference: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#running-your-application.passing-arguments
I use a custom logback.xml to configure logging. Usually I pass it in via the logging.config property in application.properties to the spring application, however I now need to specify a path to it so it can be picked up by a non-spring mvn module.
This command works fine in my intelliJ run configuration:
verify "-Dlogback.configurationFile=${ALLUSERSPROFILE}/myPath/logback.xml" -f pom.xml
I thought that transferring this to my jenkinsFile would be trivial:
withMaven(mavenSettingsConfig: 'nexus-maven-settings') {
withNPM(npmrcConfig: 'nexus-npm-ro-settings') {
powershell 'mvn verify "-Dlogback.configurationFile=${ALLUSERSPROFILE}/myPath/logback.xml" -f webApp/com.pilz.ie.s30.automation/pom.xml'
}
}
but my logbackfile isn't working. I suspect it is a powershell issue with the single quote, double quotes or the Env var, but for some reason the logback file just isn't being picked up.
Any help appreciated!
I have a question. In my app, I do have several configurations:
application-prod.properties
application-test.properties
application-dev.properties
and the main file:
application.properties
Containing one line:
spring.profiles.active=test
To build and run the app I am using ./gradlew buildNeeded
Can I somehow pass the properties suffix: test, prod, dev so it is used in the build process, so I can make different bash scripts to run the installation process on test and prod servers?
I am looking for something like ./gradlew buildNeeded --spring.profiles.active=test or anything that will work...
You can achieve that using spring-gradle-plugin.
From plugin documentation Passing arguments to your application
Like all JavaExec tasks, arguments can be passed into bootRun from the
command line using --args='' when using Gradle 4.9 or
later. For example, to run your application with a profile named dev
active the following command can be used:
./gradlew bootRun --args='--spring.profiles.active=dev'
You can use project properties:
task buildNeeded {
doLast {
def springProfile = project.properties.getOrDefault('spring.profiles.active', 'test')
println springProfile
}
}
Then you can run the project with
./gradlew buildNeeded -Pspring.profiles.active=test
I am running Jenkins ver. 2.60.2 and it doesn't seem possible, within a Maven Job, to define a local repository not in /usr/share/tomcat7/.m2.
Here are my attempts:
I created a Global Maven settings.xml and a Settings file with the Config File Management Plugin, that contains:
<settings>
<localRepository>/srv/maven/.m2/repository</localRepository>
...
</settings>
I Created a new Maven Project. Tried to make the Job see that file by attempting all of the following:
a) Defining either Settings file or Global settings file (I created two identical files) within the build step:
b) Adding a Pre-step Provide Configuration files, and then using the variable MY_SETTINGS either in the Goals and options or MAVEN_OPTS:
c) Use the Provide Configuration files within the build environment (and using the MY_SETINGS in the same way as in the previous step.:
However, none of these seems to work. The job always fails, trying to use the default maven repository location (/usr/share(tomcat7/.m2) - which I have no idea how to re-define:
provisioning config files...
copy managed file [MYFILE settings] to file:/srv/webapps/jenkins/jobs/testJob/workspace#tmp/config3408982272576109420tmp
provisioning config files...
copy managed file [MYFILE settings] to file:/srv/webapps/jenkins/jobs/testJob/workspace#tmp/config2203063037747373567tmp
Parsing POMs
using global settings config with name MYFILE settings
Replacing all maven server entries not found in credentials list is true
Deleting 1 temporary files
ERROR: Failed to create /usr/share/tomcat7/.m2
Finished: FAILURE
Do you know how to make this work within a Maven Job type in Jenkins?
Is it possible for new Flyway migrations to be generated by JPA/Hibernate's automatic schema generation when a new model / field etc. are added via Java code.
It would be useful to capture the auto-generated SQL and save it directly to a new Flyway migration, for review / editing / committing to a project repository.
Thank you in advance for any assistance or enlightenment you can offer.
If your IDE of choice is IntelliJ IDEA, I'd recommend using the JPA Buddy plugin to do this. It can generate Flyway migrations by comparing your Java model to the target DB.
You can use it to keep your evolving model and your SQL scripts in sync.
Also, it can create the init script if your DB is empty.
Once you have it installed and have Flyway as your Maven/Gradle dependency, you can generate a migration like this:
Flyway doesn't have built-in support for diff, I use liquidbase within a maven spring boot project and changelogs can be created from JPA/hibernate changes by using:
mvn liquibase:diff
All of the options for liquibase diff are located here:
http://www.liquibase.org/documentation/maven/maven_diff.html
If you want to generate the update SQL automatically, you can ask Hibernate to do so; just add the lines below to your Spring Boot configuration:
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql
When you execute the application, this will generate a file named update.sql on the root of your project. Now, you can just copy and paste them into your Flyway migration.
This was adapted from this other answer: https://stackoverflow.com/a/36966419/679240 ; it is basically the same logic, except that one wants to generate a database creation script, while I needed an update script, instead.
BTW, if you want to replace the names of the foreign keys on the script with more readable ones, you could use this regex: ^(alter table .*?)(\w+)(\s+add constraint )\w+( foreign key \()(.*?)(\).*) with this replacement: $1$2$3fk_$2__$5$4$5$6; this will change the names of the FKs in the script to fk_name_of_the_table__name_of_the_field.