# Database Migration

**URL:** <https://forum.heroiclabs.com/t/database-migration/4471>\
**Category:** Runtime Framework\
**Tags:** typescript, golang, server-framework\
**Created:** [October 16, 2023, 7:09pm UTC](https://forum.heroiclabs.com/t/database-migration/4471 "2023-10-16T19:09:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albertcleetus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/albertcleetus/32/603_2.png) [@Albertcleetus](https://forum.heroiclabs.com/u/Albertcleetus)\
**Post date:** [October 16, 2023, 7:09pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/1 "2023-10-16T19:09:50Z")

</div>

How add database migration,(add new field to existing collection in data base need to take effect every user new and old).  
for example - after the game launch in production.  
As the part of updation of game i need to add more fields to existing character collection storage - like new “character” to the game.  
it need to take effect in all users in databse, new and old too…  
how will i do this, please help, i stuck in this for 3 months, Please help

we already created the database objects, also doing data management and crates opening and giving reward from these collections of data.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/1/16c06f4aad99848afc05f56fa4db0ba8312f07c0.png)

@novabyte @sesposito @ftkg

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 17, 2023, 2:18pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/2 "2023-10-17T14:18:35Z")

</div>

@Albertcleetus to clarify, you’d like to add new storage objects to all existing accounts?

---

<div class="post-metadata">

**Author:** ![Albertcleetus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/albertcleetus/32/603_2.png) [@Albertcleetus](https://forum.heroiclabs.com/u/Albertcleetus)\
**Post date:** [October 17, 2023, 6:49pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/3 "2023-10-17T18:49:10Z")

</div>

yes , that’s the thing. please help @sesposito

---

<div class="post-metadata">

**Author:** ![Albertcleetus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/albertcleetus/32/603_2.png) [@Albertcleetus](https://forum.heroiclabs.com/u/Albertcleetus)\
**Post date:** [October 20, 2023, 2:45pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/4 "2023-10-20T14:45:14Z")

</div>

@sesposito did you need any code screenshot or our system setup?

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 20, 2023, 3:09pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/5 "2023-10-20T15:09:35Z")

</div>

@Albertcleetus the best way to do this would be player-by-player instead of all the users.  
Use an [after hook](https://heroiclabs.com/docs/nakama/server-framework/introduction/hooks/#after-hooks) on your authentication method call that checks if the storage objects exist and if not, creates them for the user.

---

<div class="post-metadata">

**Author:** ![Albertcleetus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/albertcleetus/32/603_2.png) [@Albertcleetus](https://forum.heroiclabs.com/u/Albertcleetus)\
**Post date:** [October 21, 2023, 9:56am UTC](https://forum.heroiclabs.com/t/database-migration/4471/6 "2023-10-21T09:56:55Z")

</div>

you telling every time login a user, check that user had this collection or object in collection, if it’s not, create for it.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 23, 2023, 1:46pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/7 "2023-10-23T13:46:35Z")

</div>

That’s correct. This means you only need to perform the work of migrating data for active players and is our recommended approach.

---

<div class="post-metadata">

**Author:** ![Albertcleetus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/albertcleetus/32/603_2.png) [@Albertcleetus](https://forum.heroiclabs.com/u/Albertcleetus)\
**Post date:** [October 24, 2023, 7:41am UTC](https://forum.heroiclabs.com/t/database-migration/4471/8 "2023-10-24T07:41:55Z")

</div>

Can you share any Database migration script example here. please @sesposito

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 24, 2023, 4:18pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/9 "2023-10-24T16:18:57Z")

</div>

Using TS as an example, you’d do something like this:

```auto
function InitModule(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
    initializer.registerAfterAuthenticateEmail(addNewStorageObjectsFn);

    logger.info('JavaScript logic loaded.');
}

function addNewStorageObjectsFn(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, data: nkruntime.Session, request: nkruntime.AuthenticateEmailRequest): void {
    let user = nk.usersGetUsername([request.username]);
    if (user.length != 1) {
        throw Error('User not found');
    }

    try {
        let userId = user[0].userId;

        let objects = nk.storageRead([{
            collection: 'foo',
            key: 'bar',
            userId: userId,
        }]);

        if (objects.length === 0) {
            // Object not found, create it

            let newObject = {
                food: ['apple', 'pear'],
            };     

            nk.storageWrite([{
                collection: 'foo',
                key: 'baz',
                userId: userId,
                value: newObject,
            }]);
        }
    } catch(error) {
        logger.error('Error: %v', error);
    }
}

```

For further examples pelase check out our [project template](https://github.com/heroiclabs/nakama-project-template/).

---

<div class="post-metadata">

**Author:** ![Albertcleetus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/albertcleetus/32/603_2.png) [@Albertcleetus](https://forum.heroiclabs.com/u/Albertcleetus)\
**Post date:** [October 24, 2023, 7:57pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/10 "2023-10-24T19:57:17Z")

</div>

@sesposito Thank you so much , I going to integrate and let you know

---

<div class="post-metadata">

**Author:** ![Albertcleetus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/albertcleetus/32/603_2.png) [@Albertcleetus](https://forum.heroiclabs.com/u/Albertcleetus)\
**Post date:** [November 20, 2023, 12:44pm UTC](https://forum.heroiclabs.com/t/database-migration/4471/11 "2023-11-20T12:44:44Z")

</div>

Yes ,its worked my database migration work Completed , its perfect @sesposito
