验证数据库是否如您所愿
我一直在我的机器上编写一个java应用程序,它使用我设置的数据库完美运行,但是当我在现场安装它时,它会崩溃,因为数据库略有不同。
因此,我正在编写一些代码来验证:
A:我的数据库详细信息正确
B:数据库有我期望的所有表,并且它们具有正确的列。
我已经有了 A,但我不知道从哪里开始 B,有什么建议吗?
当前客户端的目标数据库是 Oracle,但该应用程序也可以配置为在 SQL Server 上运行。 因此,我们将不胜感激通用的解决方案,但这并不是必需的,因为我确信我可以弄清楚如何从另一个解决方案中做到这一点。
I've been writing a java app on my machine and it works perfectly using the DB I set up, but when I install it on site it blows up because the DB is slightly different.
So I'm in the process of writing some code to verify that:
A: I've got the DB details correct
B: The database has all the Tables I expect and they have the right columns.
I've got A down but I've got no idea where to start with B, any suggestions?
Target DB is for the current client is Oracle, but the app can be configured to run on SQL Server as well. So a generic solution would be appreciated, but is not nessisary as I'm sure I can figure out how to do one from the other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你会想要查询数据库的information_schema,这里有一些Oracle的例子,我知道的每个平台都有类似的东西。
http://www.alberton.info/oracle_meta_info.html
You'll want to query the information_schema of the database, here are some examples for Oracle, every platform I am aware of has something similar.
http://www.alberton.info/oracle_meta_info.html
为此,您可以使用 LiquiBase 等数据库迁移工具——大多数工具都有某种检查数据库的方法。 我没有使用它的第一手经验,所以这是一个猜测。
You might be able to use a database migration tool like LiquiBase for this -- most of these tools have some way of checking the database. I don't have first hand experience using it so it's a guess.
我使用 DbUnit 来测试数据库。 它是一个基于 Java 的解决方案,与 Junit 集成良好。 几乎不用Java就可以使用它。 我没有在与您描述的情况完全相同的情况下使用它,但它应该足够接近可以工作。
I use DbUnit to test databases. It is a Java based solution, that integrates well with Junit. It is possible to use it with almost no Java. I havent used it in exactly the same situation as you described, but it should be close enough to work.
最通用的解决方案是在 try catch 块内使用具有预期值的 select 子句和具有表名的 from 子句执行查询。 您可以将 where 子句设置为 1=2,以免获取任何数据。 如果执行查询时没有抛出异常,那么您就得到了预期的表和列。
Most generic solution would be to execute queries with select clause having the expected coulmns and from clause having table names, within try catch block. You can put where clause as 1=2 so as not to fetch any data. If query executed without throwing exception then you have got the expected table and columns.
通过首先编写数据库创建脚本可能会更好地处理略有不同的部分。 自动化流程使您更有机会使两者相同。
另一点值得一提的是,您可以通过使开发和生产环境相同(两者的数据库架构和供应商相同)来最大限度地降低风险。 改变使两者不同的环境。
最后,你没有说什么是“稍微”不同,但有时这些是不可避免的(例如Oracle使用序列,SQL Server使用身份)。 也许 Hibernate 可以帮助您更可靠地在供应商之间切换。 它以这样的方式抽象细节:更改数据库可能意味着修改配置文件中的单个值。
The slightly different piece might be better handled by scripting the creation of the database in the first place. A automated process gives you a better chance of making the two identical.
Another point worth making is that you minimize your risk by making your devl and prod environments identical - same database schema and vendor for both. Change the circumstances that make the two different.
Lastly, you don't say what is "slightly" different, but sometimes these are unavoidable (e.g. Oracle uses sequences, SQL Server uses identities). Maybe Hibernate can help you to switch between vendors more reliably. It abstracts details in such a way that changing databases can mean modifying a single value in a configuration file.
您需要的基本上是数据库的单元测试。 “必须存在名为 FOOBAR 的列,类型必须为 Integer。不得存在外键等。”
这对于普通的 JUnit 和 JDBC 是可行的(向表询问其元数据),因为您可能希望确保您绝对确定正在做什么,这在使用例如 dbUnit 时可能会更困难。
What you need to have is basically Unit Tests for your database. "A column must exist named FOOBAR, the type must be Integer. No foreign keys may exist etc."
This is doable with plain JUnit and JDBC (ask the table for its meta-data) as you may want to ensure that you are absolutely certain what is being done which may be harder when using e.g. dbUnit.
中的这些表来检查表、列、视图等是否存在
您可以使用 Oracle USER_TABLES
USER_VIEWS
USER_PROCEDURE
(或所有内容)
USER_OBJECTS 其中 OBJECT_TYPE = '??'
继续...表列的 USER_TAB_COLS
问候
K
You can check for the presence of tables, columns, views, etc. using these tables in Oracle
USER_TABLES
USER_VIEWS
USER_PROCEDURE
(or for everything)
USER_OBJECTS WHERE OBJECT_TYPE = '??'
To keep going... USER_TAB_COLS for table columns
Regards
K
我为此使用 MigrateDB 。 它允许您构建查询来执行诸如检查给定数据库是否存在给定表、列、行、索引等的查询,并将它们用作“测试”。 如果测试失败,它会触发一个“操作”(这只是另一个知道如何解决问题的查询。)
MigrateDB 支持多个数据库平台(例如,您可以为每个平台指定“检查表存在查询”) ,完全可配置的测试(您可以自己编写),附带相当完整的 Oracle 测试,并且可以在“仅审核”模式下运行,以便它只告诉您差异是什么。
这是一个很好、强大的解决方案。
I use MigrateDB for this. It lets you build queries that do things like check for the existence of given tables, columns, rows, indexes, etc. for a given database and use those as "tests." If a test fails, it triggers an "action" (which is just another query that knows how to remedy the problem.)
MigrateDB supports multiple database platforms (you can specify the "check for table existence query" for each platform, for example), completely configurable tests (you can make your own up), comes with fairly complete Oracle tests, and can be run in "audit only" mode so that it only tells you what the differences are.
It's a nice, robust solution.
如果您使用普通 JDBC,则应尝试使用此方法: DatabaseMetadata.getTables 等可用的类似方法在元数据类中。
If you're using plain JDBC, you should try utilizing this method: DatabaseMetadata.getTables and other similar methods available in the metadata class.