Skip to content

Commit d035c45

Browse files
committed
Add SchemaExportCommand
Closes gh-71 See graceframework/grace-data-hibernate#123
1 parent f9033a3 commit d035c45

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ dependencies {
6363
compileOnly "org.graceframework:grace-cli"
6464
compileOnly 'jakarta.servlet:jakarta.servlet-api'
6565
implementation "org.graceframework:grace-datastore-gorm"
66-
compileOnly "org.graceframework.plugins:hibernate"
66+
implementation "org.graceframework.plugins:hibernate"
6767

6868
testImplementation "org.graceframework:grace-shell"
6969
testImplementation "org.graceframework.plugins:hibernate"
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2015-2025 the original author or 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+
* https://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 org.hibernate.engine.spi.SessionFactoryImplementor
20+
import org.hibernate.tool.hbm2ddl.SchemaExport as HibernateSchemaExport
21+
import org.hibernate.tool.schema.TargetType
22+
23+
import grails.dev.commands.ApplicationCommand
24+
import grails.dev.commands.ExecutionContext
25+
import grails.util.Environment
26+
27+
import org.grails.build.parsing.CommandLine
28+
import org.grails.datastore.mapping.core.connections.ConnectionSource
29+
import org.grails.orm.hibernate.HibernateDatastore
30+
31+
/**
32+
* Adds a schema-export command
33+
*
34+
* @author Graeme Rocher
35+
* @author Michael Yan
36+
* @since 4.0
37+
*/
38+
@CompileStatic
39+
class SchemaExportCommand implements ApplicationCommand {
40+
41+
final String description = "Creates a DDL file of the database schema"
42+
Boolean skipBootstrap = true
43+
44+
@Override
45+
boolean handle(ExecutionContext executionContext) {
46+
CommandLine commandLine = executionContext.commandLine
47+
48+
String filename = "${executionContext.baseDir.canonicalPath}/db/schema.sql"
49+
boolean export = false
50+
boolean stdout = false
51+
52+
for (arg in commandLine.remainingArgs) {
53+
switch (arg) {
54+
case 'export': export = true; break
55+
case 'generate': export = false; break
56+
case 'stdout': stdout = true; break
57+
default: filename = arg
58+
}
59+
}
60+
61+
def argsMap = commandLine.undeclaredOptions
62+
String dataSourceName = argsMap.datasource ? argsMap.datasource : ConnectionSource.DEFAULT
63+
64+
def file = new File(filename)
65+
file.parentFile.mkdirs()
66+
67+
HibernateDatastore hibernateDatastore = applicationContext.getBean("hibernateDatastore", HibernateDatastore)
68+
hibernateDatastore = hibernateDatastore.getDatastoreForConnection(dataSourceName)
69+
70+
def serviceRegistry = ((SessionFactoryImplementor) hibernateDatastore.sessionFactory).getServiceRegistry()
71+
.getParentServiceRegistry()
72+
def metadata = hibernateDatastore.metadata
73+
74+
def schemaExport = new HibernateSchemaExport()
75+
.setHaltOnError(true)
76+
.setOutputFile(file.path)
77+
.setDelimiter(';')
78+
79+
80+
String action = export ? "Exporting" : "Generating script to ${file.absolutePath}"
81+
String ds = argsMap.datasource ? "for DataSource '$argsMap.datasource'" : "for the default DataSource"
82+
println "$action in environment '${Environment.current.name}' $ds"
83+
84+
EnumSet<TargetType> targetTypes
85+
if (stdout) {
86+
targetTypes = EnumSet.of(TargetType.SCRIPT, TargetType.STDOUT)
87+
}
88+
else {
89+
targetTypes = EnumSet.of(TargetType.SCRIPT)
90+
}
91+
92+
schemaExport.execute(targetTypes, HibernateSchemaExport.Action.CREATE, metadata, serviceRegistry)
93+
94+
if (schemaExport.exceptions) {
95+
def e = (Exception) schemaExport.exceptions[0]
96+
e.printStackTrace()
97+
return false
98+
}
99+
return true
100+
}
101+
102+
}

0 commit comments

Comments
 (0)