From 0f5ac18479717b65acc6830af5dfa44a837b0901 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Wed, 20 Apr 2016 20:17:53 +0300 Subject: [PATCH] added sqldump and sqldrop commands --- README.md | 2 +- package.json | 6 +- setup/sql/base.sql | 101 +--------------- setup/sql/drop.js | 39 ++++++ setup/sql/drop.sh | 3 + setup/sql/dump.js | 30 +++++ setup/sql/mailtrain.sql | 114 ++---------------- setup/{ => sql}/sql.js | 22 ++-- .../{update-00001.sql => upgrade-00001.sql} | 0 ...date-template.sql => upgrade-template.sql} | 0 10 files changed, 102 insertions(+), 215 deletions(-) create mode 100644 setup/sql/drop.js create mode 100755 setup/sql/drop.sh create mode 100644 setup/sql/dump.js rename setup/{ => sql}/sql.js (87%) rename setup/sql/{update-00001.sql => upgrade-00001.sql} (100%) rename setup/sql/{update-template.sql => upgrade-template.sql} (100%) diff --git a/README.md b/README.md index a92bee72..349b0a19 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Subscribe to Mailtrain Newsletter [here](http://mailtrain.org/subscription/EysIv 1. Download Mailtrain files using git: `git clone git://github.com/andris9/mailtrain.git` and open Mailtrain folder `cd mailtrain` 2. Run `npm install` in the Mailtrain folder to install required dependencies 3. Copy [config/default.toml](config/default.toml) as `config/production.toml` and update MySQL and any other settings in it - 4. Import SQL tables by running `npm run sql` (invokes `mysql` command in the background, so you should have it installed). If MySQL configuration in config file is not correct then this command fails + 4. Import SQL tables by running `npm run sql` (invokes `mysql` command in the background, so you should have it installed. If you can't use this command, then you can import the [database file](setup/sql/mailtrain.sql) yourself). If MySQL configuration in config file is not correct then this command fails. 5. Run the server `NODE_ENV=production npm start` 6. Open [http://localhost:3000/](http://localhost:3000/) 7. Authenticate as `admin`:`test` diff --git a/package.json b/package.json index 8b2ba3d4..db424d7b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "scripts": { "test": "grunt", "start": "node index.js", - "sql": "node setup/sql.js" + "sql": "node setup/sql/sql.js", + "sqldump": "node setup/sql/dump.js | sed -e '/^--.*$/d' > setup/sql/mailtrain.sql", + "sqldrop": "node setup/sql/drop.js" }, "repository": { "type": "git", @@ -53,7 +55,7 @@ "openpgp": "^2.2.1", "passport": "^0.3.2", "passport-local": "^1.0.0", - "request": "^2.71.0", + "request": "^2.72.0", "serve-favicon": "^2.3.0", "shortid": "^2.2.6", "slugify": "^0.1.1", diff --git a/setup/sql/base.sql b/setup/sql/base.sql index b47d5bf5..a3b32b2f 100644 --- a/setup/sql/base.sql +++ b/setup/sql/base.sql @@ -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 diff --git a/setup/sql/drop.js b/setup/sql/drop.js new file mode 100644 index 00000000..98f8fd08 --- /dev/null +++ b/setup/sql/drop.js @@ -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); +}); diff --git a/setup/sql/drop.sh b/setup/sql/drop.sh new file mode 100755 index 00000000..0f84d353 --- /dev/null +++ b/setup/sql/drop.sh @@ -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" diff --git a/setup/sql/dump.js b/setup/sql/dump.js new file mode 100644 index 00000000..39988401 --- /dev/null +++ b/setup/sql/dump.js @@ -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); +}); diff --git a/setup/sql/mailtrain.sql b/setup/sql/mailtrain.sql index 2388a4ac..7c3fc885 100644 --- a/setup/sql/mailtrain.sql +++ b/setup/sql/mailtrain.sql @@ -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 diff --git a/setup/sql.js b/setup/sql/sql.js similarity index 87% rename from setup/sql.js rename to setup/sql/sql.js index b17514e8..f570fec8 100644 --- a/setup/sql.js +++ b/setup/sql/sql.js @@ -1,9 +1,9 @@ 'use strict'; let config = require('config'); -let db = require('../lib/db'); +let db = require('../../lib/db'); let spawn = require('child_process').spawn; -let settings = require('../lib/models/settings'); +let settings = require('../../lib/models/settings'); let log = require('npmlog'); let fs = require('fs'); let pathlib = require('path'); @@ -62,19 +62,19 @@ function getSchemaVersion(callback) { function listUpdates(current, callback) { current = current || 0; - fs.readdir(__dirname + '/sql', (err, list) => { + fs.readdir(__dirname, (err, list) => { if (err) { return callback(err); } let updates = []; [].concat(list || []).forEach(row => { - if (/^update-\d+\.sql$/i.test(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, 'sql', row) + path: pathlib.join(__dirname, row) }); } } @@ -95,7 +95,7 @@ function getSql(path, data, callback) { } function runInitial(callback) { - let path = pathlib.join(__dirname, 'sql', 'mailtrain.sql'); + let path = pathlib.join(__dirname, process.env.FROM_START ? 'base.sql' : 'mailtrain.sql'); applyUpdate({ path }, callback); @@ -108,7 +108,7 @@ function runUpdates(callback) { } if (!tables.settings) { - log.info('Initial', 'SQL not set up, initializing'); + log.info('sql', 'SQL not set up, initializing'); return runInitial(callback); } @@ -136,9 +136,9 @@ function runUpdates(callback) { return callback(err); } if (status) { - log.info('Update', 'Update %s applied', update.seq); + log.info('sql', 'Update %s applied', update.seq); } else { - log.info('Update', 'Update %s not applied', update.seq); + log.info('sql', 'Update %s not applied', update.seq); } runNext(); @@ -175,9 +175,9 @@ function applyUpdate(update, callback) { runUpdates(err => { if (err) { - log.error('RESULT', err); + log.error('sql', err); process.exit(1); } - log.info('RESULT', 'SQL Upgrade Completed'); + log.info('sql', 'Database update completed'); process.exit(0); }); diff --git a/setup/sql/update-00001.sql b/setup/sql/upgrade-00001.sql similarity index 100% rename from setup/sql/update-00001.sql rename to setup/sql/upgrade-00001.sql diff --git a/setup/sql/update-template.sql b/setup/sql/upgrade-template.sql similarity index 100% rename from setup/sql/update-template.sql rename to setup/sql/upgrade-template.sql