在datasource.groovy中共享环境设置

时间:2013-08-21 19:34:41

标签: grails gorm

因此我们可以在datasource.groovy文件中创建不同的环境设置。我们可以将常用设置放在环境节点之外,如此

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

但是有没有办法让我们可以让某些环境共享一些属性,而其他人共享一组不同的属性,比如在开发者之间拥有一个共享集(在这种情况下是Omar和Stringer):

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

提前致谢...

3 个答案:

答案 0 :(得分:4)

我认为你可以做到externalizing you configuration。根据当前环境加载不同的文件。

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

在您的文件中,您可以声明所需的任何配置,包括dataSource。

另一个选择是设置单个环境并将其用于两个开发人员。这将消除外部配置文件的需要。

修改

现在我再次看到你的配置文件,我认为你只需要调整omar和stringer配置:

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

因为它是一个不同的dataSource,所以它不应该在dataSource块中。看at the docs,您似乎需要为多个数据源设置后缀而不是前缀。

答案 1 :(得分:4)

除了@ Sergio的方法之外,您还可以使用datasource,因为您在开发过程中期望各个用户,假设您使用run-app执行此命令:

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

<强>更新
为了访问datasource_dev而定制数据源的方法在这里有点棘手。您必须注意ConfigSlurper读取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"
        }
    }
}

根据上述设置,您可以很好地继承datasource_dev环境omar的默认属性。

Grails转换/读取上述配置,最后得到:

[
    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 :(得分:0)

您可以使用以下闭包来完成此操作:

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

关于here的类似问题。