-
Notifications
You must be signed in to change notification settings - Fork 71
/
migrate.php
155 lines (134 loc) · 5 KB
/
migrate.php
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
<?php
define('APP_PATH' , __DIR__.'/app/');
$config = array();
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/app/config/database.php';
use Illuminate\Database\Capsule\Manager as Capsule;
use Cartalyst\Sentry\Facades\Native\Sentry;
class Migrator{
protected $config;
public function __construct($config){
$this->config = $config;
$this->makeConnection($config);
}
/**
* create connection to the database based on given configuration
*/
private function makeConnection($config){
try{
$capsule = new Capsule;
$capsule->addConnection($config);
$capsule->setAsGlobal();
$capsule->bootEloquent();
Sentry::setupDatabaseResolver($capsule->connection()->getPdo());
}catch(Exception $e){
throw $e;
}
}
/**
* migrate the database schema
*/
public function migrate(){
/**
* create table for sentry user
*/
if (!Capsule::schema()->hasTable('users')){
Capsule::schema()->create('users', function($table)
{
$table->increments('id');
$table->string('email');
$table->string('password');
$table->text('permissions')->nullable();
$table->boolean('activated')->default(0);
$table->string('activation_code')->nullable();
$table->timestamp('activated_at')->nullable();
$table->timestamp('last_login')->nullable();
$table->string('persist_code')->nullable();
$table->string('reset_password_code')->nullable();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->timestamps();
// We'll need to ensure that MySQL uses the InnoDB engine to
// support the indexes, other engines aren't affected.
$table->engine = 'InnoDB';
$table->unique('email');
$table->index('activation_code');
$table->index('reset_password_code');
});
}
/**
* create table for sentry group
*/
if (!Capsule::schema()->hasTable('groups')){
Capsule::schema()->create('groups', function($table)
{
$table->increments('id');
$table->string('name');
$table->text('permissions')->nullable();
$table->timestamps();
// We'll need to ensure that MySQL uses the InnoDB engine to
// support the indexes, other engines aren't affected.
$table->engine = 'InnoDB';
$table->unique('name');
});
}
/**
* create user-group relation
*/
if (!Capsule::schema()->hasTable('users_groups')){
Capsule::schema()->create('users_groups', function($table)
{
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
// We'll need to ensure that MySQL uses the InnoDB engine to
// support the indexes, other engines aren't affected.
$table->engine = 'InnoDB';
$table->primary(array('user_id', 'group_id'));
});
}
/**
* create throttle table
*/
if (!Capsule::schema()->hasTable('throttle')){
Capsule::schema()->create('throttle', function($table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('ip_address')->nullable();
$table->integer('attempts')->default(0);
$table->boolean('suspended')->default(0);
$table->boolean('banned')->default(0);
$table->timestamp('last_attempt_at')->nullable();
$table->timestamp('suspended_at')->nullable();
$table->timestamp('banned_at')->nullable();
// We'll need to ensure that MySQL uses the InnoDB engine to
// support the indexes, other engines aren't affected.
$table->engine = 'InnoDB';
$table->index('user_id');
});
}
}
/**
* seed the database with initial value
*/
public function seed(){
try{
Sentry::createUser(array(
'email' => 'admin@admin.com',
'password' => 'password',
'first_name' => 'Website',
'last_name' => 'Administrator',
'activated' => 1,
'permissions' => array(
'admin' => 1
)
));
}catch(Exception $e){
echo $e->getMessage()."\n";
}
}
}
$migrator = new Migrator($config['database']['connections'][$config['database']['default']]);
$migrator->migrate();
$migrator->seed();
?>