Search code examples
grailsdatasourcegrails-orm

configure grails to create the database if it doesn't exist.


I have the following settings on a new grails project:

dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
    username = "sa"
    password = ""
}

environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:mysql://localhost/myapp?useUnicode=yes&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8"
            username = "root"
            password = ""
        }
    }
}

when I run my app it fails with an error: Error creating bean with name 'transactionManagerPostProcessor':

This error goes away when I manually go to my database and create a database called myapp

I thought the create-drop setting in dbCreate is suppose to create the db if it does not exist.

Question

How can I configure the settings so that the database gets created when it does not exist in the MySQL


Solution

  • Creating the database itself is impractical because it is very vendor-specific, even more so than the DDL to create tables, sequences, etc. You often need to specify access rules, storage options, etc.

    Hibernate will generate and run the schama DDL but you have to start the process by creating the database itself except for simple databases like H2.