3.5.13. Schema Tool
Most users will only access the schema tool indirectly, through the interfaces provided by other tools. You may find, however, that the schema tool is a powerful utility in its own right. The schema tool has two functions:
-
To reflect on the current database schema, optionally translating it to an XML representation for further manipulation.
-
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 useful as a general way to manipulate schemas.
You can invoke the schema tool through its Java class, org.apache.openjpa.jdbc.schema.SchemaTool. In addition to the universal flags of the configuration framework, the schema tool accepts the following command line arguments:
-
-ignoreErrors/-i <true/t | false/f>: Iffalse, an exception will be thrown if the tool encounters any database errors. Defaults tofalse. -
-file/-f <stdout | output file>: Use this option to write a SQL script for the planned schema modifications, rather them committing them to the database. When used in conjunction with theexportorreflectactions, the named file will be used to write the exported schema XML. If the file names a resource in theCLASSPATH, data will be written to that resource. Usestdoutto write to standard output. Defaults tostdout. -
-openjpaTables/-ot <true/t | false/f>: When reflecting on the schema, whether to reflect on tables and sequences whose names start withOPENJPA_. Certain OpenJPA components may use such tables - for example, thetableschema factory option covered in Schema Factory. When using other actions,openjpaTablescontrols whether these tables can be dropped. Defaults tofalse. -
-rollbackBeforeDDL/-rbddl <true/t | false/f>: Set this option totrueto send an initail rollback on the connection before any DDL statement is sent. -
-dropTables/-dt <true/t | false/f>: Set this option totrueto drop tables that appear to be unused duringretainandrefreshactions. Defaults totrue. -
-dropSequences/-dsq <true/t | false/f>: Set this option totrueto drop sequences that appear to be unused duringretainandrefreshactions. Defaults totrue. -
-sequences/-sq <true/t | false/f>: Whether to manipulate sequences. Defaults totrue. -
-indexes/-ix <true/t | false/f>: Whether to manipulate indexes on existing tables. Defaults totrue. -
-primaryKeys/-pk <true/t | false/f>: Whether to manipulate primary keys on existing tables. Defaults totrue. -
-foreignKeys/-fk <true/t | false/f>: Whether to manipulate foreign keys on existing tables. Defaults totrue. -
-record/-r <true/t | false/f>: Usefalseto prevent writing the schema changes made by the tool to the currentschema factory. Defaults totrue. -
-schemas/-s <schema list>: A list of schema and table names that OpenJPA should access during this run of the schema tool. This is equivalent to setting the openjpa.jdbc.Schemas property for a single run.
The schema tool also accepts an -action or -a flag.
Multiple actions can be composed in a comma-separated list.
The available actions are:
-
add: This is the default action if you do not specify one. It brings the schema up-to-date with the given XML document by adding tables, columns, indexes, etc. This action never drops any schema components. -
retain: Keep all schema components in the given XML definition, but drop the rest from the database. This action never adds any schema components. -
drop: Drop all schema components in the schema XML. Tables will only be dropped if they would have 0 columns after dropping all columns listed in the XML. -
refresh: Equivalent toretain, thenadd. -
build: Generate SQL to build a schema matching the one in the given XML file. Unlikeadd, this option does not take into account the fact that part of the schema defined in the XML file might already exist in the database. Therefore, this action is typically used in conjunction with the-fileflag to write a SQL script. This script can later be used to recreate the schema in the XML. -
reflect: Generate an XML representation of the current database schema. -
createDB: Generate SQL to re-create the current database. This action is typically used in conjunction with the-fileflag to write a SQL script that can be used to recreate the current schema on a fresh database. -
dropDB: Generate SQL to drop the current database. LikecreateDB, this action can be used with the-fileflag to script a database drop rather than perform it. -
import: Import the given XML schema definition into the current schema factory. Does nothing if the factory does not store a record of the schema. -
export: Export the current schema factory’s stored schema definition to XML. May produce an empty file if the factory does not store a record of the schema. -
deleteTableContents: Execute SQL to delete all rows from all tables that OpenJPA knows about.
|
|
The schema tool manipulates tables, columns, indexes, constraints, and sequences. 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.
Add the necessary schema components to the database to match the given XML document, but don’t drop any data:
java org.apache.openjpa.jdbc.schema.SchemaTool targetSchema.xml
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:
java org.apache.openjpa.jdbc.schema.SchemaTool -f script.sql targetSchema.xml
Write a SQL script that will re-create the current database:
java org.apache.openjpa.jdbc.schema.SchemaTool -a createDB -f script.sql
Refresh the schema and delete all contents of all tables that OpenJPA knows about:
java org.apache.openjpa.jdbc.schema.SchemaTool -a refresh,deleteTableContents
Drop the current database:
java org.apache.openjpa.jdbc.schema.SchemaTool -a dropDB
Write an XML representation of the current schema to file schema.xml .
java org.apache.openjpa.jdbc.schema.SchemaTool -a reflect -f schema.xml