solomax commented on code in PR #144: URL: https://github.com/apache/openjpa/pull/144#discussion_r3556202606
########## openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestJDBCSchemaManager.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openjpa.jdbc.meta; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.UUID; + +import org.apache.openjpa.jdbc.conf.JDBCConfiguration; +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.jdbc.sql.OracleDictionary; +import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; +import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; +import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; +import org.junit.Test; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.SchemaValidationException; + +/** + * Test that a {@link MappingTool#ACTION_REFRESH} uses the right + * types for new columns and takes any mapping in DBDictionary into account. + */ +public class TestJDBCSchemaManager extends AbstractPersistenceTestCase { + /** + * First we create a schema mapping with boolean representation as CHAR(1). + * Then we create an entry. + * After that we create a diff from the entity to the current DB. + * This should result in an empty diff. + */ + @Test + public void testSchemaReset() throws Exception { + EntityManagerFactory emf = createEMF(UUIDEntity.class, DROP_TABLES); + EntityManager em = emf.createEntityManager(); + + assertNotNull(em); + + em.getTransaction().begin(); + UUIDEntity ue1 = new UUIDEntity(); + ue1.setValue("Something"); + em.persist(ue1); + + em.getTransaction().commit(); + UUID id = ue1.getId(); + closeEM(em); + + EntityManager em2 = emf.createEntityManager(); + assertNotNull(em2); + + UUIDEntity ue2 = em2.find(UUIDEntity.class, id); + assertNotNull(ue2); + assertNotEquals(ue1, ue2); + + closeEM(em2); + + closeEMF(emf); + } + + @Test + public void testBuildSchema() throws IOException, SQLException { + EntityManagerFactory emf = createEMF(UUIDEntity.class, EntityBoolChar.class, DROP_TABLES); + emf.createEntityManager(); + + emf.getSchemaManager().create(false); + emf.getSchemaManager().drop(false); + } + + @Test + public void testTruncate() { + EntityManagerFactory emf = createEMF(UUIDEntity.class, EntityBoolChar.class, DROP_TABLES); + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + UUIDEntity ue1 = new UUIDEntity(); + ue1.setValue("Something"); + em.persist(ue1); + + em.getTransaction().commit(); + UUIDEntity ue2 = em.find(UUIDEntity.class, ue1.getId()); + assertNotNull(ue2); + + closeEM(em); + + emf.getSchemaManager().truncate(); + + em = emf.createEntityManager(); + assertNull(em.find(UUIDEntity.class, ue1.getId())); + closeEM(em); + } + + @Test + public void testValidate() { + OpenJPAEntityManagerFactorySPI emf = createEMF(UUIDEntity.class, EntityBoolChar.class, DROP_TABLES);; + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + UUIDEntity ue1 = new UUIDEntity(); + ue1.setValue("Something"); + em.persist(ue1); + em.getTransaction().commit(); + + Connection conn = (Connection) ((OpenJPAEntityManagerSPI) em.getDelegate()).getConnection(); + + JDBCConfiguration conf = ((JDBCConfiguration) emf.getConfiguration()); + DBDictionary dict = conf.getDBDictionaryInstance(); + + try (Statement stmt = conn.createStatement()) { + stmt.executeUpdate("ALTER TABLE UUIDEntity DROP COLUMN value_"); + stmt.executeUpdate("ALTER TABLE UUIDEntity ADD " + (dict instanceof OracleDictionary ? "" : "COLUMN") + " value_ BOOLEAN DEFAULT FALSE"); Review Comment: `hsqldb` errors: I'll try to check them ... Additionally will try to check `postgres-16`, `postgres-18` ... ``` [ERROR] Failures: [ERROR] TestEmbeddable>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testEmbeddableContainingRelationWithGeneratedKey:314->createEmbeddableContainingRelationWithGeneratedKey:2802 [ERROR] TestJPQLScalarExpressions>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testSimpleCaseExpressions:212 the result is not male expected:<Male> but was:<Male > [ERROR] TestEJBQLFunction>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testCEILINGFunc:554 expected:<154> but was:<153> [ERROR] TestEJBQLFunction>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testCEILINGFuncNegative:568 expected:<-152> but was:<-153> [ERROR] TestEJBQLFunction>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testFLOORFunc:596 expected:<152> but was:<153> [ERROR] Errors: [ERROR] TestMultiEMFCacheModes>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testCacheRefreshModeRefresh:88 » Persistence The database dictionary in use ("class org.apache.openjpa.jdbc.sql.HSQLDictionary") reports that it does not have feature "SupportsSelectForUpdate". This feature is needed to complete the current operation. To force OpenJPA to try to use the feature anyway, set the following property: openjpa.jdbc.DBDictionary: SupportsSelectForUpdate=<value> [ERROR] TestMultiEMFCacheModes>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testCacheRefreshModeRefreshDelete:167 » InvalidState This operation failed for some instances. See the nested exceptions array for details. [ERROR] TestTablePerClassInheritanceWithAbstractRoot>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testConsistency:111 » Persistence user lacks privilege or object not found: PUBLIC.TRANSLATIONS {stmnt 957505228 ALTER TABLE TRANSLATIONS ADD COLUMN id BIGINT} [code=-5501, state=42501] [ERROR] TestTablePerClassInheritanceWithAbstractRoot>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testEntityTypeForTablePerClassInheritance:170->populate:69 » Persistence user lacks privilege or object not found: PUBLIC.TRANSLATIONS {stmnt 1227811506 ALTER TABLE TRANSLATIONS ADD COLUMN id BIGINT} [code=-5501, state=42501] [ERROR] TestJPQLScalarExpressions>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testGeneralCaseExpressions:323 » Argument Failed to execute query "select e.name, CASE WHEN e.age = 11 THEN org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.POOR WHEN e.age = 35 THEN org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.GOOD ELSE org.apache.openjpa.persistence.common.apps.CompUser$CreditRating.EXCELLENT END FROM CompUser e ORDER BY e.age". Check the query syntax for correctness. See nested exception for details. [ERROR] TestEJBQLFunction>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testExtractWEEK:798 » Persistence unexpected token: WEEK in statement [SELECT t0.userid, t0.DTYPE, t0.version, t1.id, t1.city, t1.country, t1.streetAd, t2.userid, t2.DTYPE, t2.version, t2.age, t2.compName, t2.creditRating, t2.name, t2.nameAsLob, t1.zipcode, t0.age, t0.compName, t0.creditRating, t0.name, t0.nameAsLob FROM CompUser t0 LEFT OUTER JOIN Address t1 ON t0.ADD_ID = t1.id LEFT OUTER JOIN CompUser t2 ON t1.id = t2.ADD_ID WHERE (EXTRACT(WEEK FROM DATE '2006-03-21' ) <= ?)] {SELECT t0.userid, t0.DTYPE, t0.version, t1.id, t1.city, t1.country, t1.streetAd, t2.userid, t2.DTYPE, t2.version, t2.age, t2.compName, t2.creditRating, t2.name, t2.nameAsLob, t1.zipcode, t0.age, t0.compName, t0.creditRating, t0.name, t0.nameAsLob FROM CompUser t0 LEFT OUTER JOIN Address t1 ON t0.ADD_ID = t1.id LEFT OUT ER JOIN CompUser t2 ON t1.id = t2.ADD_ID WHERE (EXTRACT(WEEK FROM {d '2006-03-21'}) <= ?)} [code=-5581, state=42581] [ERROR] TestEJBQLFunction>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->AbstractPersistenceTestCase.runTest:590->testROUNDFunc:634 » Argument Failed to execute query "SELECT ROUND(SUM(c.age)/7.0, 3) FROM CompUser c". Check the query syntax for correctness. See nested exception for details. [ERROR] TestHSQLSequence>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->setUp:80 » Persistence unexpected token: NSSEQ in statement [DROP SEQUENCE entityE nsseq gen] {DROP SEQUENCE entityE nsseq gen} [code=-5581, state=42581] [ERROR] TestHSQLSequence>AbstractPersistenceTestCase.run:213->AbstractPersistenceTestCase.runBare:553->AbstractPersistenceTestCase.runBare:577->setUp:80 » Persistence unexpected token: NSSEQ in statement [DROP SEQUENCE entityE nsseq gen] {DROP SEQUENCE entityE nsseq gen} [code=-5581, state=42581] ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
