Browse Source

feat: metadb project migration file naming

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/622/head
Pranav C 3 years ago
parent
commit
516aae8f9a
  1. 25
      packages/nocodb/src/lib/migrator/SqlMigrator/lib/KnexMigrator.ts
  2. 3
      packages/nocodb/src/lib/noco/meta/NcMetaIO.ts
  3. 25
      packages/nocodb/src/lib/noco/meta/NcMetaIOImpl.ts
  4. 50
      packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts
  5. 25
      packages/nocodb/src/lib/noco/rest/RestAuthCtrl.ts

25
packages/nocodb/src/lib/migrator/SqlMigrator/lib/KnexMigrator.ts

@ -375,7 +375,7 @@ export default class KnexMigrator extends SqlMigrator {
// await sqlClient.createTableIfNotExists({tn: connectionConfig.meta.tn}); // await sqlClient.createTableIfNotExists({tn: connectionConfig.meta.tn});
} }
async _initEnvDbsWithSql(env, dbAlias = null, sqlClient =null) { async _initEnvDbsWithSql(env, dbAlias = null, sqlClient = null) {
const {envs} = this.project; const {envs} = this.project;
@ -470,8 +470,8 @@ export default class KnexMigrator extends SqlMigrator {
if (!migrationSteps && !args.file) { if (!migrationSteps && !args.file) {
result.code = -1; result.code = -1;
result.message = result.message =
"Neither num of steps nor file is specified for migartion"; "Neither num of steps nor file is specified for migration";
log.debug("Neither num of steps nor file is specified for migartion"); log.debug("Neither num of steps nor file is specified for migration");
log.debug("See help"); log.debug("See help");
return result; return result;
} }
@ -501,7 +501,13 @@ export default class KnexMigrator extends SqlMigrator {
args.env args.env
); );
const sqlClient = args.sqlClient || SqlClientFactory.create(connection); const sqlClient = args.sqlClient || SqlClientFactory.create(connection);
const migrations = await sqlClient.selectAll(sqlClient.getTnPath(connection.meta.tn));
let migrations = await sqlClient.selectAll(sqlClient.getTnPath(connection.meta.tn));
if (this.suffix) {
migrations = migrations.filter(m => m.title.includes(this.suffix))
}
/** ************** END : get files and migrations *************** */ /** ************** END : get files and migrations *************** */
if (files.length === migrations.length) { if (files.length === migrations.length) {
@ -971,7 +977,7 @@ export default class KnexMigrator extends SqlMigrator {
// } // }
// create filenames // create filenames
const prefix = fileHelp.getUniqFilenamePrefix(); const prefix = `${fileHelp.getUniqFilenamePrefix()}${this.suffix}`;
const upFileName = fileHelp.getFilenameForUp(prefix); const upFileName = fileHelp.getFilenameForUp(prefix);
const downFileName = fileHelp.getFilenameForDown(prefix); const downFileName = fileHelp.getFilenameForDown(prefix);
if (this.metaDb) { if (this.metaDb) {
@ -1069,7 +1075,7 @@ export default class KnexMigrator extends SqlMigrator {
* and only filenames are migrated to _evolution table * and only filenames are migrated to _evolution table
* @memberof KnexMigrator * @memberof KnexMigrator
*/ */
async migrationsUp(args: any = {}) { async migrationsUp(args: any = {}) {
const func = this.migrationsUp.name; const func = this.migrationsUp.name;
// const result = new Result(); // const result = new Result();
@ -1623,6 +1629,13 @@ export default class KnexMigrator extends SqlMigrator {
return 'nc_evolutions'; return 'nc_evolutions';
} }
private get suffix(): string {
if (this.project?.prefix)
return `_${this.project?.prefix.slice(3,7)}`
return '';
}
} }
/** /**

3
packages/nocodb/src/lib/noco/meta/NcMetaIO.ts

@ -102,7 +102,8 @@ export default abstract class NcMetaIO {
public abstract projectCreate(projectName: string, public abstract projectCreate(projectName: string,
config: any, config: any,
description?: string): Promise<any>; description?: string,
meta?:boolean): Promise<any>;
public abstract projectUpdate(projectId: string, public abstract projectUpdate(projectId: string,
config: any): Promise<any>; config: any): Promise<any>;

25
packages/nocodb/src/lib/noco/meta/NcMetaIOImpl.ts

@ -1,5 +1,5 @@
import CryptoJS from 'crypto-js'; import CryptoJS from 'crypto-js';
import {nanoid} from 'nanoid'; import {customAlphabet} from 'nanoid'
import {NcConfig} from "../../../interface/config"; import {NcConfig} from "../../../interface/config";
import {Knex, XKnex} from "../../dataMapper"; import {Knex, XKnex} from "../../dataMapper";
@ -10,6 +10,9 @@ import NcMetaIO, {META_TABLES} from "./NcMetaIO";
import NcConnectionMgr from "../common/NcConnectionMgr"; import NcConnectionMgr from "../common/NcConnectionMgr";
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 4)
export default class NcMetaIOImpl extends NcMetaIO { export default class NcMetaIOImpl extends NcMetaIO {
@ -258,14 +261,14 @@ export default class NcMetaIOImpl extends NcMetaIO {
async commit() { async commit() {
if (this.trx) { if (this.trx) {
await this.trx.commit(); await this.trx.commit();
} }
this.trx = null; this.trx = null;
} }
async rollback(e?) { async rollback(e?) {
if (this.trx) { if (this.trx) {
await this.trx.rollback(e); await this.trx.rollback(e);
} }
this.trx = null; this.trx = null;
} }
@ -294,9 +297,17 @@ export default class NcMetaIOImpl extends NcMetaIO {
} }
} }
public async projectCreate(projectName: string, config: any, description?: string): Promise<any> { public async projectCreate(projectName: string, config: any, description?: string,
meta?: boolean): Promise<any> {
try { try {
const id = this.getProjectId(projectName); const ranId = this.getNanoId();
const id = `${projectName.toLowerCase().replace(/\W+/g, '_')}_${ranId}`;
if (meta) {
config.prefix = `nc_${ranId}__`
// if(config.envs._noco?.db?.[0]?.meta?.tn){
// config.envs._noco.db[0].meta.tn += `_${prefix}`
// }
}
config.id = id; config.id = id;
const project = { const project = {
id, id,
@ -435,8 +446,8 @@ export default class NcMetaIOImpl extends NcMetaIO {
return this.knexConnection; return this.knexConnection;
} }
private getProjectId(projectName: string) { private getNanoId() {
return `${projectName.toLowerCase().replace(/\W+/g, '_')}_${nanoid(4)}` return nanoid()
} }
public async audit(project_id: string, dbAlias: string, target: string, data: any): Promise<any> { public async audit(project_id: string, dbAlias: string, target: string, data: any): Promise<any> {

50
packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts

@ -353,6 +353,10 @@ export default class NcMetaMgr {
const data = JSON.parse(fs.readFileSync(path.join(metaFolder, `${tn}.json`), 'utf8')); const data = JSON.parse(fs.readFileSync(path.join(metaFolder, `${tn}.json`), 'utf8'));
for (const row of data) { for (const row of data) {
delete row.id; delete row.id;
row.created_at = row.created_at ? new Date(row.created_at) : null;
row.updated_at = row.updated_at ? new Date(row.updated_at) : null;
await this.xcMeta.metaInsert(projectId, dbAlias, tn, { await this.xcMeta.metaInsert(projectId, dbAlias, tn, {
...row, ...row,
db_alias: dbAlias, db_alias: dbAlias,
@ -361,7 +365,7 @@ export default class NcMetaMgr {
} }
} }
} }
this.xcMeta.commit(); await this.xcMeta.commit();
this.xcMeta.audit(projectId, dbAlias, 'nc_audit', { this.xcMeta.audit(projectId, dbAlias, 'nc_audit', {
// created_at: (Knex as any).fn.now(), // created_at: (Knex as any).fn.now(),
@ -374,7 +378,7 @@ export default class NcMetaMgr {
} catch (e) { } catch (e) {
console.log(e); console.log(e);
this.xcMeta.rollback(e); await this.xcMeta.rollback(e);
} }
} }
@ -410,6 +414,14 @@ export default class NcMetaMgr {
} else { } else {
// decrypt with old key and encrypt again with latest key // decrypt with old key and encrypt again with latest key
const projectConfig = JSON.parse(CryptoJS.AES.decrypt(projectDetails.config, projectDetails.key).toString(CryptoJS.enc.Utf8)) const projectConfig = JSON.parse(CryptoJS.AES.decrypt(projectDetails.config, projectDetails.key).toString(CryptoJS.enc.Utf8))
if (projectConfig?.prefix) {
const metaProjConfig = NcConfigFactory.makeProjectConfigFromConnection(this.config?.meta?.db, args.args.projectType);
projectConfig.envs._noco = metaProjConfig.envs._noco
}
// delete projectDetails.key; // delete projectDetails.key;
projectDetails.config = projectConfig; projectDetails.config = projectConfig;
@ -708,9 +720,9 @@ export default class NcMetaMgr {
id: row.id id: row.id
}) })
} }
trx.commit(); await trx.commit();
} catch (e) { } catch (e) {
trx.rollback(); await trx.rollback();
throw e; throw e;
} }
} }
@ -1315,28 +1327,30 @@ export default class NcMetaMgr {
config.title = args.args.title; config.title = args.args.title;
config.projectType = args.args.projectType; config.projectType = args.args.projectType;
const metaProjectsCount = await this.xcMeta.metaGet(null, null, 'nc_store', { // const metaProjectsCount = await this.xcMeta.metaGet(null, null, 'nc_store', {
key: 'NC_PROJECT_COUNT' // key: 'NC_PROJECT_COUNT'
}); // });
// todo: populate unique prefix dynamically // // todo: populate unique prefix dynamically
config.prefix = `xb${Object.keys(this.projectConfigs).length}__`; // config.prefix = `xb${Object.keys(this.projectConfigs).length}__`;
if (metaProjectsCount) { // if (metaProjectsCount) {
// todo: populate unique prefix dynamically // // todo: populate unique prefix dynamically
config.prefix = `xa${(+metaProjectsCount.value || 0) + 1}__`; // config.prefix = `xa${(+metaProjectsCount.value || 0) + 1}__`;
} // }
result = await this.xcMeta.projectCreate(config.title, config); result = await this.xcMeta.projectCreate(config.title, config, null, true);
await this.xcMeta.projectAddUser(result.id, req?.session?.passport?.user?.id, 'owner,creator'); await this.xcMeta.projectAddUser(result.id, req?.session?.passport?.user?.id, 'owner,creator');
await this.projectMgr.getSqlMgr({ await this.projectMgr.getSqlMgr({
...result, ...result,
config, config,
metaDb: this.xcMeta?.knex metaDb: this.xcMeta?.knex
}).projectOpenByWeb(config); }).projectOpenByWeb(config);
this.projectConfigs[result.id] = config; this.projectConfigs[result.id] = config;
this.xcMeta.metaUpdate(null, null, 'nc_store', { // this.xcMeta.metaUpdate(null, null, 'nc_store', {
value: ((metaProjectsCount && +metaProjectsCount.value) || 0) + 1 // value: ((metaProjectsCount && +metaProjectsCount.value) || 0) + 1
}, {key: 'NC_PROJECT_COUNT'}) // }, {key: 'NC_PROJECT_COUNT'})
this.xcMeta.audit(result?.id, null, 'nc_audit', { this.xcMeta.audit(result?.id, null, 'nc_audit', {
op_type: 'PROJECT', op_type: 'PROJECT',
@ -1352,7 +1366,7 @@ export default class NcMetaMgr {
case 'projectList': case 'projectList':
result = await this.xcMeta.userProjectList(req?.session?.passport?.user?.id); result = await this.xcMeta.userProjectList(req?.session?.passport?.user?.id);
result.forEach(p => { result.forEach(p => {
const config =JSON.parse(p.config); const config = JSON.parse(p.config);
p.projectType = config?.projectType; p.projectType = config?.projectType;
p.prefix = config?.prefix p.prefix = config?.prefix
delete p.config delete p.config

25
packages/nocodb/src/lib/noco/rest/RestAuthCtrl.ts

@ -30,7 +30,16 @@ import axios from 'axios';
import IEmailAdapter from "../../../interface/IEmailAdapter"; import IEmailAdapter from "../../../interface/IEmailAdapter";
import XcCache from "../plugins/adapters/cache/XcCache"; import XcCache from "../plugins/adapters/cache/XcCache";
passport.serializeUser(function ({id, email, email_verified, roles, provider, firstname, lastname, isAuthorized}, done) { passport.serializeUser(function ({
id,
email,
email_verified,
roles,
provider,
firstname,
lastname,
isAuthorized
}, done) {
done(null, { done(null, {
isAuthorized, isAuthorized,
id, id,
@ -651,7 +660,7 @@ export default class RestAuthCtrl {
console.log('token refresh') console.log('token refresh')
try { try {
if(!req?.cookies?.refresh_token){ if (!req?.cookies?.refresh_token) {
return res.status(400).json({msg: 'Missing refresh token'}); return res.status(400).json({msg: 'Missing refresh token'});
} }
@ -682,7 +691,7 @@ export default class RestAuthCtrl {
roles: user.roles roles: user.roles
}, this.config.auth.jwt.secret, this.config.auth.jwt.options) }, this.config.auth.jwt.secret, this.config.auth.jwt.options)
} as any); } as any);
}catch (e) { } catch (e) {
return res.status(400).json({msg: e.message}); return res.status(400).json({msg: e.message});
} }
} }
@ -1037,7 +1046,7 @@ export default class RestAuthCtrl {
} }
Tele.emit('evt', {evt_type: 'project:invite', count:count?.count}) Tele.emit('evt', {evt_type: 'project:invite', count: count?.count})
this.xcMeta.audit(req.body.project_id, null, 'nc_audit', { this.xcMeta.audit(req.body.project_id, null, 'nc_audit', {
op_type: 'AUTHENTICATION', op_type: 'AUTHENTICATION',
op_sub_type: 'INVITE', op_sub_type: 'INVITE',
@ -1265,9 +1274,9 @@ export default class RestAuthCtrl {
}, aclRow.id); }, aclRow.id);
} }
} }
this.xcMeta.commit(); await this.xcMeta.commit();
} catch (e) { } catch (e) {
this.xcMeta.rollback(e); await this.xcMeta.rollback(e);
} }
} }
} }
@ -1293,9 +1302,9 @@ export default class RestAuthCtrl {
}); });
} }
} }
this.xcMeta.commit(); await this.xcMeta.commit();
} catch (e) { } catch (e) {
this.xcMeta.rollback(e); await this.xcMeta.rollback(e);
} }
} }
} }

Loading…
Cancel
Save