Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question]: How can I create migrations with an index that is a Number #10

Open
brewencoded opened this issue Nov 15, 2024 · 3 comments

Comments

@brewencoded
Copy link

I have a schema:

{
   format: 'onetable:1.1.0',
   version: '0.0.1',
   indexes: {
      primary: { hash: 'pk', sort: 'sk' }
   },
   models: {
      Model: {
         pk: { type: String },
         sk: { type: Number }
      }
   } as const
};

My IaC code also defines this table:

{
         fields: {
            pk: 'string',
            sk: 'number'
         },
         primaryIndex: {
            partitionKey: 'pk',
            sortKey: 'sk'
         }
}

But OneTable Migrate wants to create:

{
   format: 'onetable:1.1.0',
   version: '0.0.1',
   indexes: {
      primary: { hash: 'pk', sort: 'sk' }
   },
   models: {
      _Migration: {
         pk: { type: String },
         sk: { type: String }
      }
   } as const
};

So I will end up getting an error:

One or more parameter values were invalid: Condition parameter type does not match schema type

Is is possible to have the _Migration model created on a GSI instead? Is there anything I can do to make migrations work given a String/Number Hash/Sort?

@mobsense
Copy link

I'm not sure if the underlying OneTable supports that use case either. Does OneTable work for you in that case?

It looks like OneTable expects the MigrationModel to have a string sort key.

@brewencoded
Copy link
Author

OneTable Works in 90% of my use cases, but it would be useful to have the flexibility to configure the Migration Model. I had a look in the code: https://github.com/sensedeep/dynamodb-onetable/blob/main/src/Schema.js#L154-L172

createMigrationModel() {
    let { indexes } = this;
    let primary = indexes.primary;
    let fields = (this.migrationModelFields = {
        [primary.hash]: { type: 'string', value: `${MigrationKey}` },
        date: { type: 'date', required: true },
        description: { type: 'string', required: true },
        path: { type: 'string', required: true },
        version: { type: 'string', required: true },
        status: { type: 'string' },
    });
    if (primary.sort) {
        fields[primary.sort] = { type: 'string', value: `${MigrationKey}:\${version}:\${date}` };
    }
    this.models[MigrationModel] = new Model(this.table, MigrationModel, { fields, indexes });
}

Correct me if I am wrong, but it looks like the migration model does not need a sort key to function. So perhaps we could have a configuration to omit it? Then it wouldn't matter if I had a Numeric sort key.

@dev-embedthis
Copy link
Contributor

It would depend on if migration and the cli modules are using the SK to query migrations by version or not.

If they are getting the migrations list and using the version and date properties, it may work. If they are constructing queries and constructing the SK manually, then it would not.

Feel free to hack and try. Would be a good use case to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants