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

Test for changeColumn breaking on composite primary keys #283

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ matrix.node-version }}
cache: yarn
- run: /bin/bash ./dev/prepare-ci.sh
- name: Execute SSCCE
run: yarn run _test
Expand All @@ -45,7 +44,6 @@ jobs:
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ matrix.node-version }}
cache: yarn
- run: /bin/bash ./dev/prepare-ci.sh
- run: yarn start-postgres-${{ matrix.postgres-version }}
- name: Execute SSCCE
Expand All @@ -66,7 +64,6 @@ jobs:
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ matrix.node-version }}
cache: yarn
- run: /bin/bash ./dev/prepare-ci.sh
- run: yarn start-${{ matrix.dialect }}-${{ matrix.database-version }}
- name: Execute SSCCE
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test.sqlite2
*.sublime*
package-lock.json
pnpm-lock.yaml
yarn.lock

.nyc_output
coverage-*
Expand Down
70 changes: 70 additions & 0 deletions dev/ci-db-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,73 @@ export const CiDbConfigs = {
minifyAliases: Boolean(env.SEQ_PG_MINIFY_ALIASES),
},
};

export const CiDbConfigsV7 = {
mssql: {
server: env.SEQ_MSSQL_HOST || env.SEQ_HOST || 'localhost',
authentication: {
type: 'default',
options: {
userName: env.SEQ_MSSQL_USER || env.SEQ_USER || 'SA',
password: env.SEQ_MSSQL_PW || env.SEQ_PW || 'Password12!',
},
},
port: env.SEQ_MSSQL_PORT || env.SEQ_PORT || 22_019,
database: env.SEQ_MSSQL_DB || env.SEQ_DB || 'sequelize_test',
encrypt: false,
requestTimeout: 25_000,
pool: {
max: Number(env.SEQ_MSSQL_POOL_MAX || env.SEQ_POOL_MAX || 5),
idle: Number(env.SEQ_MSSQL_POOL_IDLE || env.SEQ_POOL_IDLE || 3000),
},
},

mysql: {
database: env.SEQ_MYSQL_DB || env.SEQ_DB || 'sequelize_test',
user: env.SEQ_MYSQL_USER || env.SEQ_USER || 'sequelize_test',
password: env.SEQ_MYSQL_PW || env.SEQ_PW || 'sequelize_test',
host: env.MYSQL_PORT_3306_TCP_ADDR || env.SEQ_MYSQL_HOST || env.SEQ_HOST || '127.0.0.1',
port: env.MYSQL_PORT_3306_TCP_PORT || env.SEQ_MYSQL_PORT || env.SEQ_PORT || 20_057,
pool: {
max: Number(env.SEQ_MYSQL_POOL_MAX || env.SEQ_POOL_MAX || 5),
idle: Number(env.SEQ_MYSQL_POOL_IDLE || env.SEQ_POOL_IDLE || 3000),
},
},

snowflake: {
username: env.SEQ_SNOWFLAKE_USER || env.SEQ_USER || 'root',
password: env.SEQ_SNOWFLAKE_PW || env.SEQ_PW || '',
database: env.SEQ_SNOWFLAKE_DB || env.SEQ_DB || 'sequelize_test',
account: env.SEQ_SNOWFLAKE_ACCOUNT || env.SEQ_ACCOUNT || 'sequelize_test',
role: env.SEQ_SNOWFLAKE_ROLE || env.SEQ_ROLE || 'role',
warehouse: env.SEQ_SNOWFLAKE_WH || env.SEQ_WH || 'warehouse',
schema: env.SEQ_SNOWFLAKE_SCHEMA || env.SEQ_SCHEMA || '',
},

mariadb: {
database: env.SEQ_MARIADB_DB || env.SEQ_DB || 'sequelize_test',
user: env.SEQ_MARIADB_USER || env.SEQ_USER || 'sequelize_test',
password: env.SEQ_MARIADB_PW || env.SEQ_PW || 'sequelize_test',
host: env.MARIADB_PORT_3306_TCP_ADDR || env.SEQ_MARIADB_HOST || env.SEQ_HOST || '127.0.0.1',
port: env.MARIADB_PORT_3306_TCP_PORT || env.SEQ_MARIADB_PORT || env.SEQ_PORT || 21_103,
pool: {
max: Number(env.SEQ_MARIADB_POOL_MAX || env.SEQ_POOL_MAX || 5),
idle: Number(env.SEQ_MARIADB_POOL_IDLE || env.SEQ_POOL_IDLE || 3000),
},
},

sqlite: {},

postgres: {
database: env.SEQ_PG_DB || env.SEQ_DB || 'sequelize_test',
user: env.SEQ_PG_USER || env.SEQ_USER || 'sequelize_test',
password: env.SEQ_PG_PW || env.SEQ_PW || 'sequelize_test',
host: env.POSTGRES_PORT_5432_TCP_ADDR || env.SEQ_PG_HOST || env.SEQ_HOST || '127.0.0.1',
port: env.POSTGRES_PORT_5432_TCP_PORT || env.SEQ_PG_PORT || env.SEQ_PORT || 23_010,
pool: {
max: Number(env.SEQ_PG_POOL_MAX || env.SEQ_POOL_MAX || 5),
idle: Number(env.SEQ_PG_POOL_IDLE || env.SEQ_POOL_IDLE || 3000),
},
minifyAliases: Boolean(env.SEQ_PG_MINIFY_ALIASES),
},
};
6 changes: 3 additions & 3 deletions dev/create-sequelize-instance.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Options as Sequelize6Options } from 'sequelize';
import { Sequelize as Sequelize6 } from 'sequelize';
import type { Options as Sequelize7Options, Sequelize as Sequelize7 } from '@sequelize/core';
import type { Options as Sequelize7Options, Sequelize as Sequelize7, AbstractDialect } from '@sequelize/core';
import { wrapOptions } from './wrap-options';

