mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
9.1 KiB
322 lines
9.1 KiB
2 years ago
|
import { DbConfig } from '../../src/interface/config';
|
||
|
import { NcConfigFactory } from '../../src/lib';
|
||
|
import SqlMgrv2 from '../../src/lib/db/sql-mgr/v2/SqlMgrv2';
|
||
2 years ago
|
import fs from 'fs';
|
||
2 years ago
|
import { Knex, knex } from 'knex';
|
||
|
import process from 'process';
|
||
2 years ago
|
|
||
|
export default class TestDbMngr {
|
||
|
public static readonly dbName = 'test_meta';
|
||
|
public static readonly sakilaDbName = 'test_sakila';
|
||
2 years ago
|
public static metaKnex: Knex;
|
||
|
public static sakilaKnex: Knex;
|
||
2 years ago
|
|
||
2 years ago
|
public static defaultConnection = {
|
||
2 years ago
|
user: 'root',
|
||
|
password: 'password',
|
||
|
host: 'localhost',
|
||
|
port: 3306,
|
||
2 years ago
|
client: 'mysql2',
|
||
2 years ago
|
};
|
||
|
|
||
|
public static pgConnection = {
|
||
|
user: 'postgres',
|
||
|
password: 'password',
|
||
|
host: 'localhost',
|
||
|
port: 5432,
|
||
|
client: 'pg',
|
||
|
};
|
||
2 years ago
|
|
||
2 years ago
|
public static connection: {
|
||
|
user: string;
|
||
|
password: string;
|
||
|
host: string;
|
||
|
port: number;
|
||
|
client: string;
|
||
|
} = TestDbMngr.defaultConnection;
|
||
|
|
||
2 years ago
|
public static dbConfig: DbConfig;
|
||
|
|
||
2 years ago
|
static populateConnectionConfig() {
|
||
|
const { user, password, host, port, client } = TestDbMngr.defaultConnection;
|
||
|
TestDbMngr.connection = {
|
||
|
user: process.env['DB_USER'] || user,
|
||
|
password: process.env['DB_PASSWORD'] || password,
|
||
|
host: process.env['DB_HOST'] || host,
|
||
|
port: Number(process.env['DB_PORT']) || port,
|
||
2 years ago
|
client: process.env['DB_CLIENT'] || client,
|
||
|
};
|
||
|
|
||
|
console.log(TestDbMngr.connection);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
static async testConnection(config: DbConfig) {
|
||
2 years ago
|
try {
|
||
2 years ago
|
console.log('Testing connection', TestDbMngr.connection);
|
||
2 years ago
|
return await SqlMgrv2.testConnection(config);
|
||
2 years ago
|
} catch (e) {
|
||
|
console.log(e);
|
||
|
return { code: -1, message: 'Connection invalid' };
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
static async init() {
|
||
2 years ago
|
TestDbMngr.populateConnectionConfig();
|
||
2 years ago
|
|
||
2 years ago
|
// common for both pg and mysql
|
||
|
if (await TestDbMngr.isDbConfigured()) {
|
||
|
await TestDbMngr.connectDb();
|
||
2 years ago
|
} else {
|
||
2 years ago
|
console.log('Mysql is not configured. Switching to sqlite');
|
||
2 years ago
|
await TestDbMngr.switchToSqlite();
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
private static async isDbConfigured() {
|
||
2 years ago
|
const { user, password, host, port, client } = TestDbMngr.connection;
|
||
2 years ago
|
const config = NcConfigFactory.urlToDbConfig(
|
||
|
`${client}://${user}:${password}@${host}:${port}`
|
||
|
);
|
||
2 years ago
|
config.connection = {
|
||
|
user,
|
||
|
password,
|
||
|
host,
|
||
|
port,
|
||
2 years ago
|
};
|
||
2 years ago
|
const result = await TestDbMngr.testConnection(config);
|
||
|
return result.code !== -1;
|
||
|
}
|
||
2 years ago
|
static async connectDb() {
|
||
2 years ago
|
const { user, password, host, port, client } = TestDbMngr.connection;
|
||
2 years ago
|
if (!process.env[`DATABASE_URL`]) {
|
||
|
process.env[
|
||
|
`DATABASE_URL`
|
||
|
] = `${client}://${user}:${password}@${host}:${port}/${TestDbMngr.dbName}`;
|
||
2 years ago
|
}
|
||
|
|
||
|
TestDbMngr.dbConfig = NcConfigFactory.urlToDbConfig(
|
||
|
NcConfigFactory.extractXcUrlFromJdbc(process.env[`DATABASE_URL`])
|
||
|
);
|
||
|
this.dbConfig.meta = {
|
||
|
tn: 'nc_evolutions',
|
||
|
dbAlias: 'db',
|
||
|
api: {
|
||
|
type: 'rest',
|
||
|
prefix: '',
|
||
|
graphqlDepthLimit: 10,
|
||
|
},
|
||
|
inflection: {
|
||
|
tn: 'camelize',
|
||
|
cn: 'camelize',
|
||
|
},
|
||
2 years ago
|
};
|
||
2 years ago
|
|
||
2 years ago
|
await TestDbMngr.setupMeta();
|
||
|
await TestDbMngr.setupSakila();
|
||
|
}
|
||
|
|
||
|
static async setupMeta() {
|
||
2 years ago
|
if (TestDbMngr.metaKnex) {
|
||
2 years ago
|
await TestDbMngr.metaKnex.destroy();
|
||
|
}
|
||
|
|
||
2 years ago
|
if (TestDbMngr.isSqlite()) {
|
||
2 years ago
|
await TestDbMngr.resetMetaSqlite();
|
||
|
TestDbMngr.metaKnex = knex(TestDbMngr.getMetaDbConfig());
|
||
2 years ago
|
return;
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
TestDbMngr.metaKnex = knex(TestDbMngr.getDbConfigWithNoDb());
|
||
|
await TestDbMngr.resetDatabase(TestDbMngr.metaKnex, TestDbMngr.dbName);
|
||
|
await TestDbMngr.metaKnex.destroy();
|
||
|
|
||
|
TestDbMngr.metaKnex = knex(TestDbMngr.getMetaDbConfig());
|
||
|
await TestDbMngr.useDatabase(TestDbMngr.metaKnex, TestDbMngr.dbName);
|
||
|
}
|
||
|
|
||
2 years ago
|
static async setupSakila() {
|
||
|
if (TestDbMngr.sakilaKnex) {
|
||
2 years ago
|
await TestDbMngr.sakilaKnex.destroy();
|
||
|
}
|
||
|
|
||
2 years ago
|
if (TestDbMngr.isSqlite()) {
|
||
2 years ago
|
await TestDbMngr.seedSakila();
|
||
|
TestDbMngr.sakilaKnex = knex(TestDbMngr.getSakilaDbConfig());
|
||
2 years ago
|
return;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
TestDbMngr.sakilaKnex = knex(TestDbMngr.getDbConfigWithNoDb());
|
||
2 years ago
|
await TestDbMngr.resetDatabase(
|
||
|
TestDbMngr.sakilaKnex,
|
||
|
TestDbMngr.sakilaDbName
|
||
|
);
|
||
2 years ago
|
await TestDbMngr.sakilaKnex.destroy();
|
||
2 years ago
|
|
||
2 years ago
|
TestDbMngr.sakilaKnex = knex(TestDbMngr.getSakilaDbConfig());
|
||
2 years ago
|
await TestDbMngr.useDatabase(
|
||
|
TestDbMngr.sakilaKnex,
|
||
|
TestDbMngr.sakilaDbName
|
||
|
);
|
||
2 years ago
|
}
|
||
|
|
||
|
static async switchToSqlite() {
|
||
|
// process.env[`DATABASE_URL`] = `sqlite3:///?database=${__dirname}/${TestDbMngr.dbName}.sqlite`;
|
||
|
TestDbMngr.dbConfig = {
|
||
|
client: 'sqlite3',
|
||
|
connection: {
|
||
|
filename: `${__dirname}/${TestDbMngr.dbName}.db`,
|
||
|
database: TestDbMngr.dbName,
|
||
|
},
|
||
|
useNullAsDefault: true,
|
||
|
meta: {
|
||
|
tn: 'nc_evolutions',
|
||
|
dbAlias: 'db',
|
||
|
api: {
|
||
|
type: 'rest',
|
||
|
prefix: '',
|
||
|
graphqlDepthLimit: 10,
|
||
|
},
|
||
|
inflection: {
|
||
|
tn: 'camelize',
|
||
|
cn: 'camelize',
|
||
|
},
|
||
|
},
|
||
2 years ago
|
};
|
||
2 years ago
|
|
||
2 years ago
|
process.env[
|
||
|
`NC_DB`
|
||
|
] = `sqlite3:///?database=${__dirname}/${TestDbMngr.dbName}.db`;
|
||
2 years ago
|
await TestDbMngr.setupMeta();
|
||
|
await TestDbMngr.setupSakila();
|
||
|
}
|
||
|
|
||
|
private static async resetDatabase(knexClient, dbName) {
|
||
2 years ago
|
if (TestDbMngr.isSqlite()) {
|
||
2 years ago
|
// return knexClient.raw(`DELETE FROM sqlite_sequence`);
|
||
|
} else {
|
||
|
try {
|
||
|
await knexClient.raw(`DROP DATABASE ${dbName}`);
|
||
2 years ago
|
} catch (e) {}
|
||
2 years ago
|
await knexClient.raw(`CREATE DATABASE ${dbName}`);
|
||
|
console.log(`Database ${dbName} created`);
|
||
2 years ago
|
|
||
|
if (!TestDbMngr.isPg()) {
|
||
|
await knexClient.raw(`USE ${dbName}`);
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
static isSqlite() {
|
||
|
return TestDbMngr.dbConfig.client === 'sqlite3';
|
||
|
}
|
||
|
|
||
2 years ago
|
static isPg() {
|
||
|
return TestDbMngr.dbConfig.client === 'pg';
|
||
|
}
|
||
|
|
||
2 years ago
|
private static async useDatabase(knexClient, dbName) {
|
||
2 years ago
|
if (!TestDbMngr.isSqlite() && !TestDbMngr.isPg()) {
|
||
2 years ago
|
await knexClient.raw(`USE ${dbName}`);
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
static getDbConfigWithNoDb() {
|
||
2 years ago
|
const dbConfig = JSON.parse(JSON.stringify(TestDbMngr.dbConfig));
|
||
2 years ago
|
delete dbConfig.connection.database;
|
||
|
return dbConfig;
|
||
|
}
|
||
|
|
||
2 years ago
|
static getMetaDbConfig() {
|
||
|
return TestDbMngr.dbConfig;
|
||
|
}
|
||
|
|
||
2 years ago
|
private static resetMetaSqlite() {
|
||
2 years ago
|
if (fs.existsSync(`${__dirname}/test_meta.db`)) {
|
||
2 years ago
|
fs.unlinkSync(`${__dirname}/test_meta.db`);
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
static getSakilaDbConfig() {
|
||
2 years ago
|
const sakilaDbConfig = JSON.parse(JSON.stringify(TestDbMngr.dbConfig));
|
||
2 years ago
|
sakilaDbConfig.connection.database = TestDbMngr.sakilaDbName;
|
||
2 years ago
|
sakilaDbConfig.connection.multipleStatements = true;
|
||
|
if (TestDbMngr.isSqlite()) {
|
||
|
sakilaDbConfig.connection.filename = `${__dirname}/test_sakila.db`;
|
||
2 years ago
|
}
|
||
2 years ago
|
return sakilaDbConfig;
|
||
|
}
|
||
|
|
||
2 years ago
|
static async seedSakila() {
|
||
2 years ago
|
const testsDir = __dirname.replace('tests/unit', 'tests');
|
||
|
|
||
2 years ago
|
if (TestDbMngr.isSqlite()) {
|
||
|
if (fs.existsSync(`${__dirname}/test_sakila.db`)) {
|
||
2 years ago
|
fs.unlinkSync(`${__dirname}/test_sakila.db`);
|
||
|
}
|
||
2 years ago
|
fs.copyFileSync(
|
||
|
`${testsDir}/sqlite-sakila-db/sakila.db`,
|
||
|
`${__dirname}/test_sakila.db`
|
||
|
);
|
||
|
} else if (TestDbMngr.isPg()) {
|
||
|
const schemaFile = fs
|
||
|
.readFileSync(`${testsDir}/pg-sakila-db/01-postgres-sakila-schema.sql`)
|
||
|
.toString();
|
||
|
const dataFile = fs
|
||
|
.readFileSync(
|
||
|
`${testsDir}/pg-sakila-db/02-postgres-sakila-insert-data.sql`
|
||
|
)
|
||
|
.toString();
|
||
|
await TestDbMngr.sakilaKnex.raw(schemaFile);
|
||
|
await TestDbMngr.sakilaKnex.raw(dataFile);
|
||
2 years ago
|
} else {
|
||
2 years ago
|
const schemaFile = fs
|
||
|
.readFileSync(`${testsDir}/mysql-sakila-db/03-test-sakila-schema.sql`)
|
||
|
.toString();
|
||
|
const dataFile = fs
|
||
|
.readFileSync(`${testsDir}/mysql-sakila-db/04-test-sakila-data.sql`)
|
||
|
.toString();
|
||
2 years ago
|
await TestDbMngr.sakilaKnex.raw(schemaFile);
|
||
|
await TestDbMngr.sakilaKnex.raw(dataFile);
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
static async disableForeignKeyChecks(knexClient) {
|
||
2 years ago
|
if (TestDbMngr.isSqlite()) {
|
||
|
await knexClient.raw('PRAGMA foreign_keys = OFF');
|
||
|
} else if (TestDbMngr.isPg()) {
|
||
|
await knexClient.raw(`SET session_replication_role = 'replica'`);
|
||
|
} else {
|
||
2 years ago
|
await knexClient.raw(`SET FOREIGN_KEY_CHECKS = 0`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static async enableForeignKeyChecks(knexClient) {
|
||
2 years ago
|
if (TestDbMngr.isSqlite()) {
|
||
2 years ago
|
await knexClient.raw(`PRAGMA foreign_keys = ON;`);
|
||
2 years ago
|
} else if (TestDbMngr.isPg()) {
|
||
|
await knexClient.raw(`SET session_replication_role = 'origin'`);
|
||
|
} else {
|
||
2 years ago
|
await knexClient.raw(`SET FOREIGN_KEY_CHECKS = 1`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static async showAllTables(knexClient) {
|
||
2 years ago
|
if (TestDbMngr.isSqlite()) {
|
||
|
const tables = await knexClient.raw(
|
||
|
`SELECT name FROM sqlite_master WHERE type='table'`
|
||
2 years ago
|
);
|
||
2 years ago
|
return tables
|
||
|
.filter((t) => t.name !== 'sqlite_sequence' && t.name !== '_evolutions')
|
||
|
.map((t) => t.name);
|
||
|
} else if (TestDbMngr.isPg()) {
|
||
|
const tables = await knexClient.raw(
|
||
|
`SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';`
|
||
|
);
|
||
|
return tables.rows.map((t) => t.tablename);
|
||
|
} else {
|
||
|
const response = await knexClient.raw(`SHOW TABLES`);
|
||
|
return response[0].map((table) => Object.values(table)[0]);
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
}
|