Skip to content

Commit 913d487

Browse files
committed
Fix migrations and docker compose & add tables plain sql as documentation
1 parent dccfe4c commit 913d487

6 files changed

Lines changed: 51 additions & 31 deletions

File tree

Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,5 @@ RUN npm install --global nodemon db-migrate db-migrate-mysql
1515
# Install dependencies from package.json
1616
RUN npm install --quiet --no-warnings
1717

18-
# Run database migrations
19-
#RUN db-migrate up
20-
2118
# Expose port from container so host can access 1337
2219
EXPOSE 1337

core/db.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ exports.isValidApiKey = function(secret, callback){
4040
pool.getConnection(function(err, connection) {
4141
if(err) { console.log(err); callback(true); return; }
4242

43-
let sql = "SELECT valid from apikeys WHERE secret = '?';";
43+
let sql = "SELECT * from apikeys WHERE secret = ?;";
4444

4545
// make the query
4646
connection.query(sql, [secret], function(err, results) {
@@ -74,7 +74,7 @@ exports.getUserByHash = function(hash, callback){
7474
pool.getConnection(function(err, connection) {
7575
if (err) { console.log(err); callback(true); return; }
7676

77-
let sql = "SELECT email, firstname from entries WHERE confirm_key = '?';";
77+
let sql = "SELECT email, firstname from entries WHERE confirm_key = ?;";
7878

7979
// make the query
8080
connection.query(sql, [hash], function(err, results) {
@@ -121,7 +121,7 @@ exports.saveEntry = function(fields, callback){
121121
if(err) { console.log(err); callback(true); return; }
122122
let data = prepareEntry(fields);
123123

124-
let sqlEmailExists = "SELECT count(*) as cnt FROM entries WHERE email = '?';";
124+
let sqlEmailExists = "SELECT count(*) as cnt FROM entries WHERE email = ?;";
125125
connection.query(sqlEmailExists, [data.email], function(err, results) {
126126
if(!err) {
127127
if(results[0]['cnt'] > 0){
@@ -130,7 +130,7 @@ exports.saveEntry = function(fields, callback){
130130
}else{
131131
let sql = "INSERT INTO entries (firstname, lastname, email, country, message, anon, ipv4, image, "
132132
+ "created_at, updated_at, confirm_key, beta, newsletter, pax) "
133-
+ "VALUES ('?', '?', '?', '?', '?', ?, '?', '?', ?, ?, '?', ?, ?, ?);";
133+
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
134134

135135
// run the query
136136
connection.query(
@@ -153,6 +153,7 @@ exports.saveEntry = function(fields, callback){
153153
],
154154
function(err, results) {
155155
connection.release();
156+
console.log('this.sql', this.sql); //command/query
156157
if(err) { callback(true); return; }
157158
callback(false, results);
158159
}

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ services:
2525
- MYSQL_PASS=123456
2626
depends_on:
2727
- db
28-
command: bash -c "db-migrate up && npm run start"
28+
command: bash -c "sleep 5 && db-migrate up && npm run start"
2929
#entrypoint: /docker-entrypoint.sh

documentation/tables.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CREATE TABLE IF NOT EXISTS `apikeys` (
2+
`secret` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
3+
`valid` int(1) DEFAULT NULL,
4+
UNIQUE KEY `secret` (`secret`)
5+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
6+
7+
CREATE TABLE IF NOT EXISTS `entries` (
8+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
9+
`firstname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
10+
`lastname` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
11+
`email` varchar(320) COLLATE utf8mb4_unicode_ci NOT NULL,
12+
`country` char(2) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
13+
`message` text COLLATE utf8mb4_unicode_ci NOT NULL,
14+
`anon` int(1) DEFAULT '0',
15+
`ipv4` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
16+
`image` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
17+
`created_at` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
18+
`updated_at` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
19+
`confirm_key` varchar(75) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
20+
`beta` int(1) DEFAULT '0',
21+
`newsletter` int(1) DEFAULT '0',
22+
`pax` int(1) DEFAULT '0',
23+
`email_confirmed` int(1) DEFAULT '0',
24+
`status` int(1) DEFAULT '0',
25+
PRIMARY KEY (`id`)
26+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

migrations/20190206134449-entries.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ exports.up = function(db, callback) {
5555
},
5656
ipv4: {
5757
type: 'string',
58-
length: 15,
58+
length: 40,
5959
defaultValue : null,
6060
},
6161
image: {
@@ -64,30 +64,43 @@ exports.up = function(db, callback) {
6464
defaultValue : null,
6565
},
6666
created_at: {
67-
type: 'int',
68-
length: 11,
67+
type: 'string',
68+
length: 15,
6969
defaultValue : null,
7070
},
7171
updated_at: {
72-
type: 'int',
73-
length: 11,
72+
type: 'string',
73+
length: 15,
7474
defaultValue : null,
7575
},
7676
confirm_key: {
7777
type: 'string',
78-
length: 60,
78+
length: 75,
7979
defaultValue : null,
8080
},
8181
beta: {
8282
type: 'int',
83+
length: 1,
8384
defaultValue : 0,
8485
},
8586
newsletter: {
8687
type: 'int',
88+
length: 1,
8789
defaultValue : 0,
8890
},
8991
pax: {
9092
type: 'int',
93+
length: 1,
94+
defaultValue : 0,
95+
},
96+
email_confirmed: {
97+
type: 'int',
98+
length: 1,
99+
defaultValue : 0,
100+
},
101+
status: {
102+
type: 'int',
103+
length: 1,
91104
defaultValue : 0,
92105
},
93106
}, function(err) {

migrations/20190206140226-apikeys.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@ exports.setup = function(options, seedLink) {
1616

1717
exports.up = function(db, callback) {
1818
db.createTable('apikeys', {
19-
entry_id: {
20-
type: 'int',
21-
unsigned: true,
22-
length: 11,
23-
notNull: true,
24-
foreignKey: {
25-
name: 'fk_entry_id',
26-
table: 'entries',
27-
rules: {
28-
onDelete: 'RESTRICT',
29-
onUpdate: 'RESTRICT'
30-
},
31-
mapping: {
32-
entry_id: 'id'
33-
}
34-
}
35-
},
3619
secret: {
3720
type: 'string',
3821
length: 100,

0 commit comments

Comments
 (0)