3

因此我们可以在 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"
        }
    }
}

提前致谢...

4

3 回答 3

4

我认为您可以将配置外部化。根据当前环境加载不同的文件。

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

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

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

编辑

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

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

由于它是不同的数据源,它不应该在数据源块内。查看文档,您似乎需要为多个数据源设置后缀而不是前缀。

于 2013-08-21T19:45:33.997 回答
3

除了@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"
    ]
]
于 2013-08-21T19:59:02.767 回答
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"
        }
    }
}

关于 SO here的类似问题。

于 2019-05-17T10:21:43.937 回答