2

I'm working on testing an application that utilizes a SQL Server database. For testing purposes, I'd really like to be able to utilize an in-memory database, like HSQL. I actually have my tests running but I have one pain point still outstanding - I have a difficult time generating a SQL script that I can use to build my HSQL database. As this system is still in development, database changes can and do happen so I don't want to spend all my time manually updating SQL scripts.

Within SQL Server Management Studio, I can generate a script to build that database. That script, however, contains a lot of "stuff" that is SQL Server specific. As such, I can't just take that script to HSQL and have it execute. Instead, I need to spend a lot of time tearing out the stuff that won't work in HSQL.

A co-worker suggested using PowerBuilder to generate the script and, while the script it generated was somewhat cleaner, it still wouldn't run "as-is" in HSQL.

Can anyone suggest a method (tool, process, etc.) that I can use to point at a SQL Server database and generate myself a script that will build that exact same database in HSQL?

Thanks!

1
  • 1
    Assuming you can connect to HSQL via ODBC, you could create a SQL Server Linked Server instance, and dump tables & data into HSQL. Then there should be HSQL tools to generate scripts based on an existing database/schema. Commented Jan 28, 2011 at 16:48

3 Answers 3

2

Assuming you are using Hibernate, you can configure Hibernate in your testcase to create the tables when the sessionFactory is created.

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>...hbm file...</value>
            ...
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            ...
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

The only cons to this approach is if your HBM files don't have the matching settings as your physical SQL Server database (for example, you omit certain constraints or columns in your HBM files), then the created tables in HSQL are going to be different.

Sign up to request clarification or add additional context in comments.

Comments

2

Just as a follow-up to the answer given by limc, here's what I was able to do.

I'm using Spring and Hibernate so I was able to take my Spring data config file and turn it into this:

<bean id="sessionFactory" name="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
    <list>
    ...hbm files...
    </list>
</property>
<property name="hibernateProperties">
<props>
    <prop key="hibernate.dialect">${db.dialect}</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.use_sql_comments">false</prop> 
    <prop key="hibernate.hbm2ddl.auto">${ddl.create}</prop>
</props>
</property>
</bean>

With that in place, I was able to propertyConfigurer configuration to fill in the placeholders, that looks like this:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="propertyConfigurer" 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:test-messages.properties</value>
        </property>
    </bean>

</beans>

I have one of those files for my testing and one more for my production code. The one shown above is my testing version and references the file test-messages.properties, that looks like this:

db.dialect=org.hibernate.dialect.HSQLDialect
ddl.create=create

This actually works very nicely as I can reuse virtually all of my production config files by utilizing the placeholders and the propertyConfigurer construct. The only extra files I need for my test environment are the propertyConfigurer config files and the properties files they point to.

Thanks, limc!

As an aside, I do see the issue that limc mentioned. This system is designed to be deployed to connect to a SQL Server database in production, which will not be created via hbm2ddl. So, if there's an error in my HBM files, my tests might all work just fine while I may still get failures in production.

On one hand, I'm very unhappy about that but, on the other, I can actually run test cases against a DB in a fully self-contained way. As it stands with this solution, someone could check this project out of source control and immediately run the test cases without having to do any sort of DB setup. I like that, too. Unfortunately, I haven't yet found a really great way of having both.

Comments

0

There are tools for cross-engine database migration. One such tool that can do this job is http://liquibase.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.