液基Spring如何使用单独的用户进行模式更改 [英] Liquibase & Spring how to use separate user for schema changes

查看:47
本文介绍了液基Spring如何使用单独的用户进行模式更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用Liquibase作为使用现有数据库服务器(不同类型)部署Web应用程序的潜在解决方案.

I'm looking into Liquibase as a potential solution to deploy my web application using pre-existing database servers (of different types).

此应用程序应该以只能操作数据的用户访问数据库,我想使用其他用户作为架构所有者.由于我的应用程序使用Spring,所以我认为我可以使用Integration类,尽管这意味着我必须创建第二个数据源,该数据源将在我的应用程序运行期间保持打开状态,这会破坏分离帐户的目的.

This application should access the database with a user that can only manipulate data, I would like to use a different user as schema owner. Since my application uses Spring I thought I could use the integration class, though it would mean I have to create a second datasource which will remain opened for as long as my application runs which defeats the purpose of separating accounts.

有人遇到过同样的问题吗?有解决方案的主意吗?我确定可以手动执行liquibase并传递相关信息,但我想知道是否有人想出一种更清洁的方法.

Does anyone ever faced the same problem ? Any idea for a solution ? I sure can execute liquibase manually and pass relevant information but I wondered if someone figured a cleaner approach.

预先感谢您的帮助.

推荐答案

感谢您的输入Nathan,我研究了Spring和CommandLine类,最后编写了自己的小型静态方法,该方法使用所有者"凭证打开数据库连接.,通过此连接应用更改日志并关闭它.

Thanks for your input Nathan, I looked into the Spring and CommandLine classes and ended up writing my own small static method which opens a DB connection with "owner" credentials, apply the changelog through this connection and close it.

然后我从WebApplicationInitializer调用此方法,从而允许liquibase在初始化Spring bean之前运行.这样,我可以确定所做的更改是在与数据库相关的任何代码运行之前应用的,而且我只能通过我的数据源建立用户"连接.

I then call this method from a WebApplicationInitializer, allowing liquibase to run before Spring beans are initialized. This way I'm sure the changes are applied before any piece of code related with database runs plus I get to only have "user" connections through my data source.

作为一个很好的副作用,该静态方法也可以在启动单元测试时使用,这使我可以在干净创建的数据库上运行测试,而不必依赖于休眠的DDL.

And as a nice side-effect, that static method can also be used when starting unit tests, which allows me to run tests on a cleanly created database instead of relying on hibernate's DDL.

以下是代码示例,第一个是实用程序类,该实用程序类封装了从我的应用程序的属性文件中检索架构所有者连接详细信息的逻辑( http://pastebin.com/HP4HzFmp ),它还有几种方法可以简化与单元测试设置的集成.

Here are the code examples, first is a utility class that encapsulate the logic to retrieve schema owner connection details from my application's properties file (http://pastebin.com/HP4HzFmp), it also has couple methods to ease integration with unit tests setup.

然后在每个Web应用程序中,我使用的是Spring的WebApplicationInitializer,它使我可以在应用程序启动时(即在Hibernate向数据库发出任何请求之前)触发liquibase更改应用程序:

Then in each web application I'm using a WebApplicationInitializer from Spring which allows me to trigger liquibase changes application right when the application start (i.e. before Hibernate makes any request to the DB): http://pastebin.com/anXicM15. Doing it this way allows me to keep a complete separation between schema manipulations and data-related connection.

关于数据库连接的详细信息,我将它们全部存储在一个属性文件中,该文件可直接由这段代码读取,并由Spring用作Hibernate配置的Placholder替换源.如果要实现进一步的隔离,则可以在更安全的位置替换用于捕获连接详细信息的部分.

With regards to database connection details, I store all of them in a properties files which is read directly by this piece of code and used as placholder replacement source by Spring for the Hibernate configuration. If you want to achieve even further isolation you can replace the part that grabs connection details by a more secured location.

这篇关于液基Spring如何使用单独的用户进行模式更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