bedatadriven / activityinfo-R

ActivityInfo R Language Client
https://www.activityinfo.org/support/docs/R/
17 stars 12 forks source link

`migrateField()` function to move data for columns that have changed field type #31

Closed Ryo-N7 closed 1 year ago

Ryo-N7 commented 1 year ago

Preserve the data inside fields that have changed types (within type changes that make sense, of course).

formsch-1 formsch-2

## changing field type from form-schema
## ... does not preserve data in changed fields??

## grab form schema from existing table: Recover_Record_EX1 in FIFES
fifstab <- queryTable(form = "cb62ijfl8k2ymxx3")

dplyr::glimpse(fifstab)
## NUMPAR is a 'quantity field'

formsch <- activityinfo::getFormSchema(formId = "cb62ijfl8k2ymxx3")

## change NUMPAR field type from 'quantity' to 'FREE_TEXT'
formsch$elements[[1]] <- purrr::list_modify(formsch$elements[[1]], type = "FREE_TEXT")

## Revert formschema classes to regular "list" so that JSON // ActivityInfo can parse it properly
## formSchema S3 class >>> list
class(formsch) <- "list" 

## formField S3 class >>> list       class(formsch$elements[[1]]) 
formsch$elements <- lapply(formsch$elements, function(e) {
  class(e) <- "list"
  e
})

## Update schema with changes back to database
updateFormSchema(schema = formsch)

formsch-3 formsch-4

Currently doesn't work, data is wiped away when underlying field type changes. This was actually a Feature request brought up by project staff originally but since I knew about the updateFormSchema() function I had told them they could just use the R package. But unfortunately with the above problem, it's a harder sell as project staff don't want to lose data that already exists in the fields.

Of course, values getting wiped makes sense if I'm changing like DATE to numeric or Attachment to text or some other weird conversion but for something like text to quantity (and vice-versa), which is a conversion that's going to happen a lot (whether due to a mistake or otherwise), it would be nice if we could preserve the underlying values somehow?

Ryo-N7 commented 1 year ago

field conversion//split field into separate/etc. is not possible in ActivityInfo API

-- export and re-import into new field

Alex: create new functionality to make easier

nickdickinson commented 1 year ago

Take oldField and newField and an R function to convert the values. It will copy over the data to the new field (using importTable with recordId field and possibly parentId).

Ryo-N7 commented 1 year ago

tried to migrateField() but it doesn't seem to be working despite the "import" being successful:

before migrateField():

theformobj <- getRecords(form = "co8ify9lfrpd3z5b")

before-migrate-field

attempt migrateField(): migrate-field-proc

it says "success" but i don't see the migrated values anywhere?

not in the current form object i modified, theformobj: where-is-mig-data

nor in the db itself:

where-is-mig-data2

nickdickinson commented 1 year ago

I think it is the fn() that is introducing NAs by coercion. The letters are not converting to numbers (as.numeric) in R.

Nick

On Tue, Mar 28, 2023, 06:57 Ryo Nakagawara @.***> wrote:

tried to migrateField() but it doesn't seem to be working despite the "import" being successful:

BEFORE migrateField():

theformobj <- getRecords(form = "co8ify9lfrpd3z5b")

[image: before-migrate-field] https://user-images.githubusercontent.com/19657164/228131386-c3f8d238-c670-42bd-9b97-ccc4c4acad04.PNG

attempt migrateField(): [image: migrate-field-proc] https://user-images.githubusercontent.com/19657164/228131459-dc2b0aa5-75c9-4098-a54b-a2f3efec12ff.PNG

it says "success" but i don't see the migrated values anywhere?

not in the current form object i modified, theformobj: [image: where-is-mig-data] https://user-images.githubusercontent.com/19657164/228131961-a6825c72-4348-4238-935d-dabe720b285e.PNG

nor in the db itself:

[image: where-is-mig-data2] https://user-images.githubusercontent.com/19657164/228131968-f450f2b3-c174-4d42-88de-fbbf857071ed.PNG

— Reply to this email directly, view it on GitHub https://github.com/bedatadriven/activityinfo-R/issues/31#issuecomment-1486215529, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOP4ADOHAAOP3CRW6CNLV3W6JVSFANCNFSM6AAAAAASLDAVZA . You are receiving this because you were assigned.Message ID: @.***>