added sqldump and sqldrop commands

This commit is contained in:
Andris Reinman 2016-04-20 20:17:53 +03:00
parent 77b3ded476
commit 0f5ac18479
10 changed files with 102 additions and 215 deletions

View file

@ -1,8 +1,3 @@
-- MySQL dump 10.13 Distrib 5.7.11, for osx10.11 (x86_64)
--
-- Host: localhost Database: mailtrain
-- ------------------------------------------------------
-- Server version 5.7.11
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
@ -15,9 +10,6 @@
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `campaign`
--
DROP TABLE IF EXISTS `campaign`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -36,21 +28,15 @@ CREATE TABLE `campaign` (
UNIQUE KEY `list` (`list`,`segment`,`subscription`),
KEY `created` (`created`),
KEY `response_id` (`response_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `campaign`
--
LOCK TABLES `campaign` WRITE;
/*!40000 ALTER TABLE `campaign` DISABLE KEYS */;
/*!40000 ALTER TABLE `campaign` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `campaign_tracker`
--
DROP TABLE IF EXISTS `campaign_tracker`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -63,21 +49,15 @@ CREATE TABLE `campaign_tracker` (
`country` varchar(2) CHARACTER SET ascii DEFAULT NULL,
`count` int(11) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`list`,`subscriber`,`link`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `campaign_tracker`
--
LOCK TABLES `campaign_tracker` WRITE;
/*!40000 ALTER TABLE `campaign_tracker` DISABLE KEYS */;
/*!40000 ALTER TABLE `campaign_tracker` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `campaigns`
--
DROP TABLE IF EXISTS `campaigns`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -111,18 +91,12 @@ CREATE TABLE `campaigns` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `campaigns`
--
LOCK TABLES `campaigns` WRITE;
/*!40000 ALTER TABLE `campaigns` DISABLE KEYS */;
/*!40000 ALTER TABLE `campaigns` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `confirmations`
--
DROP TABLE IF EXISTS `confirmations`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -141,18 +115,12 @@ CREATE TABLE `confirmations` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `confirmations`
--
LOCK TABLES `confirmations` WRITE;
/*!40000 ALTER TABLE `confirmations` DISABLE KEYS */;
/*!40000 ALTER TABLE `confirmations` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `custom_fields`
--
DROP TABLE IF EXISTS `custom_fields`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -172,21 +140,15 @@ CREATE TABLE `custom_fields` (
UNIQUE KEY `list` (`list`,`column`),
KEY `list_2` (`list`),
CONSTRAINT `custom_fields_ibfk_1` FOREIGN KEY (`list`) REFERENCES `lists` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `custom_fields`
--
LOCK TABLES `custom_fields` WRITE;
/*!40000 ALTER TABLE `custom_fields` DISABLE KEYS */;
/*!40000 ALTER TABLE `custom_fields` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `importer`
--
DROP TABLE IF EXISTS `importer`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -207,21 +169,15 @@ CREATE TABLE `importer` (
PRIMARY KEY (`id`),
KEY `list` (`list`),
CONSTRAINT `importer_ibfk_1` FOREIGN KEY (`list`) REFERENCES `lists` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `importer`
--
LOCK TABLES `importer` WRITE;
/*!40000 ALTER TABLE `importer` DISABLE KEYS */;
/*!40000 ALTER TABLE `importer` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `links`
--
DROP TABLE IF EXISTS `links`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -237,21 +193,15 @@ CREATE TABLE `links` (
UNIQUE KEY `campaign_2` (`campaign`,`url`),
KEY `campaign` (`campaign`),
CONSTRAINT `links_ibfk_1` FOREIGN KEY (`campaign`) REFERENCES `campaigns` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `links`
--
LOCK TABLES `links` WRITE;
/*!40000 ALTER TABLE `links` DISABLE KEYS */;
/*!40000 ALTER TABLE `links` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `lists`
--
DROP TABLE IF EXISTS `lists`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -269,18 +219,12 @@ CREATE TABLE `lists` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `lists`
--
LOCK TABLES `lists` WRITE;
/*!40000 ALTER TABLE `lists` DISABLE KEYS */;
/*!40000 ALTER TABLE `lists` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `segment_rules`
--
DROP TABLE IF EXISTS `segment_rules`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -296,18 +240,12 @@ CREATE TABLE `segment_rules` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `segment_rules`
--
LOCK TABLES `segment_rules` WRITE;
/*!40000 ALTER TABLE `segment_rules` DISABLE KEYS */;
/*!40000 ALTER TABLE `segment_rules` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `segments`
--
DROP TABLE IF EXISTS `segments`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -325,18 +263,12 @@ CREATE TABLE `segments` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `segments`
--
LOCK TABLES `segments` WRITE;
/*!40000 ALTER TABLE `segments` DISABLE KEYS */;
/*!40000 ALTER TABLE `segments` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `settings`
--
DROP TABLE IF EXISTS `settings`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -350,9 +282,6 @@ CREATE TABLE `settings` (
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `settings`
--
LOCK TABLES `settings` WRITE;
/*!40000 ALTER TABLE `settings` DISABLE KEYS */;
@ -360,9 +289,6 @@ INSERT INTO `settings` VALUES (1,'smtp_hostname','localhost'),(2,'smtp_port','46
/*!40000 ALTER TABLE `settings` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `subscription`
--
DROP TABLE IF EXISTS `subscription`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -390,18 +316,12 @@ CREATE TABLE `subscription` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `subscription`
--
LOCK TABLES `subscription` WRITE;
/*!40000 ALTER TABLE `subscription` DISABLE KEYS */;
/*!40000 ALTER TABLE `subscription` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `templates`
--
DROP TABLE IF EXISTS `templates`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -418,18 +338,12 @@ CREATE TABLE `templates` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `templates`
--
LOCK TABLES `templates` WRITE;
/*!40000 ALTER TABLE `templates` DISABLE KEYS */;
/*!40000 ALTER TABLE `templates` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -450,9 +364,6 @@ CREATE TABLE `users` (
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `users`
--
LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
@ -468,5 +379,3 @@ UNLOCK TABLES;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2016-04-04 14:25:59

39
setup/sql/drop.js Normal file
View file

@ -0,0 +1,39 @@
'use strict';
let config = require('config');
let spawn = require('child_process').spawn;
let log = require('npmlog');
let path = require('path');
log.level = 'verbose';
function createDump(callback) {
let cmd = spawn(path.join(__dirname, 'drop.sh'), [], {
env: {
MYSQL_HOST: config.mysql.host || 'localhost',
MYSQL_DB: config.mysql.database,
MYSQL_PORT: config.mysql.port || 3306,
MYSQL_USER: config.mysql.user,
MYSQL_PASSWORD: config.mysql.password
}
});
cmd.stdout.pipe(process.stdout);
cmd.stderr.pipe(process.stderr);
cmd.on('close', code => {
if (code) {
return callback(new Error('drop command exited with code ' + code));
}
return callback(null, true);
});
}
createDump(err => {
if (err) {
log.error('sqldrop', err);
process.exit(1);
}
log.info('sqldrop', 'Command completed, all tables dropped from "%s"', config.mysql.database);
process.exit(0);
});

3
setup/sql/drop.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "-p${MYSQL_PASSWORD}" --add-drop-table --no-data "$MYSQL_DB" | grep -e '^DROP \| FOREIGN_KEY_CHECKS' | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "-p${MYSQL_PASSWORD}" "$MYSQL_DB"

30
setup/sql/dump.js Normal file
View file

@ -0,0 +1,30 @@
'use strict';
let config = require('config');
let spawn = require('child_process').spawn;
let log = require('npmlog');
log.level = 'verbose';
function createDump(callback) {
let cmd = spawn('mysqldump', ['-h', config.mysql.host || 'localhost', '-P', config.mysql.port || 3306, '-u', config.mysql.user, '-p' + config.mysql.password, '--opt', config.mysql.database]);
cmd.stdout.pipe(process.stdout);
cmd.stderr.pipe(process.stderr);
cmd.on('close', code => {
if (code) {
return callback(new Error('mysqldump command exited with code ' + code));
}
return callback(null, true);
});
}
createDump(err => {
if (err) {
log.error('sqldump', err);
process.exit(1);
}
log.info('sqldump', 'MySQL Dump Completed');
process.exit(0);
});

View file

@ -1,8 +1,3 @@
-- MySQL dump 10.16 Distrib 10.1.12-MariaDB, for osx10.11 (x86_64)
--
-- Host: localhost Database: frontmail
-- ------------------------------------------------------
-- Server version 10.1.12-MariaDB
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
@ -15,9 +10,6 @@
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `campaign`
--
DROP TABLE IF EXISTS `campaign`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -36,21 +28,15 @@ CREATE TABLE `campaign` (
UNIQUE KEY `list` (`list`,`segment`,`subscription`),
KEY `created` (`created`),
KEY `response_id` (`response_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `campaign`
--
LOCK TABLES `campaign` WRITE;
/*!40000 ALTER TABLE `campaign` DISABLE KEYS */;
/*!40000 ALTER TABLE `campaign` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `campaign_tracker`
--
DROP TABLE IF EXISTS `campaign_tracker`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -63,21 +49,15 @@ CREATE TABLE `campaign_tracker` (
`country` varchar(2) CHARACTER SET ascii DEFAULT NULL,
`count` int(11) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`list`,`subscriber`,`link`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `campaign_tracker`
--
LOCK TABLES `campaign_tracker` WRITE;
/*!40000 ALTER TABLE `campaign_tracker` DISABLE KEYS */;
/*!40000 ALTER TABLE `campaign_tracker` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `campaigns`
--
DROP TABLE IF EXISTS `campaigns`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -111,18 +91,12 @@ CREATE TABLE `campaigns` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `campaigns`
--
LOCK TABLES `campaigns` WRITE;
/*!40000 ALTER TABLE `campaigns` DISABLE KEYS */;
/*!40000 ALTER TABLE `campaigns` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `confirmations`
--
DROP TABLE IF EXISTS `confirmations`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -141,18 +115,12 @@ CREATE TABLE `confirmations` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `confirmations`
--
LOCK TABLES `confirmations` WRITE;
/*!40000 ALTER TABLE `confirmations` DISABLE KEYS */;
/*!40000 ALTER TABLE `confirmations` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `custom_fields`
--
DROP TABLE IF EXISTS `custom_fields`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -160,9 +128,9 @@ DROP TABLE IF EXISTS `custom_fields`;
CREATE TABLE `custom_fields` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`list` int(11) unsigned NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 DEFAULT '',
`name` varchar(255) DEFAULT '',
`key` varchar(100) CHARACTER SET ascii NOT NULL,
`default_value` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
`default_value` varchar(255) DEFAULT NULL,
`type` varchar(255) CHARACTER SET ascii NOT NULL DEFAULT '',
`group` int(11) unsigned DEFAULT NULL,
`column` varchar(255) CHARACTER SET ascii DEFAULT NULL,
@ -172,21 +140,15 @@ CREATE TABLE `custom_fields` (
UNIQUE KEY `list` (`list`,`column`),
KEY `list_2` (`list`),
CONSTRAINT `custom_fields_ibfk_1` FOREIGN KEY (`list`) REFERENCES `lists` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `custom_fields`
--
LOCK TABLES `custom_fields` WRITE;
/*!40000 ALTER TABLE `custom_fields` DISABLE KEYS */;
/*!40000 ALTER TABLE `custom_fields` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `import_failed`
--
DROP TABLE IF EXISTS `import_failed`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -203,18 +165,12 @@ CREATE TABLE `import_failed` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `import_failed`
--
LOCK TABLES `import_failed` WRITE;
/*!40000 ALTER TABLE `import_failed` DISABLE KEYS */;
/*!40000 ALTER TABLE `import_failed` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `importer`
--
DROP TABLE IF EXISTS `importer`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -230,26 +186,20 @@ CREATE TABLE `importer` (
`error` varchar(255) DEFAULT NULL,
`processed` int(11) unsigned NOT NULL DEFAULT '0',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`mapping` text CHARACTER SET utf8mb4 NOT NULL,
`mapping` text NOT NULL,
`finished` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `list` (`list`),
CONSTRAINT `importer_ibfk_1` FOREIGN KEY (`list`) REFERENCES `lists` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `importer`
--
LOCK TABLES `importer` WRITE;
/*!40000 ALTER TABLE `importer` DISABLE KEYS */;
/*!40000 ALTER TABLE `importer` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `links`
--
DROP TABLE IF EXISTS `links`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -265,21 +215,15 @@ CREATE TABLE `links` (
UNIQUE KEY `campaign_2` (`campaign`,`url`),
KEY `campaign` (`campaign`),
CONSTRAINT `links_ibfk_1` FOREIGN KEY (`campaign`) REFERENCES `campaigns` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `links`
--
LOCK TABLES `links` WRITE;
/*!40000 ALTER TABLE `links` DISABLE KEYS */;
/*!40000 ALTER TABLE `links` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `lists`
--
DROP TABLE IF EXISTS `lists`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -297,18 +241,12 @@ CREATE TABLE `lists` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `lists`
--
LOCK TABLES `lists` WRITE;
/*!40000 ALTER TABLE `lists` DISABLE KEYS */;
/*!40000 ALTER TABLE `lists` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `segment_rules`
--
DROP TABLE IF EXISTS `segment_rules`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -324,18 +262,12 @@ CREATE TABLE `segment_rules` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `segment_rules`
--
LOCK TABLES `segment_rules` WRITE;
/*!40000 ALTER TABLE `segment_rules` DISABLE KEYS */;
/*!40000 ALTER TABLE `segment_rules` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `segments`
--
DROP TABLE IF EXISTS `segments`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -353,18 +285,12 @@ CREATE TABLE `segments` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `segments`
--
LOCK TABLES `segments` WRITE;
/*!40000 ALTER TABLE `segments` DISABLE KEYS */;
/*!40000 ALTER TABLE `segments` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `settings`
--
DROP TABLE IF EXISTS `settings`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -378,9 +304,6 @@ CREATE TABLE `settings` (
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `settings`
--
LOCK TABLES `settings` WRITE;
/*!40000 ALTER TABLE `settings` DISABLE KEYS */;
@ -388,9 +311,6 @@ INSERT INTO `settings` VALUES (1,'smtp_hostname','localhost'),(2,'smtp_port','46
/*!40000 ALTER TABLE `settings` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `subscription`
--
DROP TABLE IF EXISTS `subscription`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -418,18 +338,12 @@ CREATE TABLE `subscription` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `subscription`
--
LOCK TABLES `subscription` WRITE;
/*!40000 ALTER TABLE `subscription` DISABLE KEYS */;
/*!40000 ALTER TABLE `subscription` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `templates`
--
DROP TABLE IF EXISTS `templates`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -446,18 +360,12 @@ CREATE TABLE `templates` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `templates`
--
LOCK TABLES `templates` WRITE;
/*!40000 ALTER TABLE `templates` DISABLE KEYS */;
/*!40000 ALTER TABLE `templates` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
@ -478,13 +386,10 @@ CREATE TABLE `users` (
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `users`
--
LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES (1,'admin','$2a$10$mzKU71G62evnGB2PvQA4k..Wf9jASk.c7a8zRMHh6qQVjYJ2r/g/K','admin@example.com',NULL,NULL,'2016-04-19 08:51:04');
INSERT INTO `users` VALUES (1,'admin','$2a$10$mzKU71G62evnGB2PvQA4k..Wf9jASk.c7a8zRMHh6qQVjYJ2r/g/K','admin@example.com',NULL,NULL,'2016-04-20 17:15:36');
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
@ -497,4 +402,3 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2016-04-19 11:53:09

183
setup/sql/sql.js Normal file
View file

@ -0,0 +1,183 @@
'use strict';
let config = require('config');
let db = require('../../lib/db');
let spawn = require('child_process').spawn;
let settings = require('../../lib/models/settings');
let log = require('npmlog');
let fs = require('fs');
let pathlib = require('path');
let Handlebars = require('handlebars');
log.level = 'verbose';
function listTables(callback) {
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
let query = 'SHOW TABLES';
connection.query(query, (err, rows) => {
connection.release();
if (err) {
return callback(err);
}
let tables = {};
[].concat(rows || []).forEach(row => {
let name;
let table;
Object.keys(row).forEach(key => {
if (/^Tables_in_/i.test(key)) {
table = name = row[key];
}
});
if (/__\d+$/.test(name)) {
let parts = name.split('__');
parts.pop();
table = parts.join('__');
}
if (tables.hasOwnProperty(table)) {
tables[table].push(name);
} else {
tables[table] = [name];
}
return table;
});
return callback(null, tables);
});
});
}
function getSchemaVersion(callback) {
settings.list(['db_schema_version'], (err, configItems) => {
if (err) {
return callback(err);
}
let dbSchemaVersion = Number(configItems.dbSchemaVersion) || 0;
callback(null, dbSchemaVersion);
});
}
function listUpdates(current, callback) {
current = current || 0;
fs.readdir(__dirname, (err, list) => {
if (err) {
return callback(err);
}
let updates = [];
[].concat(list || []).forEach(row => {
if (/^upgrade-\d+\.sql$/i.test(row)) {
let seq = row.match(/\d+/)[0];
if (seq > current) {
updates.push({
seq: Number(seq),
path: pathlib.join(__dirname, row)
});
}
}
});
return callback(null, updates);
});
}
function getSql(path, data, callback) {
fs.readFile(path, 'utf-8', (err, source) => {
if (err) {
return callback(err);
}
let renderer = Handlebars.compile(source);
return callback(null, renderer(data || {}));
});
}
function runInitial(callback) {
let path = pathlib.join(__dirname, process.env.FROM_START ? 'base.sql' : 'mailtrain.sql');
applyUpdate({
path
}, callback);
}
function runUpdates(callback) {
listTables((err, tables) => {
if (err) {
return callback(err);
}
if (!tables.settings) {
log.info('sql', 'SQL not set up, initializing');
return runInitial(callback);
}
getSchemaVersion((err, schemaVersion) => {
if (err) {
return callback(err);
}
listUpdates(schemaVersion, (err, updates) => {
if (err) {
return callback(err);
}
let pos = 0;
let runNext = () => {
if (pos >= updates.length) {
return callback(null, pos);
}
let update = updates[pos++];
update.data = {
tables
};
applyUpdate(update, (err, status) => {
if (err) {
return callback(err);
}
if (status) {
log.info('sql', 'Update %s applied', update.seq);
} else {
log.info('sql', 'Update %s not applied', update.seq);
}
runNext();
});
};
runNext();
});
});
});
}
function applyUpdate(update, callback) {
getSql(update.path, update.data, (err, sql) => {
if (err) {
return callback(err);
}
let cmd = spawn(config.mysql.command || 'mysql', ['-h', config.mysql.host || 'localhost', '-P', config.mysql.port || 3306, '-u', config.mysql.user, '-p' + config.mysql.password, '-D', config.mysql.database]);
cmd.stdout.pipe(process.stdout);
cmd.stderr.pipe(process.stderr);
cmd.on('close', code => {
if (code) {
return callback(new Error('mysql command exited with code ' + code));
}
return callback(null, true);
});
cmd.stdin.end(sql);
});
}
runUpdates(err => {
if (err) {
log.error('sql', err);
process.exit(1);
}
log.info('sql', 'Database update completed');
process.exit(0);
});