private static Set getRelationshipSchemaStatements()

in v2/googlecloud-to-neo4j/src/main/java/com/google/cloud/teleport/v2/neo4j/database/CypherGenerator.java [270:410]


  private static Set<String> getRelationshipSchemaStatements(
      RelationshipTarget target, Neo4jCapabilities capabilities) {
    RelationshipSchema schema = target.getSchema();
    if (schema == null) {
      return Set.of();
    }
    Set<String> statements = new LinkedHashSet<>();
    String escapedType = sanitize(target.getType());

    if (capabilities.hasRelationshipTypeConstraints()) {
      Map<String, PropertyType> types = indexPropertyTypes(target.getProperties());
      for (var constraint : schema.getTypeConstraints()) {
        String property = constraint.getProperty();
        statements.add(
            "CREATE CONSTRAINT "
                + sanitize(constraint.getName())
                + " IF NOT EXISTS FOR ()-[r:"
                + escapedType
                + "]-() REQUIRE r."
                + sanitize(property)
                + " IS :: "
                + CypherPatterns.propertyType(types.get(property)));
      }
    }

    if (capabilities.hasRelationshipKeyConstraints()) {
      for (var constraint : schema.getKeyConstraints()) {
        var properties =
            CypherPatterns.qualifyAll("r", CypherPatterns.sanitizeAll(constraint.getProperties()));
        var options = CypherPatterns.schemaOptions(constraint.getOptions());
        statements.add(
            "CREATE CONSTRAINT "
                + sanitize(constraint.getName())
                + " IF NOT EXISTS FOR ()-[r:"
                + escapedType
                + "]-() REQUIRE ("
                + String.join(", ", properties)
                + ") IS RELATIONSHIP KEY"
                + (options.isEmpty() ? "" : " " + options));
      }
    }

    if (capabilities.hasRelationshipUniqueConstraints()) {
      for (var constraint : schema.getUniqueConstraints()) {
        var properties =
            CypherPatterns.qualifyAll("r", CypherPatterns.sanitizeAll(constraint.getProperties()));
        var options = CypherPatterns.schemaOptions(constraint.getOptions());
        statements.add(
            "CREATE CONSTRAINT "
                + sanitize(constraint.getName())
                + " IF NOT EXISTS FOR ()-[r:"
                + escapedType
                + "]-() REQUIRE ("
                + String.join(", ", properties)
                + ") IS UNIQUE"
                + (options.isEmpty() ? "" : " " + options));
      }
    }
    if (capabilities.hasRelationshipExistenceConstraints()) {
      for (var constraint : schema.getExistenceConstraints()) {
        statements.add(
            "CREATE CONSTRAINT "
                + sanitize(constraint.getName())
                + " IF NOT EXISTS FOR ()-[r:"
                + escapedType
                + "]-() REQUIRE r."
                + sanitize(constraint.getProperty())
                + " IS NOT NULL");
      }
    }

    for (var index : schema.getRangeIndexes()) {
      var properties =
          CypherPatterns.qualifyAll("r", CypherPatterns.sanitizeAll(index.getProperties()));
      statements.add(
          "CREATE INDEX "
              + sanitize(index.getName())
              + " IF NOT EXISTS FOR ()-[r:"
              + escapedType
              + "]-() ON ("
              + String.join(", ", properties)
              + ")");
    }

    for (var index : schema.getTextIndexes()) {
      String options = CypherPatterns.schemaOptions(index.getOptions());
      statements.add(
          "CREATE TEXT INDEX "
              + sanitize(index.getName())
              + " IF NOT EXISTS FOR ()-[r:"
              + escapedType
              + "]-() ON (r."
              + sanitize(index.getProperty())
              + ")"
              + (options.isEmpty() ? "" : " " + options));
    }

    for (var index : schema.getPointIndexes()) {
      String options = CypherPatterns.schemaOptions(index.getOptions());
      statements.add(
          "CREATE POINT INDEX "
              + sanitize(index.getName())
              + " IF NOT EXISTS FOR ()-[r:"
              + escapedType
              + "]-() ON (r."
              + sanitize(index.getProperty())
              + ")"
              + (options.isEmpty() ? "" : " " + options));
    }

    for (var index : schema.getFullTextIndexes()) {
      String options = CypherPatterns.schemaOptions(index.getOptions());
      var properties =
          CypherPatterns.qualifyAll("r", CypherPatterns.sanitizeAll(index.getProperties()));
      statements.add(
          "CREATE FULLTEXT INDEX "
              + sanitize(index.getName())
              + " IF NOT EXISTS FOR ()-[r:"
              + escapedType
              + "]-() ON EACH ["
              + String.join(", ", properties)
              + "]"
              + (options.isEmpty() ? "" : " " + options));
    }

    if (capabilities.hasVectorIndexes()) {
      for (var index : schema.getVectorIndexes()) {
        String options = CypherPatterns.schemaOptions(index.getOptions());
        statements.add(
            "CREATE VECTOR INDEX "
                + sanitize(index.getName())
                + " IF NOT EXISTS FOR ()-[r:"
                + escapedType
                + "]-() ON (r."
                + sanitize(index.getProperty())
                + ")"
                + (options.isEmpty() ? "" : " " + options));
      }
    }
    return statements;
  }