Last modified by christoph_lechleitner@iteg_at on 2013-01-31 07.32:56

From version 3.1
edited by 5fbc055b7cc103006957e1ae
on 2012-06-14 07.03:35
Change comment: There is no comment for this version
To version 4.1
edited by christoph_lechleitner@iteg_at
on 2013-01-31 04.25:51
Change comment: Name of schema history table configurable now, and text refactoring

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.5fbc055b7cc103006957e1ae
1 +XWiki.christoph_lechleitner@iteg_at
Content
... ... @@ -1,9 +1,11 @@
1 1  = {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-Preliminary"/}}Preliminary =
2 2  
3 -The SchemaManager and SchemaEngine give you an abstracted access to the database schema of your application, allowing you to add and delete tables, columns, relations and data in the database on application updates. It uses a designated table to keep track of the current schema status, which it will check whenever it is run, and update the database if it is not at the correct version.
3 +The SchemaManager ({{code language="none"}}org.clazzes.jdbc2xml.schema.SchemaManager{{/code}}) and SchemaEngine provide means to maintain the database scheme of an application, allowing you to add and delete tables, columns, relations and data in the database along application updates.
4 4  
5 -SchemaManager ({{code language="none"}}org.clazzes.jdbc2xml.schema.SchemaManager{{/code}}) will create a table called SCHEMA_HISTORY if it can not find it the first time it is run. This table contains the following columns:
5 +==== {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-SchemaHistoryTable"/}}Schema History Table ====
6 6  
7 +A designated schema table, by default named SCHEMA_HISTORY, is used to keep track of the current scheme. It will be automatically at SchemaManager's first run. (% style="font-size: 10.0pt;line-height: 13.0pt;" %)SCHEMA_HISTORY contains the following columns:
8 +
7 7  {{code language="none"}}
8 8  VERSION:varchar(10), not null, primary key
9 9  DESCRIPTION:varchar(512), nullable
... ... @@ -11,17 +11,21 @@
11 11  SERIALNR:integer(5), not null
12 12  {{/code}}
13 13  
16 +In (% style="font-size: 10.0pt;font-weight: normal;line-height: 13.0pt;" %)heterogenous environments as well as in heavily modularized software architectures a single database may be shared by multiple parties each requiring a couple of tables.
17 +
18 +(% style="font-size: 10.0pt;font-weight: normal;line-height: 13.0pt;" %)To allow multiple modules (applications, libraries, other OSGi bundles) to use (% style="font-size: 10.0pt;line-height: 13.0pt;" %)JDBC2XML's SchemaManager concurrently within one database, as of JDBC 1.1.1 SchemaManager hold the name of the schema history table in an overwritable property, {{code language="none"}}versionHistoryTable{{/code}}. See (%%)[[JDBCTOXML-11>>url:https://jira.clazzes.org/browse/JDBCTOXML-11||shape="rect"]](% style="font-size: 10.0pt;line-height: 13.0pt;" %).
19 +
14 14  = {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-ProjectConfiguration"/}}Project Configuration =
15 15  
16 -To function correctly, SchemaManager needs a DataSource ({{code language="none"}}javax.sql.DataSource{{/code}}) and a list of TableInfo ({{code language="none"}}org.clazzes.jdbc2xml.schema.TableInfo{{/code}}) Objects, from which a database will be created if it finds an empty database. To function properly, it also needs an implementation of ISchemaEngine ({{code language="none"}}org.clazzes.jdbc2xml.schema.ISchemaEngine{{/code}}).
22 +SchemaManager requires a DataSource ({{code language="none"}}javax.sql.DataSource{{/code}}) and a list of TableInfo ({{code language="none"}}org.clazzes.jdbc2xml.schema.TableInfo{{/code}}) Objects, from which database structures will be created if an "empty" database is detected. Furthermore, an implementation of ISchemaEngine ({{code language="none"}}org.clazzes.jdbc2xml.schema.ISchemaEngine{{/code}}) is required.
17 17  
18 -Optionally, you may set the base version (default value 0.1.00) and base description String (default "initial database schema").
24 +Optionally, a base version (default value 0.1.00) and a base description String (default "initial database schema") may be specified.
19 19  
20 20  Database updates are passed as a Map<String, ISchemaUpdateSnippet> (org.clazzes.jdbc2xml.schema.ISchemaUpdateSnippet) - details see below.
21 21  
22 22  To perform the operations, call {{code language="none"}}SchemaManager.start(){{/code}}.
23 23  
24 -=== {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-UsingSpringorOSGi/Blueprint"/}}Using Spring or OSGi/Blueprint ===
30 +=== {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-ConfigurationusingSpringorOSGi/Blueprint"/}}Configuration using Spring or OSGi/Blueprint ===
25 25  
26 26  If you are using OSGi with Blueprint or Spring to set up your project, you can configure a SchemaManager instance by adding the following to your blueprint {{code language="none"}}services.xml{{/code}} (or Spring configuration file):
27 27  
... ... @@ -33,7 +33,7 @@
33 33  </bp:reference>
34 34   
35 35  <bp:bean id="sqlDialect" factory-ref="dialectFactory" factory-method="newDialect">
36 -<bp:argument> <!-- Pass JDBC URL as an argument -->
42 +<bp:argument ref="jdbcUrl"><!-- bean jdbcUrl specified above -->
37 37  </bp:argument>
38 38  </bp:bean>
39 39   
... ... @@ -45,22 +45,32 @@
45 45  <bp:bean id="databaseSetup" class="org.clazzes.jdbc2xml.schema.SchemaManager" init-method="start">
46 46  <bp:property name="dataSource" ref="dataSource"></bp:property>
47 47  <bp:property name="schemaEngine" ref="schemaEngine"></bp:property>
54 + <!-- optional but recommended: special name for schema history table: -->
55 + <bp:property name="versionHistoryTable" value="MYLIB_SCHEMA_HISTORY"/>
56 + <!-- optional but recommended: explicit first version -->
48 48  <bp:property name="baseVersion" value="0.1.00" />
49 49  <bp:property name="baseTables">
50 -<!-- Add List of TableDefinitions here (see below) -->
59 +<!-- List of TableDefinitions here (see below), typical: -->
60 + <!-- <bp:bean factory-ref="tableDefinitions" factory-method="getSetup" /> -->
51 51  </bp:property>
52 52  <bp:property name="upateSnippets">
53 53  <!-- Add Update-Snippets here -->
64 + <!-- Example for update from 0.1.00 to 0.1.01 -->
65 + <!-- <bp:entry key="0.1.00" value="foo.schema.SchemaUpdate_0_1_01"></bp:entry> -->
54 54  </bp:property>
55 55  </bp:bean>
56 56  {{/code}}
57 57  
58 -By default, JDBC2XML provides an implementation of IDialectFactory and ISchemaEngineFactory as an OSGi service or via ServiceRegistry lookup for Spring.
70 +By default, JDBC2XML provides an implementation of IDialectFactory and ISchemaEngineFactory as an OSGi service or via ServiceRegistry for Spring.
59 59  
60 60  = {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-Settingupaninitialdatabaseschema"/}}Setting up an initial database schema =
61 61  
62 -To create an initial database schema, you will need to provide SchemaManager with a list of TableInfo objects. The recommended way to do this is to provide a class in your project which creates this list in it's constructor and provides it through a getter. You can instantiate this class in your Spring/Blueprint config as a singleton, and feed the provided List to SchemaManager. An example of this class could look like this:
74 +To create an initial database schema, SchemaManager needs a list of TableInfo objects.
63 63  
76 +The recommended strategy is to implement a table definition class providing this list through a getter.
77 +
78 +This is an example:
79 +
64 64  {{code language="java"}}
65 65  package org.clazzes.example.jdbc2xml;
66 66  
Confluence.Code.ConfluencePageClass[0]
Id
... ... @@ -1,1 +1,1 @@
1 -656821
1 +656825
URL
... ... @@ -1,1 +1,1 @@
1 -https://clazzes.atlassian.net/wiki/spaces/JDBC2XML/pages/656821/How To create and update Databases using SchemaManager and SchemaUpdateSnippets
1 +https://clazzes.atlassian.net/wiki/spaces/JDBC2XML/pages/656825/How To create and update Databases using SchemaManager and SchemaUpdateSnippets