export function createSequelize6Instance(options?: Sequelize6Options): Sequelize6 {
return new Sequelize6(wrapOptions(options));
}

export function createSequelize7Instance(options?: Sequelize7Options): Sequelize7 {
export function createSequelize7Instance(options?: Omit<Sequelize7Options<AbstractDialect<object, object>>, "dialect">): Sequelize7 {
// not compatible with node 10
const { Sequelize: Sequelize7Constructor } = require('@sequelize/core');
// @ts-expect-error -- wrapOptions expect sequelize 6.
return new Sequelize7Constructor(wrapOptions(options));
return new Sequelize7Constructor(wrapOptions(options, true));
}
2 changes: 1 addition & 1 deletion dev/mssql/latest/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
volumes:
- mssql-latest:/var/opt/mssql
healthcheck:
test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "localhost", "-U", "SA", "-P", "Password12!", "-l", "30", "-Q", "SELECT 1"]
test: ["CMD", "/opt/mssql-tools18/bin/sqlcmd", "-S", "localhost", "-No", "-U", "SA", "-P", "Password12!", "-l", "30", "-Q", "SELECT 1"]
interval: 3s
timeout: 1s
retries: 10
Expand Down
2 changes: 1 addition & 1 deletion dev/mssql/latest/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ docker compose -p sequelize-mssql-latest up -d
./../../wait-until-healthy.sh sequelize-mssql-latest

docker exec sequelize-mssql-latest \
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Password12!" -Q "CREATE DATABASE sequelize_test; ALTER DATABASE sequelize_test SET READ_COMMITTED_SNAPSHOT ON;"
/opt/mssql-tools18/bin/sqlcmd -S localhost -No -U SA -P "Password12!" -Q "CREATE DATABASE sequelize_test; ALTER DATABASE sequelize_test SET READ_COMMITTED_SNAPSHOT ON;"

DIALECT=mssql ts-node ../../check-connection.ts
12 changes: 6 additions & 6 deletions dev/prepare-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
yarn install --ignore-engines;

if [ "$DIALECT" = "postgres" ]; then
yarn add pg@^8 pg-hstore@^2 pg-types@^2 --ignore-engines;
yarn add pg@^8 pg-hstore@^2 pg-types@^2 @sequelize/postgres --ignore-engines;
elif [ "$DIALECT" = "postgres-native" ]; then
yarn add pg@^8 pg-hstore@^2 pg-types@^2 pg-native --ignore-engines;
yarn add pg@^8 pg-hstore@^2 pg-types@^2 pg-native @sequelize/postgres --ignore-engines;
elif [ "$DIALECT" = "mysql" ]; then
yarn add mysql2@^2 --ignore-engines;
yarn add mysql2@^2 @sequelize/mysql --ignore-engines;
elif [ "$DIALECT" = "mariadb" ]; then
yarn add mariadb@^2 --ignore-engines;
yarn add mariadb@^2 @sequelize/mariadb --ignore-engines;
elif [ "$DIALECT" = "sqlite" ]; then
yarn add sqlite3@^5 --ignore-engines;
yarn add sqlite3@^5 @sequelize/sqlite3 --ignore-engines;
elif [ "$DIALECT" = "mssql" ]; then
yarn add tedious@^8 --ignore-engines;
yarn add tedious@^8 @sequelize/mssql --ignore-engines;
fi
9 changes: 3 additions & 6 deletions dev/wrap-options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import defaults from 'lodash/defaults.js';
import { CiDbConfigs } from './ci-db-configs';
import { CiDbConfigs, CiDbConfigsV7 } from './ci-db-configs';
import { log } from './logging';
import type { Dialect, Options } from 'sequelize';

