-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate tableExists to typescript (#16320)
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
- Loading branch information
Showing
18 changed files
with
178 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/core/test/integration/query-interface/table-exists.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { expect } from 'chai'; | ||
import { DataTypes } from '@sequelize/core'; | ||
import { sequelize } from '../support'; | ||
|
||
const queryInterface = sequelize.queryInterface; | ||
|
||
describe('QueryInterface#tableExists', () => { | ||
describe('Without schema', () => { | ||
beforeEach(async () => { | ||
await queryInterface.createTable('levels', { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return true if table exists', async () => { | ||
const exists = await queryInterface.tableExists('levels'); | ||
expect(exists).to.be.true; | ||
}); | ||
|
||
it('should return false if table does not exist', async () => { | ||
const exists = await queryInterface.tableExists('actors'); | ||
expect(exists).to.be.false; | ||
}); | ||
}); | ||
|
||
if (sequelize.dialect.supports.schemas) { | ||
describe('With schema', () => { | ||
beforeEach(async () => { | ||
await queryInterface.createSchema('archive'); | ||
|
||
await queryInterface.createTable({ tableName: 'levels', schema: 'archive' }, { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return true if table exists', async () => { | ||
const exists = await queryInterface.tableExists({ tableName: 'levels', schema: 'archive' }); | ||
expect(exists).to.be.true; | ||
}); | ||
|
||
it('should return false if table does not exist', async () => { | ||
const exists = await queryInterface.tableExists({ tableName: 'actors', schema: 'archive' }); | ||
expect(exists).to.be.false; | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/core/test/unit/query-generator/table-exists-query.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { createSequelizeInstance, expectsql, sequelize } from '../../support'; | ||
|
||
const dialect = sequelize.dialect; | ||
|
||
describe('QueryGenerator#tableExistsQuery', () => { | ||
const queryGenerator = sequelize.queryGenerator; | ||
const defaultSchema = dialect.getDefaultSchema(); | ||
|
||
it('produces a table exists query for a table', () => { | ||
expectsql(() => queryGenerator.tableExistsQuery('myTable'), { | ||
default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = '${defaultSchema}'`, | ||
db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = '${defaultSchema}'`, | ||
ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = CURRENT SCHEMA`, | ||
mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'${defaultSchema}'`, | ||
sqlite: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'myTable'`, | ||
}); | ||
}); | ||
|
||
it('produces a table exists query for a model', () => { | ||
const MyModel = sequelize.define('MyModel', {}); | ||
|
||
expectsql(() => queryGenerator.tableExistsQuery(MyModel), { | ||
default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'MyModels' AND TABLE_SCHEMA = '${defaultSchema}'`, | ||
db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'MyModels' AND TABSCHEMA = '${defaultSchema}'`, | ||
ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'MyModels' AND TABLE_SCHEMA = CURRENT SCHEMA`, | ||
mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'MyModels' AND TABLE_SCHEMA = N'${defaultSchema}'`, | ||
sqlite: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'MyModels'`, | ||
}); | ||
}); | ||
|
||
it('produces a table exists query for a table and schema', () => { | ||
expectsql(() => queryGenerator.tableExistsQuery({ tableName: 'myTable', schema: 'mySchema' }), { | ||
default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`, | ||
db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = 'mySchema'`, | ||
ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`, | ||
mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'mySchema'`, | ||
sqlite: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'mySchema.myTable'`, | ||
}); | ||
}); | ||
|
||
it('produces a table exists query for a table and default schema', () => { | ||
expectsql(() => queryGenerator.tableExistsQuery({ tableName: 'myTable', schema: dialect.getDefaultSchema() }), { | ||
default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = '${defaultSchema}'`, | ||
db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = '${defaultSchema}'`, | ||
ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = CURRENT SCHEMA`, | ||
mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'${defaultSchema}'`, | ||
sqlite: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'myTable'`, | ||
}); | ||
}); | ||
|
||
it('produces a table exists query for a table and globally set schema', () => { | ||
const sequelizeSchema = createSequelizeInstance({ schema: 'mySchema' }); | ||
const queryGeneratorSchema = sequelizeSchema.queryGenerator; | ||
|
||
expectsql(() => queryGeneratorSchema.tableExistsQuery('myTable'), { | ||
default: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`, | ||
db2: `SELECT TABNAME FROM SYSCAT.TABLES WHERE TABNAME = 'myTable' AND TABSCHEMA = 'mySchema'`, | ||
ibmi: `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TABLE_NAME = 'myTable' AND TABLE_SCHEMA = 'mySchema'`, | ||
mssql: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = N'myTable' AND TABLE_SCHEMA = N'mySchema'`, | ||
sqlite: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'mySchema.myTable'`, | ||
}); | ||
}); | ||
|
||
it('produces a table exists query for a table with schema and custom delimiter argument', () => { | ||
// This test is only relevant for dialects that do not support schemas | ||
if (dialect.supports.schemas) { | ||
return; | ||
} | ||
|
||
expectsql(() => queryGenerator.tableExistsQuery({ tableName: 'myTable', schema: 'mySchema', delimiter: 'custom' }), { | ||
sqlite: `SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'mySchemacustommyTable'`, | ||
}); | ||
}); | ||
}); |