8.2. Schema Tool

Most users will only use the schema tool indirectly, through the interface provided by the mapping tool. You may find, however, that the schema tool is a powerful utility in its own right.

The schema tool's function is to take in an XML schema definition, calculate the differences between the XML and the existing database schema, and apply the necessary changes to make the database match the XML. The XML format used by the schema tool abstracts away the differences between SQL dialects used by different database vendors. The tool also automatically adapts its SQL to meet foreign key dependencies. Thus the schema tool is very useful as a general way to manipulate schemas.

You can invoke the schema tool through the schematool shell/bat script included in the Kodo JDO distribution, or through its Java class, kodo.jdbc.schema.SchemaTool. In addition to the universal flags of the configuration framework, the schema tool accepts the following command line arguments:

The schema tool also requires an -action flag. The available actions are:

[Note]Note

The schema tool can manipulate tables, columns, indexes, primary keys, and foreign keys. It cannot create or drop the database schema objects in which the tables reside, however. If your XML documents refer to named database schemas, those schemas must exist.

We present some examples of schema tool usage below.

Example 8.2. Schema Creation

Add the necessary schema components to the database to match the given XML document, but don't drop any data:

schematool -a add targetSchema.xml

Example 8.3. SQL Scripting

Repeat the same action as the first example, but this time don't change the database. Instead, write any planned changes to a SQL script:

schematool -a add -f script.txt targetSchema.xml

Write a SQL script that will re-create the current database:

schematool -a createDB -f script.txt

Example 8.4. Schema Drop

Drop the current database:

schematool -a dropDB