# I want to add a bunch new optional properties/members to types - will I lose my data? DSocial

**URL:** <https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364>\
**Category:** Developers\
**Created:** [December 6, 2021, 11:24am UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364 "2021-12-06T11:24:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Ashley](https://forum.dfinity.org/u/Ashley)\
**Post date:** [December 6, 2021, 11:24am UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/1 "2021-12-06T11:24:23Z")

</div>

Hi there - I need to add many new properties/members to many types on DSocial. But I fear I’ll lose my data.

My platform is live with many uploads per day, the last thing I want is to silently lose data on an upgrade now or in future versions of dfx. Can someone confirm the following is okay? From my understanding, I can add new properties/members to Types with ‘?’ operator e.g.

Current code:

```auto
public type Video = {
    id : VideoId;
    hash : Text;
    thumb : Text;
    title : Text;
    desc : Text;
  };

```

new version:

```auto
public type Video = {
    id : VideoId;
    hash : Text;
    thumb : Text;
    title : Text;
    desc : Text;
    duration: ?Int;
    shares: ?Int;
    ctr: ?Float;
    viewTimeRate: ?Float;
  };

```

These are all stored in `HashMap.HashMap<VideoId, Video>` and a stable array `[(VideoId, Video)]`.

Will this work now without deleting data and in the future?

---

<div class="post-metadata">

**Author:** ![anthonymq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anthonymq/32/524_2.png) [@anthonymq](https://forum.dfinity.org/u/anthonymq)\
**Post date:** [December 6, 2021, 2:49pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/2 "2021-12-06T14:49:25Z")

</div>

If they are all optional fields you are good to go. Tried it myself.  
Maybe you can test it locally before deploying on the ic

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [December 6, 2021, 4:24pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/3 "2021-12-06T16:24:32Z")

</div>

It also looks compatible to me, but I want to echo the good practice of running your upgrades locally before trying in prod

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [December 6, 2021, 6:19pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/4 "2021-12-06T18:19:19Z")

</div>

I’ve been thinking about a pattern here (like with [Proposal to Adopt the Namespaced Interfaces Pattern as a Best Practice for IC Developers - #4 by nomeata](https://forum.dfinity.org/t/proposal-to-adopt-the-namespaced-interfaces-pattern-as-a-best-practice-for-ic-developers/9262/4)) where when one has a change to make in your data you actually go through the proactive steps of marking one Video and Video\_v2. Once you’ve done this you can specifically move the collection and set defaults in the post upgrade step by copying your collection VideoCollection of type Video to a VideoCollection\_v2 of type Video\_v2. After that upgrade, you could get rid of the definition of Video and VideoCollection on the next upgrade because it will be empty.

Good idea? Good practice? I’m not sure, would be interested in the community’s comments.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [December 6, 2021, 8:21pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/5 "2021-12-06T20:21:36Z")

</div>

DO NOT DO THIS!

If this change is made to the types of stable variables as you appear to be doing then you will in fact lose your data.

The type of a stable variable can only be evolved to a Motoko super type since the upgrade must be able to consume the last value of that variable.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [December 6, 2021, 8:39pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/6 "2021-12-06T20:39:26Z")

</div>

There is actual some more documentation on this here:

> **[Internet Computer | Documentation](https://smartcontracts.org/docs/language-guide/upgrades.html)**
>
> Documentation for the internet computer.

And a detailed example (for some reason not linked from the nav bar) here:

> **[Internet Computer | Documentation](https://smartcontracts.org/docs/language-guide/compatibility.html)**
>
> Documentation for the internet computer.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [December 6, 2021, 8:43pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/7 "2021-12-06T20:43:54Z")

</div>

Even if it happens to work right now, that’s actually an accidental artifact of the current implementation. A (near) future version of dfx will actually verify compatibility before upgrade, and the latest releases of Motoko also let you manually check compatibility of stable variable signatures (and use a candid tool (didc) to check the public interfaces.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [December 6, 2021, 11:53pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/8 "2021-12-06T23:53:58Z")

</div>

Wait, why? Based on the definition of “stable-compatible” in the link you provided, OP’s new type is compatible with her old type. I thought adding new fields are OK…

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [December 7, 2021, 4:56pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/9 "2021-12-07T16:56:23Z")

</div>

> A stable signature `<stab-sig1>` is _stable-compatible_ with signature `<stab-sig2>` , if, and only,
> 
> - every immutable field `stable <id> : T` in `<stab-sig1>` has a matching field `stable <id> : U` in `<stab-sig2>` with `T <: U` .
> - every mutable field `stable var <id> : T` in `<stab-sig1>` has a matching field `stable var <id> : U` in `<stab-sig2>` with `T <: U` .
> 
> Note that `<stab-sig2>` may contain additional fields. Typically, `<stab-sig1>` is the signature of an older version while `<stab-sig2>` is the signature of a newer version.
> 
> The subtyping condition on stable fields ensures that the final value of some field can be consumed as the initial value of that field in the upgraded code.

Adding new stable variable declarations is ok (the initializer will be used to determine the initial value of a new stable variable).

Adding fields to the record type of a single stable variable is not ok (a record can only lose fields when moving to a super-type).

More generally, changing the type of the variable to something which isn’t a Motoko super-type of the original type, is not ok.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [December 7, 2021, 6:13pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/10 "2021-12-07T18:13:54Z")

</div>

Do you have thoughts on the pattern I suggested?

---

<div class="post-metadata">

**Author:** ![Ashley](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Ashley](https://forum.dfinity.org/u/Ashley)\
**Post date:** [December 7, 2021, 7:35pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/11 "2021-12-07T19:35:57Z")

</div>

Thanks everyone for your comments, but I’m now very confused. What is the best method to add new fields to a type in Motoko?

In real world apps, we are constantly iterating and changing.

What exactly should I do to add new fields to type that is also future proof?

Please keep it Motoko specific. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [December 9, 2021, 8:07am UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/12 "2021-12-09T08:07:05Z")

</div>

This post proposes one solution. Not super elegant but safe.

> [@Loosing data when upgrading records](https://forum.dfinity.org/t/loosing-data-when-upgrading-records/8672):
>
> This is a barebones example: actor Test { type Entry = { desc: Text; phone: Text; }; stable var boo: ?Entry = null; public shared func set() : async () { boo := ?{desc="Some"; phone="12345678"}; }; public query func get() : async ?Entry { return boo; }; }; I know what you are thinking “Nobody does that! You! Time-wasting edgecaser” So I have also made a real-world example too. Something used in token implementations: [https://m7sm4-2iaaa-aaaab-qabra-cai.raw.…](https://m7sm4-2iaaa-aaaab-qabra-cai.raw.ic0.app/?tag=1583117559)

The idea is to define a new stable variable with the richer type and initialize it from the old one, inserting appropriate values for new fields.

The second link above on Compatibility has a similar example at the very end.

If you don’t like having to come up with new names, you can define your state variable as a variant type and add a new variant whenever you need to change the type. The Life example on the GH dfinity/examples repo does something like that.

---

<div class="post-metadata">

**Author:** ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)\
**Post date:** [September 6, 2023, 3:55pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/13 "2023-09-06T15:55:44Z")

</div>

Hi Claudio,  
Is there a solution to this? I need to update some types and don’t want to risk losing data but can’t find anything I can follow to ensure this doesn’t happen?

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [September 7, 2023, 12:48pm UTC](https://forum.dfinity.org/t/i-want-to-add-a-bunch-new-optional-properties-members-to-types-will-i-lose-my-data-dsocial/9364/14 "2023-09-07T12:48:53Z")

</div>

The main thing is to not ignore the warning produced by dfx.

Generally, there’s more changes that are allowed to the external interface than are allowed to the types of stable variables.

Have you read this?

> **[Verifying upgrade compatibility | Internet Computer](https://internetcomputer.org/docs/current/motoko/main/compatibility)**
>
> Goal

Generally, the external Candid interface of a Motoko canister can evolve to a subtype (you can add methods, with more informative results and less informative arguments).

For stable variables, you can evolve their Motoko types to Motoko super-types and add new stable variables. If you are changing the type of a stable variable to something other than a plain supertype, your best bet is to introduce a new stable variable, initialized from the old one, by a transformation that adds the optional fields etc.

Test the upgrade locally on a throw-away canister before doing it for real on mainnet.
