You are reading the documentation for an outdated Corteza release. 2023.9 is the latest stable Corteza release.

Corteza store layer

The store layer provides an extremely flexible way of defining how and where the data should be stored.

Schema management

Define new schemas

You can define a new schema in the store/rdbms/rdbms_schema.go file. The system goes over all definitions and assures that all of the tables are present in the connected database.

This step doesn’t take into account any potential schema manipulations. See Update existing schemas for details.

The following example defines a table for Corteza Messaging messages:
// General bits regarding the package
// ...

func (s Schema) Tables() []*Table {
  return []*Table{
    // Other schema definitions
    // ...

    s.MessagingMessage(),

    // Other schema definitions
    // ...
  }
}

// Other schema definitions
// ...

func (Schema) MessagingMessage() *Table {
  return TableDef("messaging_message",
    ID,
    ColumnDef("type", ColumnTypeText),
    ColumnDef("message", ColumnTypeText),
    ColumnDef("meta", ColumnTypeJson),
    ColumnDef("rel_channel", ColumnTypeIdentifier),
    ColumnDef("rel_user", ColumnTypeIdentifier),
    ColumnDef("reply_to", ColumnTypeIdentifier, DefaultValue("0")),
    ColumnDef("replies", ColumnTypeInteger, DefaultValue("0")),

    CUDTimestamps,
  )
}

// Other schema definitions
// ...

Update existing schemas

When developing new features that require new store definitions, don’t clutter the generic_upgrades.go file. Either drop the original table and re-create it or manually apply the updates.

If you are working with others, make sure to also coordinate this with them.

You can define schema altering in the store/rdbms/generic_upgrades.go file. The system will go over all of the definitions and assure that everything is up-to-date.

This is an idempotent operation, meaning that the result will always be the same.

Using plain human words — the system will only apply the changes that have not yet been applied. The result is the same, no matter how many times you run it.

The following example renames the actionlog table:
// General bits regarding the package
// ...

func (g genericUpgrades) Before(ctx context.Context) error {
  return g.all(ctx,
    g.RenameActionlog,

    // Other operations
    // ...
  )
}

// Other definitions
// ...

func (g genericUpgrades) RenameActionlog(ctx context.Context) error {
  return g.RenameTable(ctx, "sys_actionlog", "actionlog")
}

// Other definitions
// ...