Hyperledger Composer / Fabric - Update data model of assets - hyperledger-fabric

I have a question about how to update the data model of the assets already stored in the blockchain.
1 - Is it possible to change the data model of an asset? For example by adding new fields or changing the existing ones.
2 - Is it possible to add a new asset to the ledger? By creating a new one which doesn't exist.
3 - These logics could be applied both to HL Composer both to HL Fabric (chaincode written in nodeJS/goLang)?
Thanks.

Adding New fields to a Composer Model:
If you add a new field to an Asset (or participant), you can no longer see the data of existing Asset instances, but if you then remove the field from the model model - you can see the data again! If you add the new field but with 'optional' after the field in the model, you will see the original data.
If you don't want the new field to be optional, you need to start with it as optional, then run code to add a value to the new field for existing Asset instances, then you can remove the optional keyword.
Add a new asset type to the Composer model
You can add a new Asset type to the model file, and then upgrade the model on the Fabric.

Related

Update only some attributes of an entity

I'm new to Core Data and I would like to update only some attributes of an entity that may or may not yet exist in the store.
What I'm currently doing is inserting a new entity with only a few fields set, but afterwards I find that the other attributes have been reset to nil on entities for which those attributes have already been set.
I am using a merge policy of NSMergeByPropertyObjectTrumpMergePolicy which I believe would overwrite only the attributes I have set on my latest update.
I would like to avoid a complicated approach of first obtaining the entity, and then doing a field-by-field reset using old values.

Hyperledger composer upgrade business network

Let's say I have a business network v1.0.0 which has a Participant with the following model:
participant Member identified by memberId {
o String memberId
o String firstName
}
After deploying the network I create a Member.
Later I want to add a field to the Member model. E.g.
participant Member identified by memberId {
o String memberId
o String firstName
o String lastName
}
So I create new network and upgrade the previous one:
composer network upgrade -c peeradmin#hlfv1 -n example-netowrk -V 2.0.0
What happens to the member I created before? Is it deleted? How can I keep that in the system and continue to use it and update it's information by only adding lastName?
The evolution of model definitions is described in the documentation here
https://hyperledger.github.io/composer/latest/reference/model-compatibility and should answer your question.
when you change the model, your api will change as well to reflect the changes. Your data won't disappear, it can't as it's on the ledger, but it won't have the new fields you added.
Once your new model is deployed you can then issue a GET request to that asset, this gives you all the existing data, you populate the new field with whatever you need and issue a PUT request on that asset. This will now give you an old asset with the new data.
Of course, I suggest you think carefully how you change existing assets. I am thinking here of a policy which says you should not rename anything, you should not delete anything. all you should do is add new properties. If you start messing around with renaming and deleting, this is bound to cause issue, especially in a production environment.

Pre defining and pre populating a field in Couchdb

I am trying to pre define and possibly pre populate a field in CouchDB every time a new document is created by the user. That is until a user enters a different value the initial value that I created will stay.
According to this article it is not possible to do so: (CouchDB: Pre-filled fields when adding new documents?)
I was just wondering if there was an update to this. Or is there an easier way to do this?

MongoDB: Copy a collection of referenced documents as subdocuments

I made the mistake of designing a scheme so that I have two collections where one has documents which contain a manual reference to the other. I realized now that I should have created it so that the parent collection contained the other collection as sub-documents instead.
The problem is, I've already put this scheme out into a production environment where hundreds of entries have already been created. What I'd like to do is somehow scan over all of the existing data, and copy the items to their referenced parent_id as a sub-document.
Here is an example of my schema:
Collection 1 - User
_id
Name
Collection 2 - Photos
_id
url
user_id
Is there a quick way to change the existing documents to be one collection like this:
Collection - User
_id
Name
Photos: [...]
Once I have the database setup correctly, I can easily modify my code to use the new one, but the problem I'm having is figuring out how to quickly/procedural copy the documents to their parent.
Additional detail - I'm using MongoHQ.com to host my MongoDB.
Thank You.
I don't know the specifics of your environment, but this sort of change usually involves the following kinds of steps:
Ensure that your old code doesn't complain if there is a Photos array in the User object.
"Freeze" the application so that new User and Photo documents are not created
Run a migration script that copies the Photo documents into the User documents. This should be pretty easy to create either in javaScript or through app code using the driver (see example below)
Deploy the new version of the application that expects Photos to be embedded in the array
"Unfreeze" the application to start creating new documents
If you cannot "Freeze/Unfreeze" you will need to run a delta script after step 4 that will migrate newly created Photo documents after the new application is deployed.
The script will look something like this (untested):
db.User.find().forEach(function (u) {
u.Photos = new Array();
db.Photo.find({user_id : u._id}).forEach(function (p) {
u.Photos.push(p);
}
db.User.Save(u);
}

Core Data automatic migration doesn't create the new entity in my new model

I made a new version of my core data model which includes one new entity "Test" with one attribute "type".
The lightweight migration worked with no errors but I noticed that the new entity was not created in the database. When fetching this new entity "Test" , the result was nil. I had the attribute "type" set as non-optional with a default value of 1 because I expected that the migration will create the new entity with this default value . But it didn't. So my question is : is this expected behavior? In the case of core data migration, the new entities are not created but set as nil? Or if I am missing some steps , what happens if the new model has relationships between the new entity "Test" and the existing entities? Thanks.
You need to create an object of this entity, using for example -[NSEntityDescription insertNewObjectForEntityName:...]. Simply having the entity in the model will not create any objects.
You can do this at the end of migration, or lazily at startup (if you do a fetch for an object of this entity and can't find it).

Resources