2019-09-12 15:26:21 +00:00
|
|
|
import * as Bluebird from 'bluebird';
|
|
|
|
import * as Knex from 'knex';
|
|
|
|
import { fs } from 'mz';
|
|
|
|
|
|
|
|
import ChaiConfig = require('./lib/chai-config');
|
|
|
|
import prepare = require('./lib/prepare');
|
|
|
|
|
2020-05-28 17:15:33 +00:00
|
|
|
import * as constants from '../src/lib/constants';
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
const { expect } = ChaiConfig;
|
|
|
|
|
|
|
|
async function createOldDatabase(path: string) {
|
|
|
|
const knex = Knex({
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: path,
|
|
|
|
},
|
|
|
|
useNullAsDefault: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const createEmptyTable = (
|
|
|
|
name: string,
|
|
|
|
fn: (trx: Knex.CreateTableBuilder) => void,
|
|
|
|
) =>
|
2020-05-15 11:01:51 +00:00
|
|
|
knex.schema.createTable(name, (t) => {
|
2019-09-12 15:26:21 +00:00
|
|
|
if (fn != null) {
|
|
|
|
return fn(t);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-15 11:01:51 +00:00
|
|
|
await createEmptyTable('app', (t) => {
|
2019-09-12 15:26:21 +00:00
|
|
|
t.increments('id').primary();
|
|
|
|
t.boolean('privileged');
|
|
|
|
return t.string('containerId');
|
|
|
|
});
|
2020-05-15 11:01:51 +00:00
|
|
|
await createEmptyTable('config', (t) => {
|
2019-09-12 15:26:21 +00:00
|
|
|
t.string('key');
|
|
|
|
return t.string('value');
|
|
|
|
});
|
2020-05-15 11:01:51 +00:00
|
|
|
await createEmptyTable('dependentApp', (t) => t.increments('id').primary());
|
|
|
|
await createEmptyTable('dependentDevice', (t) =>
|
|
|
|
t.increments('id').primary(),
|
|
|
|
);
|
2019-09-12 15:26:21 +00:00
|
|
|
return knex;
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:15:33 +00:00
|
|
|
describe('Database Migrations', () => {
|
|
|
|
before(async () => {
|
|
|
|
await prepare();
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2020-05-28 17:15:33 +00:00
|
|
|
after(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
constants.databasePath = process.env.DATABASE_PATH;
|
|
|
|
delete require.cache[require.resolve('../src/db')];
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2020-05-28 17:15:33 +00:00
|
|
|
it('creates a database at the path passed on creation', async () => {
|
|
|
|
const databasePath = process.env.DATABASE_PATH_2!;
|
|
|
|
// @ts-ignore
|
|
|
|
constants.databasePath = databasePath;
|
|
|
|
delete require.cache[require.resolve('../src/db')];
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-05-28 17:15:33 +00:00
|
|
|
const testDb = await import('../src/db');
|
|
|
|
await testDb.initialized;
|
|
|
|
await fs.stat(databasePath);
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('adds new fields and removes old ones in an old database', async () => {
|
|
|
|
const databasePath = process.env.DATABASE_PATH_3!;
|
|
|
|
|
|
|
|
const knexForDB = await createOldDatabase(databasePath);
|
2020-05-28 17:15:33 +00:00
|
|
|
// @ts-ignore
|
|
|
|
constants.databasePath = databasePath;
|
|
|
|
delete require.cache[require.resolve('../src/db')];
|
|
|
|
const testDb = await import('../src/db');
|
|
|
|
await testDb.initialized;
|
2019-09-12 15:26:21 +00:00
|
|
|
await Bluebird.all([
|
|
|
|
expect(knexForDB.schema.hasColumn('app', 'appId')).to.eventually.be.true,
|
|
|
|
expect(knexForDB.schema.hasColumn('app', 'releaseId')).to.eventually.be
|
|
|
|
.true,
|
|
|
|
expect(knexForDB.schema.hasColumn('app', 'config')).to.eventually.be
|
|
|
|
.false,
|
|
|
|
expect(knexForDB.schema.hasColumn('app', 'privileged')).to.eventually.be
|
|
|
|
.false,
|
|
|
|
expect(knexForDB.schema.hasColumn('app', 'containerId')).to.eventually.be
|
|
|
|
.false,
|
|
|
|
expect(knexForDB.schema.hasColumn('dependentApp', 'environment')).to
|
|
|
|
.eventually.be.true,
|
|
|
|
expect(knexForDB.schema.hasColumn('dependentDevice', 'markedForDeletion'))
|
|
|
|
.to.eventually.be.true,
|
|
|
|
expect(knexForDB.schema.hasColumn('dependentDevice', 'localId')).to
|
|
|
|
.eventually.be.true,
|
|
|
|
expect(knexForDB.schema.hasColumn('dependentDevice', 'is_managed_by')).to
|
|
|
|
.eventually.be.true,
|
|
|
|
expect(knexForDB.schema.hasColumn('dependentDevice', 'lock_expiry_date'))
|
|
|
|
.to.eventually.be.true,
|
|
|
|
]);
|
|
|
|
});
|
2020-05-28 17:15:33 +00:00
|
|
|
});
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-05-28 17:15:33 +00:00
|
|
|
describe('Database', () => {
|
|
|
|
let db: typeof import('../src/db');
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
await prepare();
|
|
|
|
db = await import('../src/db');
|
|
|
|
});
|
|
|
|
it('initializes correctly, running the migrations', () => {
|
|
|
|
return expect(db.initialized).to.be.fulfilled;
|
|
|
|
});
|
|
|
|
it('creates a database at the path from an env var', () => {
|
|
|
|
const promise = fs.stat(process.env.DATABASE_PATH!);
|
|
|
|
return expect(promise).to.be.fulfilled;
|
|
|
|
});
|
2019-09-12 15:26:21 +00:00
|
|
|
it('creates a deviceConfig table with a single default value', async () => {
|
|
|
|
const deviceConfig = await db.models('deviceConfig').select();
|
|
|
|
expect(deviceConfig).to.have.lengthOf(1);
|
|
|
|
expect(deviceConfig).to.deep.equal([{ targetValues: '{}' }]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows performing transactions', () => {
|
2020-05-15 11:01:51 +00:00
|
|
|
return db.transaction((trx) => expect(trx.commit()).to.be.fulfilled);
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
});
|