export function wrapOptions(options: Options = {}) {
export function wrapOptions(options: Options = {}, v7 = false) {
if (!process.env.DIALECT) {
throw new Error('Dialect is not defined! Aborting.');
}
Expand All @@ -14,7 +14,7 @@ export function wrapOptions(options: Options = {}) {
// this fails in the CI due to mismatch between Sequelize 6 & 7. Should be resolved once we drop Sequelize 6.
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore
const config = CiDbConfigs[dialect];
const config = v7 ? CiDbConfigsV7[dialect] : CiDbConfigs[dialect];

options.dialect = dialect;
if (isPostgresNative) {
Expand All @@ -26,8 +26,5 @@ export function wrapOptions(options: Options = {}) {
...config,
});

// @ts-expect-error
options.__isOptionsObject__ = true;

return options;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"cross-env": "^7",
"fs-jetpack": "^4",
"sequelize": "^6",
"@sequelize/core": "alpha",
"@sequelize/core": "latest",
"sinon": "^13",
"sinon-chai": "^3"
},
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ You'll need to install the `pg` package and have a postgres database running.

```shell
# Do this only once.
npm install pg
npm install pg @sequelize/postgres
# or
npm install pg-native
npm install pg-native @sequelize/postgres

# if you need to use DataTypes.HSTORE, you also need this dependency
npm install pg-hstore
Expand All @@ -76,7 +76,7 @@ npm run stop:postgres

```shell
# Do this only once.
npm install mariadb
npm install mariadb @sequelize/mariadb

# Start the mariadb database using docker.
# Requires docker. You can also run your own database if you prefer.
Expand All @@ -93,7 +93,7 @@ npm run stop:mariadb

```shell
# Do this only once.
npm install mysql2
npm install mysql2 @sequelize/mysql

# Start the mysql database using docker.
# Requires docker. You can also run your own database if you prefer.
Expand All @@ -110,7 +110,7 @@ npm run stop:mysql

```shell
# Do this only once.
npm install tedious
npm install tedious @sequelize/mssql

# Start the mssql database using docker.
# Requires docker. You can also run your own database if you prefer.
Expand Down
41 changes: 0 additions & 41 deletions src/sscce-sequelize-6.ts

This file was deleted.

39 changes: 26 additions & 13 deletions src/sscce-sequelize-7.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { CreationOptional, DataTypes, DialectName, InferAttributes, InferCreationAttributes, Model } from '@sequelize/core';
import { Attribute, PrimaryKey } from '@sequelize/core/decorators-legacy';
import { createSequelize7Instance } from '../dev/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
Expand All @@ -23,21 +23,34 @@ export async function run() {
});

class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
declare id: CreationOptional<number>;

@Attribute(DataTypes.TEXT)
@NotNull
@Attribute(DataTypes.STRING(100))
@PrimaryKey
declare name: string;

@Attribute(DataTypes.STRING(100))
@PrimaryKey
declare rank: string;

@Attribute(DataTypes.STRING(100))
declare role: CreationOptional<string>;
}

sequelize.addModels([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);
// Inserting two entries with different name and rank succeeds
await expect(Foo.create({ name: "fish", rank: "novice" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "fish", rank: "expert" })).to.eventually.be.fulfilled;

// Inserting two entries with same name and rank fails
await expect(Foo.create({ name: "cat", rank: "expert" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "cat", rank: "expert" })).to.eventually.be.rejected;

// Altering an unrelated column should not change the above behaviour
await Foo.truncate();
await sequelize.queryInterface.changeColumn("Foos", "role", { type: DataTypes.TEXT });
await expect(Foo.create({ name: "bear", rank: "novice" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "bear", rank: "expert" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "dog", rank: "expert" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "dog", rank: "expert" })).to.eventually.be.rejected;
}
Loading
Loading