Search code examples
grailsgrails-orm

Sharing environment settings in datasource.groovy


So we are able to create different environment settings in the datasource.groovy file. And we can put common settings outside the environments node like so

dataSource {
   pooled = false
   driverClassName = "org.h2.Driver"
   username = "sa"
   password = ""
}
environments {
    development {
        dataSource {
            dbCreate = "create-drop"
           url = "jdbc:h2:mem:devDb"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb"
        }
    }
}

But is there a way to so that we can have certain enviroments share some properties while others share a different set of properties such as having a shared set between developers (Omar, and Stringer in this case):

dev_dataSource {
   pooled = false
   driverClassName = "oracle.jdbc.driver.OracleDriver"
   username = "dev"
   password = "dev"
}
dataSource {
   pooled = true
   driverClassName = "org.h2.Driver"
   username = "sa"
   password = "something"
}
environments {
    omar {
        dataSource {
            dev_dataSource {
                url = "jdbc:oracle:thin:@omardb.wire.com:1521:devl"
            }
        }
    }
    stringer {
        dataSource {
            dev_dataSource {
                url = "jdbc:oracle:thin:@stringerdb.wire.com:1521:devl"
            }
        }
    }
    devint {
        dataSource {
            dbCreate = "create-drop"
            url = "jdbc:h2:mem:devDb"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb"
        }
    }
}

Thanks in advance...


Solution

  • In addition to @Sergio's approach, you can also use datasource as you have expected for respective users during development assuming you use run-app by following this command:

    grails -Dgrails.env=omar run-app //uses Omar's datasource
    grails -Dgrails.env=stringer run-app //uses Stringer's datasource
    

    UPDATE
    The way to tailor the datasource in order to access datasource_dev is bit tricky here. You have to take care the way ConfigSlurper reads the ConfigObject.

    dataSource_dev {
       pooled = false
       driverClassName = "oracle.jdbc.driver.OracleDriver"
       username = "dev"
       password = "dev"
    }
    dataSource {
       pooled = true
       driverClassName = "org.h2.Driver"
       username = "sa"
       password = "something"
    }
    environments {
        omar {
            //You do not need this if you only want to use dev's datasource 
            //dataSource { 
                dataSource_dev {
                    url = "jdbc:oracle:thin:@omardb.wire.com:1521:devl"
                }
            //}
        }
        stringer {
            //You do not need this if you only want to use dev's datasource
            //dataSource {
                dataSource_dev {
                    url = "jdbc:oracle:thin:@stringerdb.wire.com:1521:devl"
                }
            //}
        }
        devint {
            dataSource {
                dbCreate = "create-drop"
                url = "jdbc:h2:mem:devDb"
            }
        }
        test {
            dataSource {
                dbCreate = "update"
                url = "jdbc:h2:mem:testDb"
            }
        }
        production {
            dataSource {
                dbCreate = "update"
                url = "jdbc:h2:prodDb"
            }
        }
    }
    

    Based on the above setup you can very well inherit the default properties from datasource_dev for omar environement.

    Grails converts/reads the above configuration and ends up with:

    [
        dataSource_dev: [
            pooled: false,
            driverClassName: "oracle.jdbc.driver.OracleDriver",
            username: "dev",
            password: "dev",
            url: "jdbc:oracle:thin:@omardb.wire.com:1521:devl"  //<-- Omar's setting
        ],
        dataSource: [
            pooled: true,
            driverClassName: "org.h2.Driver",
            username: "sa",
            password: "something"
        ]
    ]