Sharing environment settings in datasource.groovy

只愿长相守 提交于 2019-12-12 09:53:43

问题


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...


回答1:


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"
    ]
]



回答2:


I think you can do that externalizing you configuration. Load different files according with the current environment.

if(Environment.current in ['omar','stringer']) {
  grails.config.locations = [
    'file: path/to/DevelopersConfig.groovy'
  ]
}

And in your file you can declare any configuration needed, including the dataSource.

Another option is to setup a single environment and use it for both developers. This will eliminate the need of a external config file.

EDIT

Now I saw again you config file, I think that you just need to adjust omar and stringer configs:

omar {
  dataSource_dev {
    url = "jdbc:oracle:thin:@omardb.wire.com:1521:devl"
  }
}

Since it's a different dataSource it should not be inside a dataSource block. Looking at the docs it seems that you need to set a suffix and not prefix to your multiple datasources.




回答3:


You can do this using closures as below:

devDataSourceConfig = { // Note the equals sign!
   pooled = false
   driverClassName = "oracle.jdbc.driver.OracleDriver"
   username = "dev"
   password = "dev"
}
standardDataSourceConfig = {
   pooled = true
   driverClassName = "org.h2.Driver"
   username = "sa"
   password = "something"
}

environments {
    omar {
        dataSource {
            devDataSourceConfig.call()
            url = "jdbc:oracle:thin:@omardb.wire.com:1521:devl"
        }
    }
    stringer {
        dataSource {
            devDataSourceConfig.call()
            url = "jdbc:oracle:thin:@stringerdb.wire.com:1521:devl"
        }
    }
    devint {
        dataSource {
            standardDataSourceConfig.call()
            dbCreate = "create-drop"
            url = "jdbc:h2:mem:devDb"
        }
    }
    test {
        dataSource {
            standardDataSourceConfig.call() 
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb"
        }
    }
    production {
        dataSource {
            standardDataSourceConfig.call()
            dbCreate = "update"
            url = "jdbc:h2:prodDb"
        }
    }
}

Similar question on SO here.



来源:https://stackoverflow.com/questions/18366128/sharing-environment-settings-in-datasource-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!