create-schema-query.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { sql } from '@sequelize/core';
  2. import { buildInvalidOptionReceivedError } from '@sequelize/core/_non-semver-use-at-your-own-risk_/utils/check.js';
  3. import { expectsql, getTestDialect, sequelize } from '../../support';
  4. const dialectName = getTestDialect();
  5. const notSupportedError = new Error(`Schemas are not supported in ${dialectName}.`);
  6. describe('QueryGenerator#createSchemaQuery', () => {
  7. const queryGenerator = sequelize.queryGenerator;
  8. it('produces a CREATE SCHEMA query in supported dialects', () => {
  9. expectsql(() => queryGenerator.createSchemaQuery('mySchema'), {
  10. default: 'CREATE SCHEMA [mySchema]',
  11. sqlite3: notSupportedError,
  12. });
  13. });
  14. it('supports the authorization option', () => {
  15. expectsql(() => queryGenerator.createSchemaQuery('mySchema', { authorization: 'myUser' }), {
  16. default: 'CREATE SCHEMA [mySchema] AUTHORIZATION [myUser]',
  17. sqlite3: notSupportedError,
  18. 'mariadb mysql snowflake': buildInvalidOptionReceivedError('createSchemaQuery', dialectName, [
  19. 'authorization',
  20. ]),
  21. });
  22. });
  23. it('supports the authorization option with a literal', () => {
  24. expectsql(
  25. () => queryGenerator.createSchemaQuery('mySchema', { authorization: sql`CURRENT USER` }),
  26. {
  27. default: 'CREATE SCHEMA [mySchema] AUTHORIZATION CURRENT USER',
  28. sqlite3: notSupportedError,
  29. 'mariadb mysql snowflake': buildInvalidOptionReceivedError(
  30. 'createSchemaQuery',
  31. dialectName,
  32. ['authorization'],
  33. ),
  34. },
  35. );
  36. });
  37. it('supports the charset option', () => {
  38. expectsql(() => queryGenerator.createSchemaQuery('mySchema', { charset: 'utf8mb4' }), {
  39. default: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, ['charset']),
  40. 'mysql mariadb': `CREATE SCHEMA \`mySchema\` DEFAULT CHARACTER SET 'utf8mb4'`,
  41. sqlite3: notSupportedError,
  42. });
  43. });
  44. it('supports the collate option', () => {
  45. expectsql(() => queryGenerator.createSchemaQuery('mySchema', { collate: 'en_US.UTF-8' }), {
  46. default: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, ['collate']),
  47. 'mysql mariadb': `CREATE SCHEMA \`mySchema\` DEFAULT COLLATE 'en_US.UTF-8'`,
  48. sqlite3: notSupportedError,
  49. });
  50. });
  51. it('supports the comment option', () => {
  52. expectsql(() => queryGenerator.createSchemaQuery('mySchema', { comment: 'myComment' }), {
  53. default: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, ['comment']),
  54. snowflake: `CREATE SCHEMA "mySchema" COMMENT 'myComment'`,
  55. sqlite3: notSupportedError,
  56. });
  57. });
  58. it('supports the ifNotExists option', () => {
  59. expectsql(() => queryGenerator.createSchemaQuery('mySchema', { ifNotExists: true }), {
  60. default: 'CREATE SCHEMA IF NOT EXISTS [mySchema]',
  61. 'db2 ibmi mssql': buildInvalidOptionReceivedError('createSchemaQuery', dialectName, [
  62. 'ifNotExists',
  63. ]),
  64. sqlite3: notSupportedError,
  65. });
  66. });
  67. it('supports the replace option', () => {
  68. expectsql(() => queryGenerator.createSchemaQuery('mySchema', { replace: true }), {
  69. default: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, ['replace']),
  70. 'mariadb snowflake': `CREATE OR REPLACE SCHEMA [mySchema]`,
  71. sqlite3: notSupportedError,
  72. });
  73. });
  74. it('supports specifying all possible combinations', () => {
  75. expectsql(
  76. () =>
  77. queryGenerator.createSchemaQuery('mySchema', {
  78. authorization: 'myUser',
  79. charset: 'utf8mb4',
  80. collate: 'en_US.UTF-8',
  81. comment: 'myComment',
  82. ifNotExists: true,
  83. replace: true,
  84. }),
  85. {
  86. default: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, [
  87. 'charset',
  88. 'collate',
  89. 'comment',
  90. 'ifNotExists',
  91. 'replace',
  92. ]),
  93. mariadb: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, [
  94. 'authorization',
  95. 'comment',
  96. ]),
  97. mysql: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, [
  98. 'authorization',
  99. 'comment',
  100. 'replace',
  101. ]),
  102. postgres: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, [
  103. 'charset',
  104. 'collate',
  105. 'comment',
  106. 'replace',
  107. ]),
  108. snowflake: buildInvalidOptionReceivedError('createSchemaQuery', dialectName, [
  109. 'authorization',
  110. 'charset',
  111. 'collate',
  112. ]),
  113. sqlite3: notSupportedError,
  114. },
  115. );
  116. });
  117. });