mirror of https://github.com/apache/archiva.git
added missing xml file, added methods to determine if database exists or not, fixed up test cases for db initialization and removal
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@512366 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e43e5befe7
commit
210b3267a8
|
@ -30,15 +30,15 @@
|
|||
<name>Archiva Database</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ibatis</groupId>
|
||||
<groupId>org.apache.ibatis</groupId>
|
||||
<artifactId>ibatis-sqlmap</artifactId>
|
||||
<version>2.1.0.565</version>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-artifact-manager</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-project</artifactId>
|
||||
</dependency>
|
||||
|
|
|
@ -179,7 +179,7 @@ public class AbstractMetadataKeyDatabase
|
|||
|
||||
protected void dropTable( String tableName )
|
||||
throws ArchivaDatabaseException
|
||||
{
|
||||
{
|
||||
SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
|
||||
|
||||
try
|
||||
|
@ -231,6 +231,58 @@ public class AbstractMetadataKeyDatabase
|
|||
}
|
||||
|
||||
|
||||
|
||||
protected boolean tableExists( String tableName )
|
||||
throws ArchivaDatabaseException
|
||||
{
|
||||
SqlMapClient sqlMap = ibatisHelper.getSqlMapClient();
|
||||
|
||||
try
|
||||
{
|
||||
sqlMap.startTransaction();
|
||||
|
||||
Connection con = sqlMap.getCurrentConnection();
|
||||
|
||||
DatabaseMetaData databaseMetaData = con.getMetaData();
|
||||
|
||||
ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null );
|
||||
|
||||
// check if the index database exists in the database
|
||||
while ( rs.next() )
|
||||
{
|
||||
String dbTableName = rs.getString( "TABLE_NAME" );
|
||||
|
||||
// if it does then we are already initialized
|
||||
if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch ( SQLException e )
|
||||
{
|
||||
getLogger().error( "Error while check database, showing all linked exceptions in SQLException." );
|
||||
|
||||
while ( e != null )
|
||||
{
|
||||
getLogger().error( e.getMessage(), e );
|
||||
|
||||
e = e.getNextException();
|
||||
}
|
||||
|
||||
throw new ArchivaDatabaseException( "Error while checking database.", e );
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
sqlMap.endTransaction();
|
||||
}
|
||||
catch ( SQLException e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,14 @@
|
|||
<property name="JDBC.Password" value="${jdbc.password}"/>
|
||||
</dataSource>
|
||||
</transactionManager>
|
||||
|
||||
<!--
|
||||
<resultObjectFactory type="org.codehaus.plexus.ibatis.PlexusResultObjectFactory" >
|
||||
<property name="foo" value="bar"/>
|
||||
</resultObjectFactory>
|
||||
-->
|
||||
|
||||
<sqlMap resource="org/apache/maven/archiva/database/CreateTables.xml"/>
|
||||
<sqlMap resource="org/apache/maven/archiva/database/ManageTables.xml"/>
|
||||
<sqlMap resource="org/apache/maven/archiva/database/MetadataKey.xml"/>
|
||||
<sqlMap resource="org/apache/maven/archiva/database/RepositoryMetadata.xml"/>
|
||||
</sqlMapConfig>
|
|
@ -10,7 +10,7 @@ METADATA_KEYS is the index table for all other tables
|
|||
|
||||
need to make the lookup on this table fast, perhaps by indexing the combination of g:a:v in a lookup column
|
||||
-->
|
||||
<statement id="initializeMetadataKeyTable">
|
||||
<statement id="createMetadataKeys">
|
||||
CREATE TABLE MetadataKeys (
|
||||
metadataKey integer generated always as identity ( start with 1 ) primary key,
|
||||
groupId varchar(100) not null,
|
||||
|
@ -19,22 +19,29 @@ need to make the lookup on this table fast, perhaps by indexing the combination
|
|||
)
|
||||
</statement>
|
||||
|
||||
<statement id="initializeRepositoryMetadataTable">
|
||||
<statement id="dropMetadataKeys">
|
||||
DROP TABLE MetadataKeys
|
||||
</statement>
|
||||
|
||||
<statement id="createRepositoryMetadata">
|
||||
CREATE TABLE RepositoryMetadata (
|
||||
metadataKey integer not null,
|
||||
id integer generated always as identity ( start with 1 ) primary key,
|
||||
repositoryId varchar(100) not null,
|
||||
latest varchar(100) not null,
|
||||
release varchar(100) not null,
|
||||
lastUpdated integer not null,
|
||||
snapshotTimestamp integer not null,
|
||||
snapshotBuildNumber integer not null,
|
||||
snapshotLocalCopy char(1) not null,
|
||||
lastUpdated integer,
|
||||
snapshotTimestamp integer,
|
||||
snapshotBuildNumber integer,
|
||||
snapshotLocalCopy char(1),
|
||||
foreign key( metadataKey ) references MetadataKeys( metadataKey )
|
||||
)
|
||||
</statement>
|
||||
|
||||
<statement id="initializeHealthMetadataTable">
|
||||
<statement id="dropRepositoryMetadata">
|
||||
DROP TABLE RepositoryMetadata
|
||||
</statement>
|
||||
|
||||
<statement id="createHealthMetadata">
|
||||
CREATE TABLE HealthMetadata (
|
||||
metadataKey integer not null,
|
||||
id integer generated always as identity ( start with 1 ) primary key,
|
||||
|
@ -45,7 +52,7 @@ need to make the lookup on this table fast, perhaps by indexing the combination
|
|||
)
|
||||
</statement>
|
||||
|
||||
<statement id="initializeVersionsMetadataTable">
|
||||
<statement id="createVersionsMetadata">
|
||||
CREATE TABLE VersionMetadata (
|
||||
metadataKey integer not null,
|
||||
id integer generated always as identity ( start with 1 ) primary key,
|
|
@ -5,6 +5,9 @@
|
|||
|
||||
<sqlMap namespace="MetadataKey">
|
||||
|
||||
<!--
|
||||
this query can be improved by adding a lookup index based on these trinity of values
|
||||
-->
|
||||
<select id="getMetadataKey" resultClass="org.apache.maven.archiva.database.key.MetadataKey">
|
||||
SELECT
|
||||
metadataKey,
|
||||
|
@ -21,4 +24,10 @@
|
|||
VALUES ( #groupId#, #artifactId#, #version# )
|
||||
</insert>
|
||||
|
||||
<delete id="removeMetadataKey" parameterClass="org.apache.maven.archiva.database.key.MetadataKey">
|
||||
DELETE FROM
|
||||
MetadataKeys
|
||||
WHERE metadataKey=#metadataKey#
|
||||
</delete>
|
||||
|
||||
</sqlMap>
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
|
||||
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
|
||||
|
||||
<!--
|
||||
metadataKey integer not null,
|
||||
id integer generated always as identity ( start with 1 ) primary key,
|
||||
latest varchar(100) not null,
|
||||
release varchar(100) not null,
|
||||
lastUpdated integer not null,
|
||||
snapshotTimestamp integer not null,
|
||||
snapshotBuildNumber integer not null,
|
||||
snapshotLocalCopy char(1) not null,
|
||||
foreign key( metadataKey ) references MetadataKeys( metadataKey )
|
||||
-->
|
||||
|
||||
<sqlMap namespace="RepositoryMetadata">
|
||||
|
||||
<select id="getRepositoryMetadata"
|
||||
parameterClass="org.apache.maven.archiva.database.key.MetadataKey"
|
||||
resultClass="org.apache.maven.artifact.repository.metadata.Metadata">
|
||||
SELECT
|
||||
mk.groupId AS groupId,
|
||||
mk.artifactId AS artifactId,
|
||||
mk.version AS baseVersion,
|
||||
rm.latest AS latest,
|
||||
rm.release AS release,
|
||||
rm.lastUpdated AS lastUpdated
|
||||
FROM RepositoryMetadata rm, MetadataKeys mk
|
||||
WHERE
|
||||
mk.metadataKey = rm.metadataKey
|
||||
AND mk.metadataKey = #metadataKey#
|
||||
</select>
|
||||
|
||||
<!--
|
||||
<insert id="addRepositoryMetadata" parameterClass="org.apache.maven.artifact.repository.metadata.Metadata">
|
||||
INSERT INTO
|
||||
RepositoryMetadata ( latest, release, lastUpdated )
|
||||
VALUES ( #latest#, #release#, #lastUpdated# )
|
||||
</insert>
|
||||
-->
|
||||
|
||||
<delete id="removeRepositoryMetadata" parameterClass="org.apache.maven.archiva.database.key.MetadataKey">
|
||||
DELETE FROM
|
||||
RepositoryMetadata
|
||||
WHERE metadataKey=#metadataKey#
|
||||
</delete>
|
||||
|
||||
</sqlMap>
|
|
@ -1,46 +0,0 @@
|
|||
package org.apache.maven.archiva.database;
|
||||
|
||||
import org.apache.maven.archiva.database.key.MetadataKey;
|
||||
import org.apache.maven.artifact.repository.metadata.Metadata;
|
||||
import org.codehaus.plexus.PlexusTestCase;
|
||||
|
||||
public class IbatisMetadataStoreTest
|
||||
extends PlexusTestCase
|
||||
{
|
||||
|
||||
protected void setUp()
|
||||
throws Exception
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void testMetadataKeysInitialization() throws Exception
|
||||
{
|
||||
MetadataStore store = (MetadataStore) lookup( MetadataStore.ROLE, "ibatis" );
|
||||
|
||||
assertNotNull( store );
|
||||
}
|
||||
|
||||
public void testMetadataKeyRetrieval() throws Exception
|
||||
{
|
||||
MetadataStore store = (MetadataStore) lookup( MetadataStore.ROLE, "ibatis" );
|
||||
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setArtifactId( "testArtifactId" );
|
||||
metadata.setGroupId( "testGroupId" );
|
||||
metadata.setVersion( "testVersion" );
|
||||
|
||||
store.addMetadata( metadata );
|
||||
|
||||
MetadataKey metadataKey = store.getMetadataKey( metadata );
|
||||
|
||||
assertTrue( metadataKey.getMetadataKey() > 0 );
|
||||
assertEquals( metadataKey.getArtifactId(), metadata.getArtifactId() );
|
||||
assertEquals( metadataKey.getGroupId(), metadata.getGroupId() );
|
||||
assertEquals( metadataKey.getVersion(), metadata.getVersion() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package org.apache.maven.archiva.database;
|
||||
|
||||
import org.apache.maven.archiva.database.key.MetadataKey;
|
||||
import org.apache.maven.artifact.repository.metadata.Metadata;
|
||||
import org.codehaus.plexus.PlexusTestCase;
|
||||
import org.codehaus.plexus.ibatis.PlexusIbatisHelper;
|
||||
|
||||
public class RepositoryMetadataDatabaseTest
|
||||
extends PlexusTestCase
|
||||
{
|
||||
/**
|
||||
* @plexus.requirement
|
||||
*/
|
||||
protected PlexusIbatisHelper ibatisHelper;
|
||||
|
||||
protected void setUp()
|
||||
throws Exception
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void testRepositoryMetadataCreationAndDeletion() throws Exception
|
||||
{
|
||||
RepositoryMetadataDatabase db = (RepositoryMetadataDatabase) lookup( "org.apache.maven.archiva.database.RepositoryMetadataDatabase", "default" );
|
||||
|
||||
assertNotNull( db );
|
||||
assertTrue( db.tableExists( "RepositoryMetadata" ) );
|
||||
assertTrue( db.tableExists( "MetadataKeys" ) );
|
||||
|
||||
db.dropTable( "RepositoryMetadata" );
|
||||
db.dropTable( "MetadataKeys" );
|
||||
|
||||
assertFalse( db.tableExists( "RepositoryMetadata" ) );
|
||||
assertFalse( db.tableExists( "MetadataKeys" ) );
|
||||
}
|
||||
|
||||
public void testMetadataKeyRetrieval() throws Exception
|
||||
{
|
||||
RepositoryMetadataDatabase db = (RepositoryMetadataDatabase) lookup( "org.apache.maven.archiva.database.RepositoryMetadataDatabase", "default" );
|
||||
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setArtifactId( "testArtifactId" );
|
||||
metadata.setGroupId( "testGroupId" );
|
||||
metadata.setVersion( "testVersion" );
|
||||
|
||||
MetadataKey metadataKey = db.getMetadataKey( metadata );
|
||||
|
||||
assertTrue( metadataKey.getMetadataKey() > 0 );
|
||||
assertEquals( metadataKey.getArtifactId(), metadata.getArtifactId() );
|
||||
assertEquals( metadataKey.getGroupId(), metadata.getGroupId() );
|
||||
assertEquals( metadataKey.getVersion(), metadata.getVersion() );
|
||||
|
||||
db.dropTable( "RepositoryMetadata" );
|
||||
db.dropTable( "MetadataKeys" );
|
||||
|
||||
assertFalse( db.tableExists( "RepositoryMetadata" ) );
|
||||
assertFalse( db.tableExists( "MetadataKeys" ) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue