create-database-query.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { buildInvalidOptionReceivedError } from '@sequelize/core/_non-semver-use-at-your-own-risk_/utils/check.js';
  2. import { removeUndefined } from '@sequelize/core/_non-semver-use-at-your-own-risk_/utils/object.js';
  3. import { expectsql, getTestDialect, sequelize } from '../../support';
  4. const dialectName = getTestDialect();
  5. const notSupportedError = new Error(`Databases are not supported in ${dialectName}.`);
  6. describe('QueryGenerator#createDatabaseQuery', () => {
  7. const queryGenerator = sequelize.queryGenerator;
  8. it('produces a CREATE DATABASE query in supported dialects', () => {
  9. expectsql(() => queryGenerator.createDatabaseQuery('myDatabase'), {
  10. default: notSupportedError,
  11. postgres: 'CREATE DATABASE "myDatabase"',
  12. snowflake: 'CREATE DATABASE IF NOT EXISTS "myDatabase"',
  13. mssql: `IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = N'myDatabase' ) CREATE DATABASE [myDatabase]`,
  14. });
  15. });
  16. it('supports the collate option', () => {
  17. expectsql(() => queryGenerator.createDatabaseQuery('myDatabase', { collate: 'en_US.UTF-8' }), {
  18. default: notSupportedError,
  19. postgres: `CREATE DATABASE "myDatabase" LC_COLLATE = 'en_US.UTF-8'`,
  20. snowflake: buildInvalidOptionReceivedError('createDatabaseQuery', dialectName, ['collate']),
  21. mssql: `IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = N'myDatabase' ) CREATE DATABASE [myDatabase] COLLATE N'en_US.UTF-8'`,
  22. });
  23. });
  24. it('supports the encoding option', () => {
  25. expectsql(() => queryGenerator.createDatabaseQuery('myDatabase', { encoding: 'UTF8' }), {
  26. default: notSupportedError,
  27. 'mssql snowflake': buildInvalidOptionReceivedError('createDatabaseQuery', dialectName, [
  28. 'encoding',
  29. ]),
  30. postgres: `CREATE DATABASE "myDatabase" ENCODING = 'UTF8'`,
  31. });
  32. });
  33. it('supports the ctype option', () => {
  34. expectsql(() => queryGenerator.createDatabaseQuery('myDatabase', { ctype: 'zh_TW.UTF-8' }), {
  35. default: notSupportedError,
  36. 'mssql snowflake': buildInvalidOptionReceivedError('createDatabaseQuery', dialectName, [
  37. 'ctype',
  38. ]),
  39. postgres: `CREATE DATABASE "myDatabase" LC_CTYPE = 'zh_TW.UTF-8'`,
  40. });
  41. });
  42. it('supports the template option', () => {
  43. expectsql(() => queryGenerator.createDatabaseQuery('myDatabase', { template: 'template0' }), {
  44. default: notSupportedError,
  45. 'mssql snowflake': buildInvalidOptionReceivedError('createDatabaseQuery', dialectName, [
  46. 'template',
  47. ]),
  48. postgres: `CREATE DATABASE "myDatabase" TEMPLATE = 'template0'`,
  49. });
  50. });
  51. it('supports the charset option', () => {
  52. expectsql(() => queryGenerator.createDatabaseQuery('myDatabase', { charset: 'utf8mb4' }), {
  53. default: notSupportedError,
  54. 'mssql postgres snowflake': buildInvalidOptionReceivedError(
  55. 'createDatabaseQuery',
  56. dialectName,
  57. ['charset'],
  58. ),
  59. });
  60. });
  61. it('supports combining all options', () => {
  62. const optionSupport = {
  63. collate: ['postgres', 'mssql'],
  64. encoding: ['postgres'],
  65. ctype: ['postgres'],
  66. template: ['postgres'],
  67. };
  68. const config = removeUndefined({
  69. collate: optionSupport.collate.includes(dialectName) ? 'en_US.UTF-8' : undefined,
  70. encoding: optionSupport.encoding.includes(dialectName) ? 'UTF8' : undefined,
  71. ctype: optionSupport.ctype.includes(dialectName) ? 'zh_TW.UTF-8' : undefined,
  72. template: optionSupport.template.includes(dialectName) ? 'template0' : undefined,
  73. });
  74. expectsql(() => queryGenerator.createDatabaseQuery('myDatabase', config), {
  75. default: notSupportedError,
  76. postgres: `CREATE DATABASE "myDatabase" ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'zh_TW.UTF-8' TEMPLATE = 'template0'`,
  77. snowflake: `CREATE DATABASE IF NOT EXISTS "myDatabase"`,
  78. mssql: `IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = N'myDatabase') CREATE DATABASE [myDatabase] COLLATE N'en_US.UTF-8'`,
  79. });
  80. });
  81. });