mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-13 22:41:32 -05:00
d9c45b4967
Closes #2927 -refactor exporter to export tables that exist in the database instead of keying off of schema.js -move some shared database utility functions into their own module
90 lines
3.3 KiB
JavaScript
90 lines
3.3 KiB
JavaScript
var _ = require('lodash'),
|
||
when = require('when'),
|
||
knex = require('../../models/base').knex,
|
||
schema = require('../schema').tables,
|
||
client = require('../../models/base').client;
|
||
|
||
function createTable(table) {
|
||
return knex.schema.createTable(table, function (t) {
|
||
var column,
|
||
columnKeys = _.keys(schema[table]);
|
||
_.each(columnKeys, function (key) {
|
||
// creation distinguishes between text with fieldtype, string with maxlength and all others
|
||
if (schema[table][key].type === 'text' && schema[table][key].hasOwnProperty('fieldtype')) {
|
||
column = t[schema[table][key].type](key, schema[table][key].fieldtype);
|
||
} else if (schema[table][key].type === 'string' && schema[table][key].hasOwnProperty('maxlength')) {
|
||
column = t[schema[table][key].type](key, schema[table][key].maxlength);
|
||
} else {
|
||
column = t[schema[table][key].type](key);
|
||
}
|
||
|
||
if (schema[table][key].hasOwnProperty('nullable') && schema[table][key].nullable === true) {
|
||
column.nullable();
|
||
} else {
|
||
column.notNullable();
|
||
}
|
||
if (schema[table][key].hasOwnProperty('primary') && schema[table][key].primary === true) {
|
||
column.primary();
|
||
}
|
||
if (schema[table][key].hasOwnProperty('unique') && schema[table][key].unique) {
|
||
column.unique();
|
||
}
|
||
if (schema[table][key].hasOwnProperty('unsigned') && schema[table][key].unsigned) {
|
||
column.unsigned();
|
||
}
|
||
if (schema[table][key].hasOwnProperty('references') && schema[table][key].hasOwnProperty('inTable')) {
|
||
//check if table exists?
|
||
column.references(schema[table][key].references);
|
||
column.inTable(schema[table][key].inTable);
|
||
}
|
||
if (schema[table][key].hasOwnProperty('defaultTo')) {
|
||
column.defaultTo(schema[table][key].defaultTo);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
function deleteTable(table) {
|
||
return knex.schema.dropTableIfExists(table);
|
||
}
|
||
|
||
function getTablesFromSqlite3() {
|
||
return knex.raw("select * from sqlite_master where type = 'table'").then(function (response) {
|
||
return _.reject(_.pluck(response[0], 'tbl_name'), function (name) {
|
||
return name === 'sqlite_sequence';
|
||
});
|
||
});
|
||
}
|
||
|
||
function getTablesFromPgSQL() {
|
||
return knex.raw("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'").then(function (response) {
|
||
return _.flatten(_.pluck(response.rows, 'table_name'));
|
||
});
|
||
}
|
||
|
||
function getTablesFromMySQL() {
|
||
return knex.raw('show tables').then(function (response) {
|
||
return _.flatten(_.map(response[0], function (entry) {
|
||
return _.values(entry);
|
||
}));
|
||
});
|
||
}
|
||
|
||
function getTables() {
|
||
if (client === 'sqlite3') {
|
||
return getTablesFromSqlite3();
|
||
}
|
||
if (client === 'mysql') {
|
||
return getTablesFromMySQL();
|
||
}
|
||
if (client === 'pg') {
|
||
return getTablesFromPgSQL();
|
||
}
|
||
return when.reject("No support for database client " + client);
|
||
}
|
||
|
||
module.exports = {
|
||
createTable: createTable,
|
||
deleteTable: deleteTable,
|
||
getTables: getTables
|
||
};
|