Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 86927a0

Browse files
authored
Merge pull request #389 from grails/restore-grails-shell-commands-scripts
Restore grails-shell dependent commands and scripts
2 parents 51a3dca + 779247d commit 86927a0

File tree

8 files changed

+294
-3
lines changed

8 files changed

+294
-3
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ dependencies {
4848
exclude group: 'org.liquibase', module: 'liquibase-test-harness'
4949
exclude group: 'com.h2database', module: 'h2'
5050
}
51+
api("org.grails:grails-shell") {
52+
exclude group: 'org.slf4j', module: 'slf4j-simple'
53+
}
5154
compileOnly "org.hibernate:hibernate-core:5.6.15.Final"
5255

5356
compileOnly 'org.springframework.boot:spring-boot-starter-logging'

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
grailsVersion=6.2.0
2-
grailsGradlePluginVersion=6.2.0
3-
groovyVersion=3.0.21
1+
grailsVersion=6.2.1
2+
grailsGradlePluginVersion=6.2.1
3+
groovyVersion=3.0.23
44
liquibaseVersion=4.19.0
55
liquibaseHibernateVersion=4.19.0
66
projectVersion=5.0.0-SNAPSHOT
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2015 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.plugins.databasemigration.command
17+
18+
import groovy.transform.CompileStatic
19+
import groovy.transform.stc.ClosureParams
20+
import groovy.transform.stc.SimpleType
21+
import liquibase.parser.ChangeLogParserFactory
22+
import liquibase.serializer.ChangeLogSerializerFactory
23+
import org.grails.plugins.databasemigration.DatabaseMigrationException
24+
25+
@CompileStatic
26+
class DbmChangelogToGroovy implements ScriptDatabaseMigrationCommand {
27+
28+
@Override
29+
void handle() {
30+
def srcFilename = args[0]
31+
if (!srcFilename) {
32+
throw new DatabaseMigrationException("The $name command requires a source filename")
33+
}
34+
35+
def resourceAccessor = createResourceAccessor()
36+
37+
def parser = ChangeLogParserFactory.instance.getParser(srcFilename, resourceAccessor)
38+
def databaseChangeLog = parser.parse(srcFilename, null, resourceAccessor)
39+
40+
def destFilename = args[1]
41+
def destChangeLogFile = resolveChangeLogFile(destFilename)
42+
if (destChangeLogFile) {
43+
if (!destChangeLogFile.path.endsWith('.groovy')) {
44+
throw new DatabaseMigrationException("Destination ChangeLogFile ${destChangeLogFile} must be a Groovy file")
45+
}
46+
if (destChangeLogFile.exists()) {
47+
if (hasOption('force')) {
48+
destChangeLogFile.delete()
49+
} else {
50+
throw new DatabaseMigrationException("ChangeLogFile ${destChangeLogFile} already exists!")
51+
}
52+
}
53+
}
54+
55+
def serializer = ChangeLogSerializerFactory.instance.getSerializer('groovy')
56+
withFileOrSystemOutputStream(destChangeLogFile) { OutputStream out ->
57+
serializer.write(databaseChangeLog.changeSets, out)
58+
}
59+
60+
if (destChangeLogFile && hasOption('add')) {
61+
appendToChangeLog(changeLogFile, destChangeLogFile)
62+
}
63+
}
64+
65+
private static void withFileOrSystemOutputStream(File file, @ClosureParams(value = SimpleType, options = "java.io.OutputStream") Closure closure) {
66+
if (!file) {
67+
closure.call(System.out)
68+
return
69+
}
70+
71+
if (!file.parentFile.exists()) {
72+
file.parentFile.mkdirs()
73+
}
74+
file.withOutputStream { OutputStream out ->
75+
closure.call(out)
76+
}
77+
}
78+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2015 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.plugins.databasemigration.command
17+
18+
import groovy.transform.CompileStatic
19+
import liquibase.serializer.ChangeLogSerializer
20+
import liquibase.serializer.ChangeLogSerializerFactory
21+
import org.grails.plugins.databasemigration.DatabaseMigrationException
22+
23+
@CompileStatic
24+
class DbmCreateChangelog implements ScriptDatabaseMigrationCommand {
25+
26+
@Override
27+
void handle() {
28+
def filename = args[0]
29+
if (!filename) {
30+
throw new DatabaseMigrationException("The $name command requires a filename")
31+
}
32+
33+
def outputChangeLogFile = resolveChangeLogFile(filename)
34+
if (outputChangeLogFile.exists()) {
35+
if (hasOption('force')) {
36+
outputChangeLogFile.delete()
37+
} else {
38+
throw new DatabaseMigrationException("ChangeLogFile ${outputChangeLogFile} already exists!")
39+
}
40+
}
41+
if (!outputChangeLogFile.parentFile.exists()) {
42+
outputChangeLogFile.parentFile.mkdirs()
43+
}
44+
45+
ChangeLogSerializer serializer = ChangeLogSerializerFactory.instance.getSerializer(filename)
46+
47+
outputChangeLogFile.withOutputStream { OutputStream out ->
48+
serializer.write(new ArrayList(), out)
49+
}
50+
51+
if (hasOption('add')) {
52+
appendToChangeLog(changeLogFile, outputChangeLogFile)
53+
}
54+
}
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2015 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.plugins.databasemigration.command
17+
18+
import grails.config.ConfigMap
19+
import grails.util.Environment
20+
import grails.util.GrailsNameUtils
21+
import groovy.transform.CompileStatic
22+
import liquibase.parser.ChangeLogParser
23+
import liquibase.parser.ChangeLogParserFactory
24+
import org.grails.cli.profile.ExecutionContext
25+
import org.grails.config.CodeGenConfig
26+
import org.grails.plugins.databasemigration.EnvironmentAwareCodeGenConfig
27+
import org.grails.plugins.databasemigration.liquibase.GroovyChangeLogParser
28+
29+
import static org.grails.plugins.databasemigration.PluginConstants.DEFAULT_DATASOURCE_NAME
30+
31+
@CompileStatic
32+
trait ScriptDatabaseMigrationCommand implements DatabaseMigrationCommand {
33+
34+
ConfigMap config
35+
ConfigMap sourceConfig
36+
ExecutionContext executionContext
37+
38+
void handle(ExecutionContext executionContext) {
39+
this.executionContext = executionContext
40+
setConfig(executionContext.config)
41+
42+
this.commandLine = executionContext.commandLine
43+
this.contexts = optionValue('contexts')
44+
this.defaultSchema = optionValue('defaultSchema')
45+
this.dataSource = optionValue('dataSource') ?: DEFAULT_DATASOURCE_NAME
46+
47+
configureLiquibase()
48+
handle()
49+
}
50+
51+
void configureLiquibase() {
52+
GroovyChangeLogParser groovyChangeLogParser = ChangeLogParserFactory.instance.parsers.find { ChangeLogParser changeLogParser -> changeLogParser instanceof GroovyChangeLogParser } as GroovyChangeLogParser
53+
groovyChangeLogParser.config = config
54+
}
55+
56+
abstract void handle()
57+
58+
String getName() {
59+
return GrailsNameUtils.getScriptName(GrailsNameUtils.getLogicalName(getClass().getName(), "Command"))
60+
}
61+
62+
void setConfig(ConfigMap config) {
63+
this.sourceConfig = config
64+
this.config = new EnvironmentAwareCodeGenConfig(sourceConfig as CodeGenConfig, Environment.current.name)
65+
}
66+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import org.grails.plugins.databasemigration.DatabaseMigrationException
2+
import org.grails.plugins.databasemigration.command.DbmChangelogToGroovy
3+
4+
description('Converts a changelog file to a Groovy DSL file') {
5+
usage 'grails [environment] dbm-changelog-to-groovy [src_file_name] [dest_file_name]'
6+
flag name: 'src_file_name', description: 'The name and path of the changelog file to convert'
7+
flag name: 'dest_file_name', description: 'The name and path of the Groovy file'
8+
flag name: 'dataSource', description: 'if provided will run the script for the specified dataSource creating a file named changelog-dataSource.groovy if a filename is not given. Not needed for the default dataSource'
9+
flag name: 'force', description: 'Whether to overwrite existing files'
10+
flag name: 'add', description: 'if provided will run the script for the specified dataSource. Not needed for the default dataSource.'
11+
}
12+
13+
try {
14+
new DbmChangelogToGroovy().handle(executionContext)
15+
} catch (DatabaseMigrationException e) {
16+
error e.message, e
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.grails.plugins.databasemigration.DatabaseMigrationException
2+
import org.grails.plugins.databasemigration.command.DbmCreateChangelog
3+
4+
description('Creates an empty changelog file') {
5+
usage 'grails [environment] dbm-create-changelog [filename]'
6+
flag name: 'filename', description: 'The path to the output file to write to'
7+
flag name: 'dataSource', description: 'if provided will run the script for the specified dataSource creating a file named changelog-dataSource.groovy if a filename is not given. Not needed for the default dataSource'
8+
flag name: 'force', description: 'Whether to overwrite existing files'
9+
flag name: 'add', description: 'if provided will run the script for the specified dataSource. Not needed for the default dataSource.'
10+
}
11+
12+
try {
13+
new DbmCreateChangelog().handle(executionContext)
14+
} catch (DatabaseMigrationException e) {
15+
error e.message, e
16+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2015 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.plugins.databasemigration.command
17+
18+
import grails.util.GrailsNameUtils
19+
import org.grails.build.parsing.CommandLineParser
20+
import org.grails.cli.GrailsCli
21+
import org.grails.cli.profile.ExecutionContext
22+
import org.grails.config.CodeGenConfig
23+
import org.h2.Driver
24+
25+
abstract class ScriptDatabaseMigrationCommandSpec extends DatabaseMigrationCommandSpec {
26+
27+
ScriptDatabaseMigrationCommand command
28+
29+
CodeGenConfig config
30+
31+
def setup() {
32+
def configMap = [
33+
'grails.plugin.databasemigration.changelogLocation': changeLogLocation.canonicalPath,
34+
'dataSource.url' : 'jdbc:h2:mem:testDb',
35+
'dataSource.username' : 'sa',
36+
'dataSource.password' : '',
37+
'dataSource.driverClassName' : Driver.name,
38+
'environments.other.dataSource.url' : 'jdbc:h2:mem:otherDb',
39+
]
40+
config = new CodeGenConfig()
41+
config.mergeMap(configMap)
42+
config.mergeMap(configMap, true)
43+
44+
command = commandClass.newInstance()
45+
command.config = config
46+
command.changeLogFile.parentFile.mkdirs()
47+
}
48+
49+
abstract protected Class<ScriptDatabaseMigrationCommand> getCommandClass()
50+
51+
protected ExecutionContext getExecutionContext(String... args) {
52+
def executionContext = new GrailsCli.ExecutionContextImpl(config)
53+
executionContext.commandLine = new CommandLineParser().parse(([GrailsNameUtils.getScriptName(GrailsNameUtils.getLogicalName(commandClass.name, 'Command'))] + args.toList()) as String[])
54+
executionContext
55+
}
56+
}

0 commit comments

Comments
 (0)