Skip to content

SchemaGeneration

Guillaume Le Cousin edited this page Apr 9, 2022 · 3 revisions

The class SchemaBuilderFromEntities allows to build a RelationalDatabaseSchema from a list of entity classes.

Then the schema can be used on a dialect (RelationalDatabaseSchemaDialect) to generate statements, that can be printed or executed on a database.

For example:

@Component
public class MyClass {

  @Autowired
  private LcReactiveDataRelationalClient lcClient;
  
  public Mono<Void> createSchema() {
    RelationalDatabaseSchema schema = SchemaBuilderFromEntities.build(lcClient.getEntities());
    SchemaStatements statements = lcClient.getSchemaDialect().createSchemaContent(schema);
    // print to console
    statements.print(System.out);
    // drop and create content on database
    return lcClient.dropCreateSchemaContent(schema);
  }
}

As Spring Data R2DBC only provides the @Column annotation, which does not allow to specify constraints, the annotation @ColumnDefinition can be used to specify more information:

  • nullable
  • min and max that can be used with characters columns
  • precision and scale for floating point columns
  • precision for time precision (fractional part)

Clone this wiki locally