Skip to content

Maria db #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"prefer-template": 0,
"global-require": 0,
"no-unused-expressions": 0,
"no-unused-vars": 0,
"no-undef": 0,
"no-underscore-dangle": 0,
"import/no-dynamic-require": 0
},
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ COPY package.json .
RUN npm install

# Global Installs
RUN npm install -g pm2
RUN npm install -g pm2 sequelize-cli

COPY . .
39 changes: 0 additions & 39 deletions bot/models/db.js

This file was deleted.

1 change: 1 addition & 0 deletions db.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Migrations with Sequelize cli](https://codeburst.io/creating-a-dynamic-database-using-postgres-and-sequelize-5264cea5a2d)
23 changes: 23 additions & 0 deletions db/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "root",
"password": "down_and_dirty_deeds",
"database": "max",
"host": "mysql",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "max_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "max_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
9 changes: 9 additions & 0 deletions db/first_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

echo "Migrating"
sequelize db:migrate --config db/config/config.json --migrations-path db/migrations/
echo "Seeding"
sequelize db:seed:undo:all --config db/config/config.json --migrations-path db/migrations/ --seeders-path db/seeders
sequelize db:seed:all --config db/config/config.json --migrations-path db/migrations/ --seeders-path db/seeders
echo "Running PM2"
pm2 start max.config.js --no-daemon
38 changes: 38 additions & 0 deletions db/migrations/20170920201305-create-member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Members', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
discorduser: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
uuid: {
type: Sequelize.STRING
},
verified: {
type: Sequelize.BOOLEAN
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now'),
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Members');
}
};
38 changes: 38 additions & 0 deletions db/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

const basename = path.basename(module.filename);
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '/../config/config.json'))[env];

const db = {};

let sequelize;

if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
16 changes: 16 additions & 0 deletions db/models/member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Member = sequelize.define('Member', {
discorduser: DataTypes.STRING,
email: DataTypes.STRING,
uuid: DataTypes.STRING,
verified: DataTypes.BOOLEAN
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Member;
};
18 changes: 18 additions & 0 deletions db/seeders/20170921151505-test-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const uuidv4 = require('uuid/v4');

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Members', [{
discorduser: '226894986262740993',
email: 'chapman@apextion.com',
uuid: uuidv4(),
verified: 0,
}], {});
},

down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Members', null, {});
}
};
9 changes: 9 additions & 0 deletions db/test_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const models = require('../models');
const uuidv4 = require('uuid/v4');

models.Member.create({
discorduser: '@derp',
email: 'chapman@apextion.com',
uuid: uuidv4(),
verified: 0
}).then(console.log).catch(console.error);
18 changes: 17 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@ services:
volumes:
- /usr/src/app/node_modules
restart: always
command: npm start
command: ["./wait-for-it.sh", "mysql:3306", "--", "db/first_run.sh"]

mysql:
container_name: mariadb
restart: always
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: 'down_and_dirty_deeds'
MYSQL_USER: 'max_attack'
MYSQL_PASS: 'headroom_dead_batteries'
MYSQL_DATABASE: 'max'
volumes:
- my-datavolume:/var/lib/mysql
ports:
- "3306:3306"
volumes:
my-datavolume:
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
"gulp": "^3.9.1",
"mocha": "^3.5.0",
"mocha-eslint": "^4.1.0",
"mysql": "^2.14.1",
"sequelize": "^4.4.10",
"mysql2": "^1.4.2",
"sequelize": "^4.9.0",
"uuid": "^3.1.0",
"yargs": "^8.0.2"
}
}
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Use docker-compose to start the container with the bot client.
docker-compose up --build
```

To Stop the bot run the command:

```
docker-compose stop
```

> Note: Any changes to the codebase will require you to close the Docker container and re 'up'.

If all went well, and your **DEBUG_MODE** is set properly (See the chart below) you will see a logged message of ```Bot Online and Ready!:```
Expand Down
Loading