-
Notifications
You must be signed in to change notification settings - Fork 6
/
init_db-sql.coffee
228 lines (205 loc) · 8.01 KB
/
init_db-sql.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
###
Setup Dependencies
###
require.paths.unshift __dirname
s = require __dirname + "/lib/setup"
s.ext __dirname + "/lib"
s.ext __dirname + "/support"
express = require 'express'
sys = require 'sys'
CoffeeScript = require 'coffee-script'
Auth = require 'authorization'
app = module.exports = express.createServer()
tmpcfg = require('yaml').eval(
require('fs')
.readFileSync('config/app_config.yml')
.toString('utf-8')
)
global.config = CoffeeScript.helpers.merge tmpcfg['common'], tmpcfg[app.settings.env]
app.set 'db_type', global.config.db_type
app.set 'db_host', global.config.db_host
app.set 'db_port', global.config.db_port
app.set 'db_user', global.config.db_user
app.set 'db_pass', global.config.db_pass
app.set 'db_name', global.config.db_name
app.configure 'development', () ->
console.log "configure development"
app.use express.errorHandler(dumpExceptions: true, showStack: true)
app.configure 'staging', () ->
console.log "configure staging"
app.use express.errorHandler(dumpExceptions: true, showStack: true)
app.configure 'test', () ->
console.log "configure test"
app.configure 'production', () ->
console.log "configure production"
app.use express.errorHandler()
db = require('db-'+app.set('db_type')).Database
app.sqlClient = new db(
hostname: app.set 'db_host'
port: app.set 'db_port'
database: app.set 'db_name'
user: app.set 'db_user'
password: app.set 'db_pass'
).on('error', (error) ->
console.log 'ERROR connecting to SQL server: ' + error
).on('ready', (server) ->
console.log 'Connected to ' + server.hostname + ' (' + server.version + ')'
console.log 'creating table users'
app.sqlClient.query().execute(
"CREATE TABLE IF NOT EXISTS `users` (
`id` bigint NOT NULL auto_increment,
`username` varchar(255) NOT NULL DEFAULT '',
`hashed_password` varchar(255) NOT NULL DEFAULT '',
`salt` varchar(255) NOT NULL DEFAULT '',
`firstname` varchar(255) NOT NULL DEFAULT '',
`lastname` varchar(255) NOT NULL DEFAULT '',
`email` varchar(255) NOT NULL DEFAULT '',
`last_login` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
(err) ->
if err
throw err
console.log 'users table created'
console.log "Creating default user 'admin' with password admin"
salt = Auth.makeSalt()
app.sqlClient.query().
insert('users',
['id', 'firstname', 'lastname', 'username', 'salt', 'hashed_password', 'email'],
[1, 'Project', 'Admin', 'admin', salt, Auth.createHashedPassword(salt, 'admin'), 'admin@project.com']
).execute (err, result) ->
if err
console.log 'ERROR creating user: ' + err
else if result
console.log 'User created with id: %s', result.id
)
console.log 'creating table groups'
app.sqlClient.query().execute(
"CREATE TABLE IF NOT EXISTS `groups` (
`id` bigint NOT NULL auto_increment,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
(err) ->
if err
throw err
console.log 'groups table created'
console.log "Creating default group 'Admins'"
app.sqlClient.query().
insert('groups',
['id', 'name'],
[1, 'Admins']
).execute (err, result) ->
if err
console.log 'ERROR creating group: ' + err
else if result
console.log 'Group created with id: %s', result.id
)
console.log 'creating table group_users'
app.sqlClient.query().execute(
"CREATE TABLE IF NOT EXISTS `group_users` (
`id` bigint NOT NULL auto_increment,
`group_id` bigint NOT NULL DEFAULT 0,
`user_id` bigint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `group_id` USING BTREE (`group_id`),
FOREIGN KEY (`group_id`) REFERENCES `groups`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
KEY `user_id` USING BTREE (`user_id`),
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
(err) ->
if err
throw err
console.log 'group_users table created'
console.log "Assigning user 'admin' to group 'Admins'"
app.sqlClient.query().
insert('group_users',
['id', 'group_id', 'user_id'],
[1, 1, 1]
).execute (err, result) ->
if err
console.log 'ERROR assigning user to group: ' + err
else if result
console.log 'Group assigment created with id: %s', result.id
)
console.log 'creating table roles'
app.sqlClient.query().execute(
"CREATE TABLE IF NOT EXISTS `roles` (
`id` bigint NOT NULL auto_increment,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
(err) ->
if err
throw err
console.log 'roles table created'
console.log "Creating default role 'admin'"
app.sqlClient.query().
insert('roles',
['id', 'name'],
[1, 'admin']
).execute (err, result) ->
if err
console.log 'ERROR creating role: ' + err
else if result
console.log 'Role created with id: %s', result.id
)
console.log 'creating table role_users'
app.sqlClient.query().execute(
"CREATE TABLE IF NOT EXISTS `role_users` (
`id` bigint NOT NULL auto_increment,
`role_id` bigint NOT NULL DEFAULT 0,
`user_id` bigint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `role_id` USING BTREE (`role_id`),
FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
KEY `user_id` USING BTREE (`user_id`),
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
(err) ->
if err
throw err
console.log 'role_users table created'
console.log "Assigning user 'admin' to role 'admin'"
app.sqlClient.query().
insert('role_users',
['id', 'role_id', 'user_id'],
[1, 1, 1]
).execute (err, result) ->
if err
console.log 'ERROR assigning user to role: ' + err
else if result
console.log 'Role assigment created with id: %s', result.id
)
console.log 'creating table role_groups'
app.sqlClient.query().execute(
"CREATE TABLE IF NOT EXISTS `role_groups` (
`id` bigint NOT NULL auto_increment,
`role_id` bigint NOT NULL DEFAULT 0,
`group_id` bigint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `role_id` USING BTREE (`role_id`),
FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
KEY `group_id` USING BTREE (`group_id`),
FOREIGN KEY (`group_id`) REFERENCES `groups`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
(err) ->
if err
throw err
console.log 'role_groups table created'
console.log "Assigning group 'Admins' to role 'admin'"
app.sqlClient.query().
insert('role_groups',
['id', 'role_id', 'group_id'],
[1, 1, 1]
).execute (err, result) ->
if err
console.log 'ERROR assigning group to role: ' + err
else if result
console.log 'Role assigment created with id: %s', result.id
)
)
app.sqlClient.connect()