UNPKG

knex-migration-with-schema

Version:

[![npm version](https://badge.fury.io/js/knex-migration-with-schema.svg)](https://badge.fury.io/js/knex-migration-with-schema)

50 lines 3.24 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const knex_1 = __importDefault(require("knex")); const config_1 = require("../test/config"); const creator_1 = require("./creator"); describe('schema creator', () => { describe('given a knex connection and a schema name', () => { const knex = (0, knex_1.default)(config_1.config.knex); const schemaName = 'new_schema'; const checkIfSchemaExists = (schemaNameToValidate) => __awaiter(void 0, void 0, void 0, function* () { return !!(yield knex('information_schema.schemata').where({ schema_name: schemaNameToValidate }).first()); }); const dropSchema = (schemaNameToDrop) => __awaiter(void 0, void 0, void 0, function* () { return knex.raw(`DROP SCHEMA IF EXISTS "${schemaNameToDrop}" CASCADE`); }); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { yield dropSchema(schemaName); })); it('creates a new schema', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, creator_1.createSchema)({ knex, schemaName: schemaName }); (0, chai_1.expect)(yield checkIfSchemaExists(schemaName)).to.be.true; })); it('does not create a new schema if it already exists', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, creator_1.createSchema)({ knex, schemaName: schemaName }); yield (0, creator_1.createSchema)({ knex, schemaName: schemaName }); (0, chai_1.expect)(yield checkIfSchemaExists(schemaName)).to.be.true; })); describe('when trying to inject SQL through the schema name', () => { const sqlInjectionTry = `${schemaName}; SELECT 0x50 + 0x45;`; beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { yield dropSchema(sqlInjectionTry); })); it('creates the schema without executing any extra SQL code', () => __awaiter(void 0, void 0, void 0, function* () { const schemaCreationResult = yield (0, creator_1.createSchema)({ knex, schemaName: sqlInjectionTry }); (0, chai_1.expect)(schemaCreationResult.command).to.be.eql('CREATE'); (0, chai_1.expect)(yield checkIfSchemaExists(sqlInjectionTry)).to.be.true; })); }); }); }); //# sourceMappingURL=creator.test.js.map