From 357826b95d6b6a31b9457c1e23e6ce9ee030bb8f Mon Sep 17 00:00:00 2001 From: Jesper Sjovall Date: Wed, 11 Jan 2023 10:38:17 +0100 Subject: [PATCH] adding working example --- package.json | 3 +- src/sscce-sequelize-6.ts | 113 +++++++++++++++++++++++++++++++-------- src/sscce-sequelize-7.ts | 41 -------------- 3 files changed, 92 insertions(+), 65 deletions(-) delete mode 100644 src/sscce-sequelize-7.ts diff --git a/package.json b/package.json index 7b085ecf8..d14b79dff 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,15 @@ "type": "commonjs", "license": "MIT", "dependencies": { + "@sequelize/core": "^7.0.0-alpha.10", "chai": "^4", "chai-as-promised": "^7", "chai-datetime": "^1", "chalk": "^4.1.2", "cross-env": "^7", "fs-jetpack": "^4", + "pg": "^8.8.0", "sequelize": "^6", - "@sequelize/core": "^7.0.0-alpha.10", "sinon": "^13", "sinon-chai": "^3" }, diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index c90761b96..d34495f0f 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -1,41 +1,108 @@ -import { DataTypes, Model } from 'sequelize'; -import { createSequelize6Instance } from '../setup/create-sequelize-instance'; -import { expect } from 'chai'; -import sinon from 'sinon'; +import {DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize} from 'sequelize'; +import {createSequelize6Instance} from '../setup/create-sequelize-instance'; +import assert from "assert"; // if your issue is dialect specific, remove the dialects you don't need to test on. -export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb', 'postgres', 'postgres-native']); +export const testingOnDialects = new Set(['postgres']); // You can delete this file if you don't want your SSCCE to be tested against Sequelize 6 +class TestModel extends Model, InferCreationAttributes> { + declare readonly id: string; + declare readonly name: string; +} + +function initTestModel(sequelize: Sequelize) { + TestModel.init( + { + id: {type: DataTypes.UUID, primaryKey: true}, + name: {type: DataTypes.UUID, allowNull: false} + }, + { + sequelize: sequelize, + timestamps: false, + modelName: 'example', + indexes: [ + { + type: 'UNIQUE', + fields: ['id', 'name'] + } + ] + } + ); +} // Your SSCCE goes inside this function. export async function run() { - // This function should be used instead of `new Sequelize()`. - // It applies the config for your SSCCE to work on CI. + // Delete and then recreate schema so we know they are empty. const sequelize = createSequelize6Instance({ logQueryParameters: true, benchmark: true, + }); + await sequelize.query('DROP SCHEMA IF EXISTS public, test CASCADE'); + await sequelize.query('CREATE SCHEMA public'); + await sequelize.query('CREATE SCHEMA test'); + + // Instance 1, table will be created as excepted. + const sequelize1 = createSequelize6Instance({ + logQueryParameters: true, + benchmark: true, + schema: 'test', define: { - // For less clutter in the SSCCE - timestamps: false, - }, + schema: 'test' + } }); + initTestModel(sequelize1); + await sequelize1.sync({force: true}); - class Foo extends Model {} + // Instance 2, table will be created, but index is not created. + const sequelize2 = createSequelize6Instance({ + logQueryParameters: true, + benchmark: true, + schema: undefined, // eg schema "public" + define: { + schema: undefined, // eg schema "public" + } + }); + initTestModel(sequelize2); + await sequelize2.sync({force: true}) - Foo.init({ - name: DataTypes.TEXT, - }, { - sequelize, - modelName: 'Foo', + // Verify the results. + const index = await sequelize2.getQueryInterface().showIndex({ + tableName: 'examples', + schema: 'public', + delimiter: '.' }); - // You can use sinon and chai assertions directly in your SSCCE. - const spy = sinon.spy(); - sequelize.afterBulkSync(() => spy()); - await sequelize.sync({ force: true }); - expect(spy).to.have.been.called; + // Current workaround for the issue is in the "sequelize2" instead of "undefined" + // use the postgres default schema value "public", this will work as excepted. + // the issue from what I can see is when sequelize try to create the table in the sequelize2 + // the system detect an index with the same name already exists (eg the index belong to the "test" schema) + // and then do not create the index, even if we're talking about two different table in two different schemas. + + assert.deepStrictEqual(index, + [ + { + name: 'examples_id_name', + primary: false, + unique: true, + indkey: '1 2', + definition: 'CREATE UNIQUE INDEX examples_id_name ON public.examples USING btree (id, name)', + fields: [ + {attribute: 'id', collate: undefined, length: undefined, order: undefined}, + {attribute: 'name', collate: undefined, length: undefined, order: undefined} + ] + }, + { + name: 'examples_pkey', + primary: true, + unique: true, + indkey: '1', + definition: 'CREATE UNIQUE INDEX examples_pkey ON public.examples USING btree (id)', + fields: [ + {attribute: 'id', collate: undefined, length: undefined, order: undefined} + ] + } + ]); + - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); } diff --git a/src/sscce-sequelize-7.ts b/src/sscce-sequelize-7.ts deleted file mode 100644 index 861b9fdea..000000000 --- a/src/sscce-sequelize-7.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { DataTypes, Model } from '@sequelize/core'; -import { createSequelize7Instance } from '../setup/create-sequelize-instance'; -import { expect } from 'chai'; -import sinon from 'sinon'; - -// if your issue is dialect specific, remove the dialects you don't need to test on. -export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb', 'postgres', 'postgres-native']); - -// You can delete this file if you don't want your SSCCE to be tested against Sequelize 7 - -// Your SSCCE goes inside this function. -export async function run() { - // This function should be used instead of `new Sequelize()`. - // It applies the config for your SSCCE to work on CI. - const sequelize = createSequelize7Instance({ - logQueryParameters: true, - benchmark: true, - define: { - // For less clutter in the SSCCE - timestamps: false, - }, - }); - - class Foo extends Model {} - - Foo.init({ - name: DataTypes.TEXT, - }, { - sequelize, - modelName: 'Foo', - }); - - // You can use sinon and chai assertions directly in your SSCCE. - const spy = sinon.spy(); - sequelize.afterBulkSync(() => spy()); - await sequelize.sync({ force: true }); - expect(spy).to.have.been.called; - - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); -}