2019-09-12 15:26:21 +00:00
|
|
|
import * as Bluebird from 'bluebird';
|
2022-09-26 18:26:48 +00:00
|
|
|
import { knex, Knex } from 'knex';
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2021-04-26 19:54:04 +00:00
|
|
|
import { expect } from 'chai';
|
2022-08-17 23:35:08 +00:00
|
|
|
import prepare = require('~/test-lib/prepare');
|
|
|
|
import * as constants from '~/lib/constants';
|
|
|
|
import { exists } from '~/lib/fs-utils';
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
async function createOldDatabase(path: string) {
|
2022-09-26 18:26:48 +00:00
|
|
|
const db = knex({
|
2019-09-12 15:26:21 +00:00
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: path,
|
|
|
|
},
|
|
|
|
useNullAsDefault: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const createEmptyTable = (
|
|
|
|
name: string,
|
|
|
|
fn: (trx: Knex.CreateTableBuilder) => void,
|
|
|
|
) =>
|
2022-09-26 18:26:48 +00:00
|
|
|
db.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(),
|
|
|
|
);
|
2022-09-26 18:26:48 +00:00
|
|
|
return db;
|
2019-09-12 15:26:21 +00:00
|
|
|
}
|
|
|
|
|
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(() => {
|
2022-09-19 15:33:52 +00:00
|
|
|
// @ts-expect-error
|
2020-05-28 17:15:33 +00:00
|
|
|
constants.databasePath = process.env.DATABASE_PATH;
|
2022-08-17 23:35:08 +00:00
|
|
|
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!;
|
2022-09-19 15:33:52 +00:00
|
|
|
// @ts-expect-error
|
2020-05-28 17:15:33 +00:00
|
|
|
constants.databasePath = databasePath;
|
2022-08-17 23:35:08 +00:00
|
|
|
delete require.cache[require.resolve('~/src/db')];
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2022-08-17 23:35:08 +00:00
|
|
|
const testDb = await import('~/src/db');
|
2022-09-06 18:03:23 +00:00
|
|
|
await testDb.initialized();
|
2021-04-26 19:54:04 +00:00
|
|
|
expect(await exists(databasePath)).to.be.true;
|
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);
|
2022-09-19 15:33:52 +00:00
|
|
|
// @ts-expect-error
|
2020-05-28 17:15:33 +00:00
|
|
|
constants.databasePath = databasePath;
|
2022-08-17 23:35:08 +00:00
|
|
|
delete require.cache[require.resolve('~/src/db')];
|
|
|
|
const testDb = await import('~/src/db');
|
2022-09-06 18:03:23 +00:00
|
|
|
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', () => {
|
2022-08-17 23:35:08 +00:00
|
|
|
let db: typeof import('~/src/db');
|
2020-05-28 17:15:33 +00:00
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
await prepare();
|
2022-08-17 23:35:08 +00:00
|
|
|
db = await import('~/src/db');
|
2020-05-28 17:15:33 +00:00
|
|
|
});
|
|
|
|
it('initializes correctly, running the migrations', () => {
|
2022-09-06 18:03:23 +00:00
|
|
|
return expect(db.initialized()).to.be.fulfilled;
|
2020-05-28 17:15:33 +00:00
|
|
|
});
|
2021-04-26 19:54:04 +00:00
|
|
|
it('creates a database at the path from an env var', async () => {
|
|
|
|
expect(await exists(process.env.DATABASE_PATH!)).to.be.true;
|
2020-05-28 17:15:33 +00:00
|
|
|
});
|
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
|
|
|
});
|
|
|
|
});
|