pact icon indicating copy to clipboard operation
pact copied to clipboard

Schema Migration annotations

Open EnoF opened this issue 2 years ago • 1 comments

Currently migrating deployed schema's is very cumbersome to maintain. You could attempt to migrate schema's like:

(defschema old-schema
  old: string)

(defschema new-schema
  new: string)

(defun get-row(id: string)
  (try
    (with-read table id
      { "new":= new }
      { "new": new })
    (with-read table id
      { "old":= old }
      (update table id { "new": old })
      { "new": old })))

Note, not sure if this code works.

This would maybe work for moving schema attributes, but what about adding a new column? Or changing the type of an existing column?

To assist in such migration I'd like to propose the following schema annotations:

(defschema new-schema
  @moved(new)
  old: string
  new: string
  @convert(str-to-int)
  @convert(custom-func-to-convert)
  type-changed: integer ; assuming the old schema was a string
  @default("default value if not present")
  new-column: string)

EnoF avatar Mar 14 '23 16:03 EnoF

After some further testing you could do something like this:

(try 
  (with-read 
    table
    "id" 
    { 'foobar  := foobar }
    foobar)
  (with-read 
    table
    "id" 
    { 'foo:= foo
    , 'bar:= bar }
    (update table "id" { 'foobar: { 'foo: foo, 'bar: bar }})
    { 'foo: foo
    , 'bar: bar  }))

Note that every new insert will need to fill the old attributes foo, bar with empty valid values and this won't support type changes of a column.

EnoF avatar Mar 15 '23 07:03 EnoF