mirror of https://github.com/nocodb/nocodb
Muhammed Mustafa
2 years ago
13 changed files with 380 additions and 349 deletions
@ -0,0 +1,48 @@
|
||||
import Model from '../../../../lib/models/Model'; |
||||
import Project from '../../../../lib/models/Project'; |
||||
import { orderedMetaTables } from '../../../../lib/utils/globals'; |
||||
|
||||
const dropTablesAllProjects = async (knexClient) => { |
||||
const projects = await Project.list({}); |
||||
const userCreatedTableNames = []; |
||||
await Promise.all( |
||||
projects.map(async (project) => { |
||||
await project.getBases(); |
||||
const base = project.bases && project.bases[0]; |
||||
if (!base) return; |
||||
|
||||
const models = await Model.list({ |
||||
project_id: project.id, |
||||
base_id: base.id, |
||||
}); |
||||
models.forEach((model) => { |
||||
userCreatedTableNames.push(model.table_name); |
||||
}); |
||||
}) |
||||
); |
||||
|
||||
await Promise.all( |
||||
userCreatedTableNames.map(async (tableName) => { |
||||
await knexClient.raw(`DROP TABLE ${tableName}`); |
||||
}) |
||||
); |
||||
}; |
||||
|
||||
const cleanupMetaTables = async (knexClient) => { |
||||
await knexClient.raw('SET FOREIGN_KEY_CHECKS = 0'); |
||||
for (const tableName of orderedMetaTables) { |
||||
try { |
||||
await knexClient.raw(`DELETE FROM ${tableName}`); |
||||
} catch (e) {} |
||||
} |
||||
await knexClient.raw('SET FOREIGN_KEY_CHECKS = 1'); |
||||
}; |
||||
|
||||
export default async function (knexClient) { |
||||
try { |
||||
await dropTablesAllProjects(knexClient); |
||||
await cleanupMetaTables(knexClient); |
||||
} catch (e) { |
||||
console.error('cleanupMeta', e); |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
import { sakilaTableNames } from '../dbConfig'; |
||||
|
||||
const cleanUpSakila = async (sakilaKnexClient) => { |
||||
try { |
||||
const tablesInSakilaQueryRes = await sakilaKnexClient.raw(`SHOW TABLES;`); |
||||
const tablesInSakila = tablesInSakilaQueryRes[0].map( |
||||
(table) => Object.values(table)[0] |
||||
); |
||||
|
||||
await Promise.all( |
||||
tablesInSakila |
||||
.filter((tableName) => !sakilaTableNames.includes(tableName)) |
||||
.map(async (tableName) => { |
||||
try { |
||||
await sakilaKnexClient.raw(`DROP TABLE ${tableName}`); |
||||
} catch (e) { |
||||
console.error(e); |
||||
} |
||||
}) |
||||
); |
||||
} catch (e) { |
||||
console.error('cleanUpSakila', e); |
||||
} |
||||
}; |
||||
|
||||
export default cleanUpSakila; |
@ -0,0 +1,67 @@
|
||||
import { dbConfig, dbName, sakilaDbName } from '../dbConfig'; |
||||
import { Noco } from '../../../../lib'; |
||||
import express from 'express'; |
||||
import cleanupMeta from './cleanupMeta'; |
||||
import cleanUpSakila from './cleanupSakila'; |
||||
import { createUser } from '../tests/helpers/user'; |
||||
|
||||
const knex = require('knex'); |
||||
|
||||
const serverInit = async () => { |
||||
const server = express(); |
||||
server.enable('trust proxy'); |
||||
server.use(await Noco.init()); |
||||
return server; |
||||
}; |
||||
|
||||
const resetDatabase = async (knexClient) => { |
||||
try { |
||||
if (!Noco.initialized) { |
||||
try { |
||||
await knexClient.raw(`DROP DATABASE ${dbName}`); |
||||
} catch (e) {} |
||||
await knexClient.raw(`CREATE DATABASE ${dbName}`); |
||||
} |
||||
} catch (e) { |
||||
console.error('resetDatabase', e); |
||||
} |
||||
}; |
||||
|
||||
const cleanupAllTables = async (knexClient) => { |
||||
const sakilaKnexClient = knex({ |
||||
client: 'mysql2', |
||||
connection: { |
||||
host: 'localhost', |
||||
port: '3306', |
||||
user: 'root', |
||||
password: 'password', |
||||
database: sakilaDbName, |
||||
}, |
||||
}); |
||||
|
||||
try { |
||||
await cleanUpSakila(sakilaKnexClient); |
||||
|
||||
await sakilaKnexClient.destroy(); |
||||
|
||||
await cleanupMeta(knexClient); |
||||
} catch (e) { |
||||
console.error('cleanupAllTables', e); |
||||
sakilaKnexClient.destroy(); |
||||
} |
||||
}; |
||||
|
||||
export default async function () { |
||||
const knexClient = knex(dbConfig); |
||||
await resetDatabase(knexClient); |
||||
|
||||
const server = await serverInit(); |
||||
|
||||
await cleanupAllTables(knexClient); |
||||
|
||||
await knexClient.destroy(); |
||||
|
||||
const { token } = await createUser({ app: server }, { roles: 'editor' }); |
||||
|
||||
return { app: server, token }; |
||||
} |
@ -1,57 +0,0 @@
|
||||
import { Noco } from '../../../lib'; |
||||
import express from 'express'; |
||||
import { dbConfig, dbName, sakilaDbName, sakilaTableNames } from './dbConfig'; |
||||
import { orderedMetaTables } from '../../../lib/utils/globals'; |
||||
|
||||
const knex = require('knex'); |
||||
|
||||
const clearAllMetaTables = async (knexClient) => { |
||||
await knexClient.raw(`use ${sakilaDbName}`); |
||||
const tablesInSakilaQueryRes = await knexClient.raw(`SHOW TABLES;`); |
||||
const tablesInSakila = tablesInSakilaQueryRes[0].map( |
||||
(table) => Object.values(table)[0] |
||||
); |
||||
|
||||
await Promise.all( |
||||
tablesInSakila |
||||
.filter((tableName) => !sakilaTableNames.includes(tableName)) |
||||
.map(async (tableName) => { |
||||
try { |
||||
await knexClient.raw(`DROP TABLE ${sakilaDbName}.${tableName}`); |
||||
} catch (e) { |
||||
console.error(e); |
||||
} |
||||
}) |
||||
); |
||||
|
||||
await knexClient.raw(`use ${dbName}`); |
||||
await knexClient.raw('SET FOREIGN_KEY_CHECKS = 0'); |
||||
for (const tableName of orderedMetaTables) { |
||||
try { |
||||
await knexClient.raw(`DELETE FROM ${tableName}`); |
||||
} catch (e) {} |
||||
} |
||||
await knexClient.raw('SET FOREIGN_KEY_CHECKS = 1'); |
||||
}; |
||||
|
||||
export default async function () { |
||||
const knexClient = knex(dbConfig); |
||||
try { |
||||
if (!Noco.initialized) { |
||||
try { |
||||
await knexClient.raw(`DROP DATABASE ${dbName}`); |
||||
} catch (e) {} |
||||
await knexClient.raw(`CREATE DATABASE ${dbName}`); |
||||
} |
||||
} catch (e) { |
||||
console.error(e); |
||||
} |
||||
|
||||
const server = express(); |
||||
server.enable('trust proxy'); |
||||
server.use(await Noco.init()); |
||||
|
||||
await clearAllMetaTables(knexClient); |
||||
await knexClient.destroy(); |
||||
return server; |
||||
} |
Loading…
Reference in new issue