Xmysql is now NocoDB (An Open Source Airtable alternative)

This commit is contained in:
“pranavxc”
2021-05-27 11:37:53 +05:30
parent 57469dc3e8
commit 69be190d68
944 changed files with 1385067 additions and 15900 deletions

View File

@@ -0,0 +1,396 @@
const should = require("should");
const path = require("path");
const { promisify } = require("util");
const fs = require("fs");
const jsonfile = require("jsonfile");
const glob = require("glob");
const SqlMigratorCli = require("../lib/SqlMigratorCli");
const SqlClientFactory = require("../../SqlClient/lib/SqlClientFactory");
class TestUtil {
static async checkFileExists(file, isExists, msg) {
const exist = await promisify(fs.exists)("./config.xc.json");
should.equal(exist, isExists, msg);
}
static async cmdInit(args) {
const cli = new SqlMigratorCli(args);
await cli.handle();
}
static async cmdSync(args) {
const cli = new SqlMigratorCli(args);
await cli.handle();
}
static async cmdMigrationCreate(args) {
const cli = new SqlMigratorCli(args);
await cli.handle();
}
/**
*
* @param args
* @param args.tn
* @param args.env
* @param args.envIndex
* @param args.recordsLength
* @returns {Promise<void>}
*/
static async cmdMigrationUpVerify(args) {
args.env = args.env || "dev";
args.envIndex = args.envIndex || 0;
const project = await promisify(jsonfile.readFile)("./config.xc.json");
const sqlClient = SqlClientFactory.create(
project.envs[args.env][args.envIndex]
);
const exists = await sqlClient.hasTable({ tn: args.tn });
should.equal(
exists.data.value,
true,
`${args.tn} should have got created on migration`
);
const rows = await sqlClient.selectAll(
project.envs[args.env][args.envIndex].meta.tn
);
should.equal(
rows.length,
args.recordsLength,
`${args.tn} should have got created on migration`
);
}
/**
*
* @param args
* @param args.tn
* @param args.env
* @param args.envIndex
* @param args.recordsLength
* @returns {Promise<void>}
*/
static async cmdMigrationDownVerify(args) {
args.env = args.env || "dev";
args.envIndex = args.envIndex || 0;
const project = await promisify(jsonfile.readFile)("./config.xc.json");
const sqlClient = SqlClientFactory.create(
project.envs[args.env][args.envIndex]
);
const exists = await sqlClient.hasTable({ tn: args.tn });
should.equal(
exists.data.value,
false,
`${args.tn} table should have got created on migration`
);
const rows = await sqlClient.selectAll(
project.envs[args.env][args.envIndex].meta.tn
);
should.equal(
rows.length,
args.recordsLength,
`${args.tn} table should have got created on migration`
);
}
static async cmdMigrationUp(args) {
const cli = new SqlMigratorCli(args);
await cli.handle();
}
static async cmdMigrationCreateVerify(args) {
const { upStatement } = args;
const { downStatement } = args;
const { recordsLength } = args;
const { dbAlias } = args;
let files = [];
files = await promisify(glob)(`./server/tool/${dbAlias}/migrations/*.up.sql`);
console.log(files);
should.equal(
files.length,
recordsLength,
`/server/tool/${dbAlias}/migrations up file is not created`
);
await promisify(fs.writeFile)(
files[files.length - 1],
upStatement,
"utf-8"
);
files = await promisify(glob)(
`./server/tool/${dbAlias}/migrations/*.down.sql`
);
should.equal(
files.length,
recordsLength,
`./server/tool/${dbAlias}} down files is not created`
);
await promisify(fs.writeFile)(
files[files.length - 1],
downStatement,
"utf-8"
);
}
static async cmdMigrationDown(args) {
const cli = new SqlMigratorCli(args);
await cli.handle();
}
/**
*
* @param args
* @param args.env
* @param args.dbAlias
* @returns {Promise<void>}
*/
static async cmdMigrationCleanVerify(args) {
let exists = await promisify(fs.exists)("./server/tool/primary");
should.equal(
exists,
false,
"./server/tool/primary is still left after clean"
);
exists = await promisify(fs.exists)("./server/tool/primary/migrations");
should.equal(
exists,
false,
"./server/tool/primary/migrations is still left after clean"
);
exists = await promisify(fs.exists)("./server/tool/secondary");
should.equal(
exists,
false,
"./server/tool/secondary is still left after clean"
);
exists = await promisify(fs.exists)("./server/tool/secondary/migrations");
should.equal(
exists,
false,
"./server/tool/secondary/migrations is still left after clean"
);
// database exists in all environments
const project = await promisify(jsonfile.readFile)("./config.xc.json");
for (const key in project.envs) {
for (var i = 0; i < project.envs[key].length; ++i) {
const connection = project.envs[key][i];
if (connection === "sqlite3") {
const key = "dev";
for (var i = 0; i < project.envs[key].length; ++i) {
const sqlClient = SqlClientFactory.create(connection);
exists = await sqlClient.hasDatabase({
databaseName: connection.connection.connection.filename
});
should.equal(
exists.data.value,
false,
`${key}/${
connection.connection.connection.filename
} do not exists`
);
}
} else {
try {
exists = { data: { value: false } };
const sqlClient = SqlClientFactory.create(connection);
exists = await sqlClient.hasDatabase({
databaseName: connection.connection.database
});
} catch (e) {
// exists.data = {false};
}
should.equal(
exists.data.value,
false,
`${key}/${connection.connection.database} do not exists`
);
}
// exists = await sqlClient.hasTable({tn:connection.meta.tn});
// should.equal(
// exists,
// false,
// `${key}/${$connection.connection.database}/${
// connection.meta.tn
// } do not exists`
// );
}
}
}
static async cmdMigrationClean(args) {
const cli = new SqlMigratorCli(args);
await cli.handle();
}
/**
*
* @param {object} - args for future reasons
* @returns {Promise<void>}
*/
static async cmdInitVerify(args = {}) {
/** ************** START : init verify *************** */
await this.checkFileExists(
"./config.xc.json",
true,
"config.xc.json is not created on init"
);
await this.checkFileExists(
"./xmigrator",
true,
"./xmigrator is not created on init"
);
await this.checkFileExists(
"./server/tool/primary",
true,
"./server/tool/primary is not created on init"
);
await this.checkFileExists(
"./server/tool/primary/migrations",
true,
"./server/tool/primary/migrations is not created on init"
);
await this.checkFileExists(
"./server/tool/secondary",
true,
"./server/tool/secondary is not created on init"
);
await this.checkFileExists(
"./server/tool/secondary/migrations",
true,
"./server/tool/secondary/migrations is not created on init"
);
/** ************** END : init verify *************** */
}
/**
*
* @param {object} - args
* @returns {Promise<void>}
*/
static async cmdSyncVerify() {
const project = await promisify(jsonfile.readFile)("./config.xc.json");
try {
for (const key in project.envs) {
for (let i = 0; i < project.envs[key].length; ++i) {
const connection = project.envs[key][i];
const sqlClient = SqlClientFactory.create(connection);
if (connection.client === "sqlite3") {
let exists = await sqlClient.hasDatabase({
databaseName: connection.connection.connection.filename
});
should.equal(
exists.data.value,
true,
`${key}: /${
connection.connection.connection.filename
} do not exists`
);
exists = await sqlClient.hasTable({
tn: connection.meta.tn
});
should.equal(
exists.data.value,
true,
`/${connection.connection.connection.filename}/${
connection.meta.tn
} do not exists`
);
} else if (connection.client === "oracledb") {
let exists = await sqlClient.hasDatabase({
databaseName: connection.connection.user
});
should.equal(
exists.data.value,
true,
`${key}/${connection.connection.user} do not exists`
);
exists = await sqlClient.hasTable({
tn: connection.meta.tn
});
should.equal(
exists.data.value,
true,
`${key}/${connection.connection.database}/${
connection.meta.tn
} do not exists`
);
} else {
let exists = await sqlClient.hasDatabase({
databaseName: connection.connection.database
});
should.equal(
exists.data.value,
true,
`${key}/${connection.connection.database} do not exists`
);
exists = await sqlClient.hasTable({
tn: connection.meta.tn
});
should.equal(
exists.data.value,
true,
`${key}/${connection.connection.database}/${
connection.meta.tn
} do not exists`
);
}
}
}
} catch (e) {
console.log(e);
throw e;
}
}
static sleep(milliSeconds = 1100) {
const until = new Date().getTime() + milliSeconds;
while (new Date().getTime() < until) {}
}
}
module.exports = TestUtil;
/**
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
*
* @author Naveen MR <oof1lab@gmail.com>
* @author Pranav C Balan <pranavxc@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

View File

@@ -0,0 +1,425 @@
/* eslint-disable func-names */
// 'use strict';
const { promisify } = require("util");
const fs = require("fs");
const jsonfile = require("jsonfile");
const should = require("should");
const SqlClientFactory = require("../../SqlClient/lib/SqlClientFactory");
const TestUtil = require("./TestUtil");
const sqlStatements = require("./sql.statements");
const { DB_TYPE } = process.env;
let sqlType = "mysql";
let upStatement = null;
let downStatement = null;
let blogUp = null;
let blogDown = null;
let testNum = 0;
const sqlDbs = {
sqlite: {
sqlType: "sqlite",
upStatement: sqlStatements.sqlite3.user.up,
downStatement: sqlStatements.sqlite3.user.down,
blogUp: sqlStatements.sqlite3.blog.up,
blogDown: sqlStatements.sqlite3.blog.down
}
// mysql: {
// sqlType: "mysql",
// upStatement: sqlStatements.mysql.user.up,
// downStatement: sqlStatements.mysql.user.down,
// blogUp: sqlStatements.mysql.blog.up,
// blogDown: sqlStatements.mysql.blog.down
// },
// pg: {
// sqlType: "pg",
// upStatement: sqlStatements.pg.user.up,
// downStatement: sqlStatements.pg.user.down,
// blogUp: sqlStatements.pg.blog.up,
// blogDown: sqlStatements.pg.blog.down
// },
// mssql: {
// sqlType: "mssql",
// upStatement: sqlStatements.mssql.user.up,
// downStatement: sqlStatements.mssql.user.down,
// blogUp: sqlStatements.mssql.blog.up,
// blogDown: sqlStatements.mssql.blog.down
// },
// oracle: {
// sqlType: "oracle",
// upStatement: sqlStatements.oracledb.user.up,
// downStatement: sqlStatements.oracledb.user.down,
// blogUp: sqlStatements.oracledb.blog.up,
// blogDown: sqlStatements.oracledb.blog.down
// }
};
let db = sqlDbs[DB_TYPE];
if (!db) {
console.error("Invalid DB Type, running tests on sqlite", DB_TYPE);
db = sqlDbs.sqlite;
}
// sqlDbs.forEach(function(db) {
describe("SqlMigratorCli : Tests", function() {
before(async function() {
try {
await promisify(fs.unlink)("./config.xc.json");
} catch (e) {
console.log("..");
}
sqlType = db.sqlType;
upStatement = db.upStatement;
downStatement = db.downStatement;
blogUp = db.blogUp;
blogDown = db.blogDown;
testNum = 0;
});
beforeEach(function(done) {
console.log("\n", `${sqlType}:${testNum}`);
testNum++;
done();
});
it(`xmigrator init should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdInit({
args: ["i", sqlType]
});
await TestUtil.cmdInitVerify();
});
it(`xmigrator sync should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdSync({
args: ["s"]
});
await TestUtil.cmdSyncVerify();
});
it(`xmigrator migration create (first migration) should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationCreate({
args: ["m", "c"]
});
await TestUtil.cmdMigrationCreateVerify({
upStatement,
downStatement,
recordsLength: 1,
dbAlias: "primary"
});
});
it(`xmigrator migration create (second migration) should pass`, async function() {
this.timeout(20000);
TestUtil.sleep();
await TestUtil.cmdMigrationCreate({
args: ["m", "c"]
});
await TestUtil.cmdMigrationCreateVerify({
upStatement: blogUp,
downStatement: blogDown,
recordsLength: 2,
dbAlias: "primary"
});
});
it(`xmigrator migration up --step=1 (first migration) should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationUp({
args: ["m", "u"],
steps: 1
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 1,
tn: "user"
});
});
it(`xmigrator migration up --step=1 (second migration) should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationUp({
args: ["m", "u"],
steps: 1
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 2,
tn: "blog"
});
});
it(`xmigrator migration down should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationDown({
args: ["m", "d"]
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 0,
tn: "blog"
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 0,
tn: "user"
});
});
it(`xmigrator migration up should pass`, async function() {
await TestUtil.cmdMigrationUp({
args: ["m", "u"]
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 2,
tn: "blog"
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 2,
tn: "user"
});
});
it(`xmigrator migration down --steps=1 (first migration) should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationDown({
args: ["m", "d"],
steps: 1
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 1,
tn: "blog"
});
});
it(`xmigrator migration down --steps=1 (second migration) should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationDown({
args: ["m", "d"],
steps: 1
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 0,
tn: "user"
});
});
it(`xmigrator migration create (secondary - 1st migration) should pass`, async function() {
this.timeout(20000);
TestUtil.sleep();
await TestUtil.cmdMigrationCreate({
args: ["m", "c"],
dbAlias: "db2"
});
//
await TestUtil.cmdMigrationCreateVerify({
upStatement,
downStatement,
recordsLength: 1,
dbAlias: "db2"
});
});
it(`xmigrator migration create (secondary - 2nd migration) should pass`, async function() {
this.timeout(20000);
TestUtil.sleep();
await TestUtil.cmdMigrationCreate({
args: ["m", "c"],
dbAlias: "db2"
});
//
await TestUtil.cmdMigrationCreateVerify({
upStatement: blogUp,
downStatement: blogDown,
recordsLength: 2,
dbAlias: "db2"
});
});
it(`xmigrator migration up should pass db2`, async function() {
await TestUtil.cmdMigrationUp({
args: ["m", "u"],
dbAlias: "db2"
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 2,
tn: "user",
envIndex: 1
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 2,
tn: "blog",
envIndex: 1
});
});
it(`xmigrator migration down should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationDown({
args: ["m", "d"],
dbAlias: "db2"
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 0,
envIndex: 1,
tn: "user"
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 0,
envIndex: 1,
tn: "blog"
});
});
it(`xmigrator migration up --env test should pass`, async function() {
await TestUtil.cmdMigrationUp({
args: ["m", "u"],
dbAlias: "db2",
env: "test"
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 2,
tn: "user",
envIndex: 1,
env: "test"
});
await TestUtil.cmdMigrationUpVerify({
recordsLength: 2,
tn: "blog",
envIndex: 1,
env: "test"
});
});
it(`xmigrator migration down --env test should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationDown({
args: ["m", "d"],
dbAlias: "db2",
env: "test"
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 0,
envIndex: 1,
tn: "user",
env: "test"
});
await TestUtil.cmdMigrationDownVerify({
recordsLength: 0,
envIndex: 1,
tn: "blog",
env: "test"
});
});
it(`xmigrator clean --env=test --dbAlias=db2 should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationClean({
args: ["c"],
env: "test",
dbAlias: "db2"
});
let exists = false;
// database exists in all environments
const project = await promisify(jsonfile.readFile)("./config.xc.json");
const key = "test";
for (let i = 0; i < project.envs[key].length; ++i) {
const connection = project.envs[key][i];
if (connection.meta.dbAlias === "db2") {
try {
const sqlClient = SqlClientFactory.create(connection);
} catch (e) {
exists = false;
}
should.equal(
exists,
false,
`${key}/${connection.connection.database} do not exists`
);
}
}
// migrations table exists in all environments
});
if (db.sqlType === "oracle") {
console.log("\n\nPlease Drop All Database in Oracle Manually\n\n");
} else {
it(`xmigrator clean should pass`, async function() {
this.timeout(20000);
await TestUtil.cmdMigrationClean({
args: ["c"]
});
await TestUtil.cmdMigrationCleanVerify();
// migrations table exists in all environments
});
}
});
// });
/**
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
*
* @author Naveen MR <oof1lab@gmail.com>
* @author Pranav C Balan <pranavxc@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

View File

@@ -0,0 +1,103 @@
module.exports = {
mysql: {
user: {
up: `CREATE TABLE \`user\` (\n \`id\` INT NOT NULL AUTO_INCREMENT,\n \`title\` VARCHAR(45) NULL,\n PRIMARY KEY (\`id\`)\n);`,
down: "drop table user;"
},
blog: {
up:
"CREATE TABLE `blog` (\n `id` INT NOT NULL AUTO_INCREMENT,\n `title` VARCHAR(45) NULL,\n PRIMARY KEY (`id`)\n);",
down: "drop table blog;"
}
},
pg: {
user: {
up: `CREATE TABLE "user" (
user_id serial PRIMARY KEY,
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL,
email VARCHAR (355) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);`,
down: `DROP TABLE "user";`
},
blog: {
up: `CREATE TABLE "blog" (
user_id serial PRIMARY KEY,
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL,
email VARCHAR (355) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);`,
down: `DROP TABLE "blog";`
}
},
mssql: {
user: {
up:
'CREATE TABLE "user" (\n user_id INT PRIMARY KEY,\n last_name VARCHAR(50) NOT NULL,\n first_name VARCHAR(50),\n );',
down: 'DROP TABLE "user";'
},
blog: {
up:
'CREATE TABLE "blog" (\n blog_id INT PRIMARY KEY,\n blog_content VARCHAR(50) NOT NULL,\n );',
down: 'drop table "blog";'
}
},
// INFO: semicolon is require at the end
oracledb: {
user: {
up:
'CREATE TABLE "user"\n( user_id number(10) NOT NULL,\n user_name varchar2(50) NOT NULL\n);',
down: 'DROP TABLE "user";'
},
blog: {
up:
'CREATE TABLE "blog"\n( blog_id number(10) NOT NULL,\n blog_name varchar2(50) NOT NULL\n);',
down: 'DROP TABLE "blog";'
}
},
sqlite3: {
user: {
up:
"CREATE TABLE user (\n id INTEGER PRIMARY KEY,\n first_name TEXT NOT NULL\n)",
down: "drop table user"
},
blog: {
up: "CREATE TABLE blog (\n id INTEGER PRIMARY KEY\n)",
down: "drop table blog"
}
}
};
/**
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
*
* @author Naveen MR <oof1lab@gmail.com>
* @author Pranav C Balan <pranavxc@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/