michael-simons / neo4j-migrations

Automated script runner aka "Migrations" for Neo4j. Inspired by Flyway.
https://michael-simons.github.io/neo4j-migrations/
Apache License 2.0
115 stars 23 forks source link

Is there any possible way to set constraints like it is mentioned in Neo4j Migrations, is it possible in SDN 7.1.1 (@Unique , @Required, @Index) ? if possible can you guide me with this? #1156

Closed Kanish2002 closed 10 months ago

michael-simons commented 10 months ago

You can just use the annotations. It’s is described here https://michael-simons.github.io/neo4j-migrations/2.7.0/#appendix_annotation look for the headline “additional annotations”

Kanish2002 commented 10 months ago
@Node
class StrategyStarter : StrategyNode(){
    @Required
    @Unique
    var strategyName: String? = null
}

Hello sir this is my code, and I created two instances of StrategyStarter

val strategyStarter = StrategyStarter("a")
val strategyStarter1 = StrategyStarter("a")

neo4jTemplate.saveAll(listOf(strategyStarter,strategyStarter1))
OPTIONAL MATCH (hlp:`StrategyStarter`) WHERE hlp.id = $__id__ WITH hlp WHERE hlp IS NULL CREATE (strategyStarter:`StrategyStarter`) WITH strategyStarter SET strategyStarter += $__properties__ RETURN strategyStarter UNION MATCH (strategyStarter:`StrategyStarter`) WHERE strategyStarter.id = $__id__ WITH strategyStarter SET strategyStarter += $__properties__ RETURN strategyStarter
2023-12-01T17:20:43.894+05:30 DEBUG 1242869 --- [           main] org.springframework.data.neo4j.cypher    : Executing:
OPTIONAL MATCH (hlp:`StrategyStarter`) WHERE hlp.id = $__id__ WITH hlp WHERE hlp IS NULL CREATE (strategyStarter:`StrategyStarter`) WITH strategyStarter SET strategyStarter += $__properties__ RETURN strategyStarter UNION MATCH (strategyStarter:`StrategyStarter`) WHERE strategyStarter.id = $__id__ WITH strategyStarter SET strategyStarter += $__properties__ RETURN strategyStarter
2023-12-01T17:20:44.064+05:30  WARN 1242869 --- [           main] .s.n.m.s.b.a.MigrationsAutoConfiguration : No package to scan is configured and none of the configured locations exists.
2023-12-01T17:20:44.281+05:30  INFO 1242869 --- [           main] a.s.n.m.core.Migrations.Startup          : neo4j-migrations/2.7.0 connected to 
neo4j@localhost:7687 (Neo4j/5.3.0 Enterprise Edition)
Database: neo4j
2023-12-01T17:20:44.638+05:30  INFO 1242869 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path ''
2023-12-01T17:20:44.641+05:30  INFO 1242869 --- [           main] c.z.strategy.StrategyApplicationKt       : Started StrategyApplicationKt in 1.966 seconds (process running for 2.166)

Still 2 nodes with same name is created image

    implementation("eu.michael-simons.neo4j:neo4j-migrations-spring-boot-starter:2.7.0")
    implementation("eu.michael-simons.neo4j:neo4j-migrations-annotation-catalog:2.7.0")

above are the groups I use in my build.gradle.kts in a SpringBoot Project

Is there anything I'm doing in a wrong way?

Please guide me with the above information.

michael-simons commented 10 months ago

I think you should read up on the concept:

dependencies {
    annotationProcessor 'eu.michael-simons.neo4j:neo4j-migrations-annotation-processor'

   // Plus the starter and the dependency for the catalog on implementation
}

This will generate Cypher scripts for you, containing the necessary constraint definitions. Those must be applied to your database, all the checks will be done in the database.

Neo4j-Migrations will do this for you if you configure the paths correctly. For that, see the docs or the example in here:

https://github.com/michael-simons/neo4j-migrations/blob/d1e42c6ed63b2c3ce9f1d2352d27b75dd2f382a7/neo4j-migrations-examples/neo4j-migrations-examples-sb/pom.xml#L155-L194

I am not a Gradle person, but I think it should not be too hard to translate this.

In the example test I'm using the SDN annotations alone (See https://github.com/michael-simons/neo4j-migrations/blob/main/neo4j-migrations-examples/neo4j-migrations-examples-sb/src/main/java/ac/simons/neo4j/migrations/examples/sb/Person.java), but that will also generate me a unique constraint for the generated id.

michael-simons commented 10 months ago

This is not a bug, therefor I did close this.