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

From version 1.1
edited by 5fbc055b7cc103006957e1ae
on 2012-06-14 12.59:26
Change comment: There is no comment for this version
To version 2.1
edited by 5fbc055b7cc103006957e1ae
on 2012-06-14 06.29:38
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,6 +1,6 @@
1 -=== {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-Preliminary"/}}Preliminary ===
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, fields and relations with 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 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.
4 4  
5 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:
6 6  
... ... @@ -11,15 +11,21 @@
11 11  SERIALNR:integer(5), not null
12 12  {{/code}}
13 13  
14 -It will also take a list of TableInfo ({{code language="none"}}org.clazzes.jdbc2xml.schema.TableInfo{{/code}}) Objects and create a database from them, if it finds an empty database when it starts.
14 += {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-ProjectConfiguration"/}}Project Configuration =
15 15  
16 -
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}}).
17 17  
18 -=== {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-UsingOSGiandBlueprint"/}}Using OSGi and Blueprint ===
18 +Optionally, you may set the base version (default value 0.1.00) and base description String (default "initial database schema").
19 19  
20 -After adding a dependency to the {{code language="none"}}org.clazzes.jdbc2xml{{/code}} library with your favourite package management system, add the following to your blueprint {{code language="none"}}services.xml{{/code}} configuration:
20 +Database updates are passed as a Map<String, ISchemaUpdateSnippet> (org.clazzes.jdbc2xml.schema.ISchemaUpdateSnippet) - details see below.
21 21  
22 -{{code language="none"}}
22 +To perform the operations, call {{code language="none"}}SchemaManager.start(){{/code}}.
23 +
24 +=== {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-UsingSpringorOSGi/Blueprint"/}}Using Spring or OSGi/Blueprint ===
25 +
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 +
28 +{{code language="html/xml"}}
23 23  <bp:reference id="dialectFactory" interface="org.clazzes.jdbc2xml.schema.IDialectFactory">
24 24  </bp:reference>
25 25   
... ... @@ -27,7 +27,7 @@
27 27  </bp:reference>
28 28   
29 29  <bp:bean id="sqlDialect" factory-ref="dialectFactory" factory-method="newDialect">
30 -<bp:argument ref="jdbcUrl"> <!-- JDBC URL -->
36 +<bp:argument> <!-- Pass JDBC URL as an argument -->
31 31  </bp:argument>
32 32  </bp:bean>
33 33   
... ... @@ -36,13 +36,12 @@
36 36  </bp:property>
37 37  </bp:bean>
38 38   
39 -<bp:bean id="databaseSetup" class="org.clazzes.jdbc2xml.schema.SchemaManager"
40 -init-method="start">
45 +<bp:bean id="databaseSetup" class="org.clazzes.jdbc2xml.schema.SchemaManager" init-method="start">
41 41  <bp:property name="dataSource" ref="dataSource"></bp:property>
42 42  <bp:property name="schemaEngine" ref="schemaEngine"></bp:property>
43 43  <bp:property name="baseVersion" value="0.1.00" />
44 44  <bp:property name="baseTables">
45 -<bp:bean factory-ref="tableDefinitions" factory-method="getSetup" />
50 +<!-- Add List of TableDefinitions here -->
46 46  </bp:property>
47 47  <bp:property name="upateSnippets">
48 48  <!-- Add Update-Snippets here -->
... ... @@ -50,4 +50,99 @@
50 50  </bp:bean>
51 51  {{/code}}
52 52  
53 -The SchemaManager takes a map of
58 +By default, JDBC2XML provides an implementation of IDialectFactory and ISchemaEngineFactory as an OSGi service or via ServiceRegistry lookup for Spring.
59 +
60 += {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-Settingupaninitialdatabaseschema"/}}Setting up an initial database schema =
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:
63 +
64 +{{code language="java"}}
65 +package org.clazzes.example.jdbc2xml;
66 +
67 +import java.sql.Types;
68 +import java.util.Arrays;
69 +import java.util.List;
70 +
71 +import org.clazzes.jdbc2xml.schema.ColumnInfo;
72 +import org.clazzes.jdbc2xml.schema.ForeignKeyInfo;
73 +import org.clazzes.jdbc2xml.schema.PrimaryKeyInfo;
74 +import org.clazzes.jdbc2xml.schema.TableInfo;
75
76 +public class TableDefinitions {
77 +
78 + /* It is adviseable to provide the Strings used as names for tables and columns as
79 + constants, so they can be reused to build sql-statements */
80 + public static final String TB_EXAMPLE_TABLE_NAME = "ADDRESSBOOK";
81 + public static final String COL_EXAMPLE_ID = "ID";
82 + public static final String COL_EXAMPLE_NAME = "NAME";
83 + public static final String COL_EXAMPLE_ADDRESS_REF = "ADDRESS";
84 + public static final String COL_EXAMPLE_BIRTHDAY = "BIRTHDAY";
85
86 + /* ... */
87
88 + private List<TableInfo> setup;
89 +
90 + public TableDefinitions() {
91 +/* Create a table */
92 +TableInfo exampleTable = new TableInfo(TB_EXAMPLE_TABLE_NAME);
93 +exampleTable.setColumns(
94 + Arrays.asList(new ColumnInfo[] {
95 +new ColumnInfo(COL_EXAMPLE_ID, Types.BIGINT, 20, null, false, null,true),
96 +new ColumnInfo(COL_EXAMPLE_NAME, Types.VARCHAR, 256, null, false, null),
97 +new ColumnInfo(COL_EXAMPLE_ADDRESS_REF, Types.BIGINT, 20, null, true, null),
98 +new ColumnInfo(COL_EXAMPLE_BIRTHDAY, Types.DATE, 12, null, false, null)
99 +}));
100
101 +/* Example for creating a foreign key reference */
102 +exampleTable.setForeignKeys(Arrays.asList(new ForeignKeyInfo[] {
103 +new ForeignKeyInfo("FK_EXAMPLE_ADDRESS", COL_EXAMPLE_ADDRESS_REF, TB_ADDRESS, COL_ADDRESS_ID)
104 +}));
105
106 +/* Example for creating a primary key */
107 +exampleTable.setPrimaryKey(
108 +new PrimaryKeyInfo("PK_EXAMPLE", COL_EXAMPLE_ID)
109 +);
110 +
111 +/* ... */
112
113 +this.setup = Arrays.asList(
114 +exampleTable,
115 +/* ... */
116 +);
117 +
118 + }
119 +  
120 + public List<TableInfo> getSetup() {
121 +return this.setup;
122 + }
123 +
124 +}
125 +{{/code}}
126 +
127 +You must inject {{code language="none"}}TableDefinitions.getSetup(){{/code}} into {{code language="none"}}SchemaManager.setBaseTables(){{/code}} before calling {{code language="none"}}SchemaManager.start(){{/code}}.
128 +
129 +Using Blueprint/Spring, you can do this with the following snippet:
130 +
131 +{{code language="none"}}
132
133 +{{/code}}
134 +
135 += {{id name="HowTocreateandupdateDatabasesusingSchemaManagerandSchemaUpdateSnippets-UpdatingadatabaseschemawithSchemaUpdateSnippets"/}}Updating a database schema with SchemaUpdateSnippets =
136 +
137 +To update the database or it's content with schema updates, you must create a new implementation of ISchemaUpdateSnippet ({{code language="none"}}org.clazzes.jdbc2xml.schema.ISchemaUpdateSnippet{{/code}}) for each consecutive update. SchemaManager takes a {{code language="none"}}Map<String, Class<? extends ISchemaUpdateSnippet>>{{/code}} which contains the update classes keyed by the originating (e.g. previous) version.
138 +
139 +
140 +
141 +
142 +
143 +
144 +
145 +
146 +
147 +
148 +
149 +
150 +
151 +
152 +
153 +
Confluence.Code.ConfluencePageClass[0]
Id
... ... @@ -1,1 +1,1 @@
1 -656804
1 +656809
URL
... ... @@ -1,1 +1,1 @@
1 -https://clazzes.atlassian.net/wiki/spaces/JDBC2XML/pages/656804/How To create and update Databases using SchemaManager and SchemaUpdateSnippets
1 +https://clazzes.atlassian.net/wiki/spaces/JDBC2XML/pages/656809/How To create and update Databases using SchemaManager and SchemaUpdateSnippets