forked from camunda-community-hub/zeebe-simple-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The root cause was a broken check for duplicates in the DB.
This solution favors creating new ID, based on a composition of partitionId and position. See comments in PR camunda-community-hub#280 and Issue camunda-community-hub#278 for more details. Tests are added to show implementation works properly Unfortunately, because of JPA requires an @id field, a new field was introduced and there's an UPGRADE instruction documented, so people out there would be able to migrate their installations.
- Loading branch information
Showing
16 changed files
with
417 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
Upgrade documentation for Zeebe Simple Monitor | ||
================================================== | ||
|
||
## Upgrading from v2.0.0 | ||
|
||
Due to some issues with storing variables and element instances, the database structure changed. | ||
Zeebe Simple Monitor will not alter existing database tables automatically, but if you have a PostgreSQL | ||
or other DB running, you need to alter the table structures manually, in order to keep your data. | ||
|
||
Of course, if you use an in-memory DB or do not need to keep prior data, then simply drop all tables and sequences, | ||
and let Zeebe Simple Monitor create them again for you (automatic creation works). | ||
|
||
### Upgrade procedure | ||
|
||
1. stop Zeebe Simple Monitor (v2.0.0) | ||
2. run the SQL script below against your PostgreSQL Database | ||
3. start up Zeebe Simple Monitor (new version) | ||
|
||
```sql | ||
-- part 1, element_instance table changes | ||
ALTER TABLE element_instance ADD COLUMN ID varchar(255); | ||
UPDATE element_instance SET id = (partition_id_::varchar || '-' || position_::varchar) where true; | ||
ALTER TABLE element_instance DROP CONSTRAINT element_instance_pkey; | ||
ALTER TABLE element_instance ADD PRIMARY KEY (id); | ||
CREATE INDEX element_instance_processInstanceKeyIndex ON element_instance (process_instance_key_); | ||
-- part 2, variable table changes | ||
ALTER TABLE variable ADD COLUMN ID varchar(255); | ||
ALTER TABLE variable ADD COLUMN PARTITION_ID_ integer DEFAULT 1; | ||
UPDATE variable SET id = (partition_id_::varchar || '-' || position_::varchar) where true; | ||
ALTER TABLE variable DROP CONSTRAINT variable_pkey; | ||
ALTER TABLE variable ADD PRIMARY KEY (id); | ||
CREATE INDEX variable_processInstanceKeyIndex ON variable (process_instance_key_); | ||
``` | ||
(This SQL was developed and tested using a recent PostgreSQL instance. | ||
You might need to adopt that if you're using another Database) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/java/io/zeebe/monitor/repository/ElementInstanceRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.zeebe.monitor.repository; | ||
|
||
import io.zeebe.monitor.entity.ElementInstanceEntity; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ElementInstanceRepositoryTest extends ZeebeRepositoryTest { | ||
|
||
@Autowired | ||
private ElementInstanceRepository elementInstanceRepository; | ||
|
||
@Test | ||
public void JPA_will_automatically_update_the_ID_attribute() { | ||
// given | ||
ElementInstanceEntity elementInstance = createElementInstance(); | ||
|
||
// when | ||
elementInstanceRepository.save(elementInstance); | ||
|
||
// then | ||
assertThat(elementInstance.getId()).isEqualTo("123-456"); | ||
} | ||
|
||
@Test | ||
public void variable_can_be_retrieved_by_transient_ID() { | ||
// given | ||
ElementInstanceEntity elementInstance = createElementInstance(); | ||
|
||
// when | ||
elementInstanceRepository.save(elementInstance); | ||
|
||
// then | ||
Optional<ElementInstanceEntity> entity = elementInstanceRepository.findById("123-456"); | ||
assertThat(entity).isPresent(); | ||
} | ||
|
||
private ElementInstanceEntity createElementInstance() { | ||
ElementInstanceEntity elementInstance = new ElementInstanceEntity(); | ||
elementInstance.setPartitionId(123); | ||
elementInstance.setPosition(456L); | ||
return elementInstance; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/io/zeebe/monitor/repository/TestContextJpaConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.zeebe.monitor.repository; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.jdbc.datasource.DriverManagerDataSource; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.JpaVendorAdapter; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Properties; | ||
|
||
@Configuration | ||
@EnableJpaRepositories(basePackages = "io.zeebe.monitor.repository") | ||
@EnableTransactionManagement | ||
public class TestContextJpaConfiguration { | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
DriverManagerDataSource dataSource = new DriverManagerDataSource(); | ||
dataSource.setDriverClassName("org.h2.Driver"); | ||
dataSource.setUrl("jdbc:h2:mem:zeebe-monitor-test;DB_CLOSE_DELAY=-1"); | ||
dataSource.setUsername("sa"); | ||
dataSource.setPassword(""); | ||
return dataSource; | ||
} | ||
|
||
@Bean | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Autowired DataSource dataSource) { | ||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); | ||
em.setDataSource(dataSource); | ||
em.setPackagesToScan("io.zeebe.monitor.entity"); | ||
|
||
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); | ||
em.setJpaVendorAdapter(vendorAdapter); | ||
em.setJpaProperties(getAdditionalJpaProperties()); | ||
|
||
return em; | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager transactionManager(@Autowired LocalContainerEntityManagerFactoryBean entityManagerFactory) { | ||
JpaTransactionManager transactionManager = new JpaTransactionManager(); | ||
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject()); | ||
return transactionManager; | ||
} | ||
|
||
private Properties getAdditionalJpaProperties() { | ||
Properties p = new Properties(); | ||
p.setProperty("database-platform", "org.hibernate.dialect.H2Dialect"); | ||
p.setProperty("hibernate.hbm2ddl.auto", "update"); | ||
return p; | ||
} | ||
|
||
} |
Oops, something went wrong.