spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.51k stars 598 forks source link

Make Spring Statemachine table names configurable using a prefix #1137

Open pdalfarr opened 5 months ago

pdalfarr commented 5 months ago

Context

Spring Statemachine creates tables to store statemachine's definitions and states.

As a consequence, when using Spring Statemachine in a project, the following tables are created:

action
deferred_events
guard
state
state_entry_actions
state_exit_actions
state_machine
state_state_actions
transition
transition_actions

Observations

As per the list above, one can note that table names of org.springframework.statemachine.JpaRepository* classes are hardcoded.

example taken from JpaRepositoryStateMachine.java :

@Entity
@Table(name = "state_machine")
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class)
public class JpaRepositoryStateMachine extends RepositoryStateMachine {

Potential Issues

Table name clashes can occurs with already existing tables in project's DB. Possible workarounds consist in renaming exiting table(s), or using another Datasource for Spring StateMachine.

Proposed improvement

Adding a prefix to Spring Statemachine table names, for instance "ssm" would be great, both to prevent name clashes and to ease the finding and understanding of tables in DB. Prefix would have a default value ( "ssm" or "" ?) which could be changed using application property

spring:
   statemachine:
      data:
         jpa:
            table-name-prefix: "customerorder_statemachine_"

Note about table-name-prefix: A heads up should be given to users regarding the length of table names ( see https://github.com/spring-projects/spring-statemachine/issues/469 ) ( "ssm_" prefix will cause max length to be 26, which is below 30 mentioned in issue 469 )

About the implementation of this improvement

Application property spring.jpa.hibernate.naming.physical-strategy: could help with that.

References: https://www.baeldung.com/hibernate-naming-strategy https://www.baeldung.com/hibernate-field-naming-spring-boot

About Other "statemachine" implementations

Thanks

pdalfarr commented 4 months ago

@jvalkeal Sorry to bother you, but it seems you are the main contributor on this projet. Just one question: When / how often are 'need-triage' issues processed?

Thanks.