Graphback: Database migration doesn't work with sqlite

Created on 20 Nov 2019  路  7Comments  路  Source: aerogear/graphback

It seems that SQLite doesn't convert automatically strings to integer, whenn I try to migrate the databse on sqlite I recive this error:

insert into `gb_schema_migrations` (`changes`, `id`, `model`, `sql_up`) values ('[{"type":"TYPE_ADDED","path":{"type":"Item"}}]', '1574260178473', '
  type Item {
    id: ID!
    title: String!
  }
', 'create table `item` (`id` integer not null primary key autoincrement, `title` text);') - SQLITE_CONSTRAINT: UNIQUE constraint failed: gb_schema_migrations.id

What am I doing:

    const knex = Knex({
      client: "sqlite3",
      connection: { filename: ":memory:" }
    });
    const dir = tmp.dirSync();
    const migrater = new UpdateDatabaseIfChanges(knex, dir.name);
    await migrater.init(this.schema.getSchemaText());

My workaround packages/graphql-migrations/src/migrations/KnexMigrationManager.ts

  public async createMigration(migration: SchemaMigration): Promise<void> {
    try {
      // convert id to integer
      (migration as any).id = parseInt(migration.id);
      // tslint:disable-next-line: await-promise
      await this.db.table(this.tables.migrations).insert(migration);
    } catch (err) {
      handleError(err);
    }

    return Promise.resolve();
  }

Graphback: 0.10.0-dev2

Most helpful comment

@wtrocki do you mean the old method?

@b1zzu

https://github.com/aerogear/graphback/tree/master/packages/graphql-migrations#drop-and-recreate-database-every-time

const databaseInitializationStrategy = new DropCreateDatabaseAlways('pg', db);

You can use DropCreateDatabase initialization strategy which uses the old engine to create the database talbes, relationships etc.

All 7 comments

@b1zzu Thank you so much. Please use CreateDatabase strategy for the moment.
We are going to address this issue.

@wtrocki do you mean the old method?

@wtrocki do you mean the old method?

@b1zzu

https://github.com/aerogear/graphback/tree/master/packages/graphql-migrations#drop-and-recreate-database-every-time

const databaseInitializationStrategy = new DropCreateDatabaseAlways('pg', db);

You can use DropCreateDatabase initialization strategy which uses the old engine to create the database talbes, relationships etc.

The DropCreateDatabaseAlways works fine

@b1zzu https://github.com/aerogear/graphback/pull/503 should have fixed this now.

Thanks, @craicoverflow !!.. but I don't think I will going to use it for now in testx because I feel that DropCreateDatabaseAlways is more appropriate given the fact that it doesn't require a diretory where to store the migrations data

Was this page helpful?
0 / 5 - 0 ratings