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)

146 lines 6.79 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const knex_1 = require("knex"); const config_1 = require("../test/config"); const creator_1 = require("./creator"); const migration_1 = require("./migration"); describe('schema migration', () => { const schemaName = 'new_schema'; const knexConnection = (0, knex_1.knex)(config_1.config.knex); const getSchemaTables = () => __awaiter(void 0, void 0, void 0, function* () { return (yield knexConnection.raw(` SELECT table_name FROM information_schema.tables WHERE table_schema = '${schemaName}' AND table_type = 'BASE TABLE';`)).rows; }); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { yield knexConnection.raw(`DROP SCHEMA IF EXISTS "${schemaName}" CASCADE`); yield (0, creator_1.createSchema)({ knex: knexConnection, schemaName }); })); describe('using migrations object', () => { it('creates knex migrations and users table for a given schema', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, migration_1.executeSchemaMigration)({ knex: knexConnection, schemaName, migrations: migrationsFixture }); const tables = yield getSchemaTables(); (0, chai_1.expect)(tables).to.have.length(3); (0, chai_1.expect)(tables).to.have.deep.members([ { table_name: 'knex_migrations', }, { table_name: 'knex_migrations_lock', }, { table_name: 'users', }, ]); })); it('accepts migrations for new tables and saves the keys in the knex migrations table', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, migration_1.executeSchemaMigration)({ knex: knexConnection, schemaName, migrations: migrationsFixture }); yield (0, migration_1.executeSchemaMigration)({ knex: knexConnection, schemaName, migrations: Object.assign(Object.assign({}, migrationsFixture), { tokens: tokensMigrationFixture }), }); const tables = yield getSchemaTables(); const knexMigrations = (yield knexConnection(`${schemaName}.knex_migrations`)).map(({ name }) => name); (0, chai_1.expect)(tables).to.have.length(4); (0, chai_1.expect)(tables).to.have.deep.members([ { table_name: 'knex_migrations', }, { table_name: 'knex_migrations_lock', }, { table_name: 'users', }, { table_name: 'tokens', }, ]); (0, chai_1.expect)(knexMigrations).to.have.deep.members(['users', 'tokens']); })); it('fails when changing the migrations object', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, migration_1.executeSchemaMigration)({ knex: knexConnection, schemaName, migrations: migrationsFixture }); const error = yield (0, migration_1.executeSchemaMigration)({ knex: knexConnection, schemaName, migrations: {} }); (0, chai_1.expect)(error).to.match(/The migration directory is corrupt, the following files are missing: users/); })); }); describe('using migration directories', () => { it('executes the migration using a directory as parameter', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, migration_1.executeSchemaMigrationFromDir)({ knex: knexConnection, schemaName, directory: `${__dirname}/../test/migration-files`, }); const tables = yield getSchemaTables(); (0, chai_1.expect)(tables).to.have.length(3); (0, chai_1.expect)(tables).to.have.deep.members([ { table_name: 'knex_migrations', }, { table_name: 'knex_migrations_lock', }, { table_name: 'customers', }, ]); const knexMigrations = (yield knexConnection(`${schemaName}.knex_migrations`)).map(({ name }) => name); (0, chai_1.expect)(knexMigrations).to.have.deep.members([ '0001_create_customers_table.ts', '0002_add_email_column_customers_table.ts', ]); })); it('fails if the directory does not exist', () => __awaiter(void 0, void 0, void 0, function* () { const error = yield (0, migration_1.executeSchemaMigrationFromDir)({ knex: knexConnection, schemaName, directory: 'invalid' }); (0, chai_1.expect)(error).to.match(/Could not read directory/); })); }); }); const usersMigrationFixture = (schemaName) => ({ up(knex) { return __awaiter(this, void 0, void 0, function* () { return knex.schema.withSchema(schemaName).createTable('users', (table) => { table.increments('id').primary(); }); }); }, down(knex) { return __awaiter(this, void 0, void 0, function* () { return knex.schema.withSchema(schemaName).dropTableIfExists('users'); }); }, }); const tokensMigrationFixture = (schemaName) => ({ up(knex) { return __awaiter(this, void 0, void 0, function* () { return knex.schema.withSchema(schemaName).createTable('tokens', (table) => { table.increments('id').primary(); }); }); }, down(knex) { return __awaiter(this, void 0, void 0, function* () { return knex.schema.withSchema(schemaName).dropTableIfExists('tokens'); }); }, }); const migrationsFixture = { users: usersMigrationFixture, }; //# sourceMappingURL=migration.test.js.map