Add seeders.

This commit is contained in:
Toby Zerner 2014-12-20 17:27:41 +10:30
parent 5a98eb0f06
commit dc73e36312
4 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,15 @@
<?php
class ConfigTableSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}

View File

@ -0,0 +1,38 @@
<?php namespace Flarum\Core\Support\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
$tables = [
'config',
'discussions',
'groups',
'permissions',
'posts',
'sessions',
'users',
'users_discussions',
'users_groups'
];
foreach ($tables as $table)
{
DB::table($table)->truncate();
}
$this->call('Flarum\Core\Support\Seeders\ConfigTableSeeder');
$this->call('Flarum\Core\Support\Seeders\UserTableSeeder');
$this->call('Flarum\Core\Support\Seeders\DiscussionTableSeeder');
}
}

View File

@ -0,0 +1,134 @@
<?php namespace Flarum\Core\Support\Seeders;
use Illuminate\Database\Seeder;
use DB;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
use Flarum\Core\Discussions\DiscussionState;
class DiscussionTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker\Factory::create();
$users = User::count();
for ($i = 0; $i < 100; $i++) {
$posts_count = $i == 1 ? 400 : rand(1, rand(1, rand(1, 100)));
$discussion = Discussion::create([
'title' => str_replace("'", '', rtrim($faker->realText(rand(20, 80)), '.')),
'start_time' => $faker->dateTimeThisYear,
'start_user_id' => rand(1, $users)
]);
$discussion->posts_count = $posts_count;
$post = Post::create([
'discussion_id' => $discussion->id,
'number' => 1,
'time' => $discussion->start_time,
'user_id' => $discussion->start_user_id,
'type' => 'comment',
'content' => $faker->realText(rand(100, 1000))
]);
$discussion->start_post_id = $post->id;
$discussion->last_time = $post->time;
$discussion->last_user_id = $post->user_id;
$discussion->last_post_id = $post->id;
$discussion->last_post_number = $post->number;
$discussion->number_index = $post->number;
$lastPost = null;
$count = $posts_count;
$posts = [];
$startTime = $discussion->start_time;
$numberOffset = 0;
for ($j = 0; $j < $count - 1; $j++) {
if (rand(1, 100) == 1) {
$discussion->posts_count--;
$post = Post::create([
'discussion_id' => $discussion->id,
'time' => $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')),
'user_id' => rand(1, $users),
'type' => 'title',
'content' => $discussion->title
]);
} else {
$edited = rand(1, 20) == 1;
$deleted = rand(1, 100) == 1;
if ($deleted) {
$discussion->posts_count--;
}
$post = Post::create([
'discussion_id' => $discussion->id,
'number' => $j + 2 + $numberOffset,
'time' => $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')),
'user_id' => rand(1, $users),
'type' => 'comment',
'content' => $faker->realText(rand(50, 500)),
'edit_time' => $edited ? $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')) : null,
'edit_user_id' => $edited ? rand(1, $users) : null,
'delete_time' => $deleted ? $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')) : null,
'delete_user_id' => $deleted ? rand(1, $users) : null,
]);
$posts[] = $post;
}
if (! $lastPost or $post->time >= $lastPost->time) {
$lastPost = $post;
}
if (rand(1, 20) == 1) {
$numberOffset += rand(0, 3);
}
}
// Update the discussion's last post details.
if ($lastPost) {
$discussion->last_time = $lastPost->time;
$discussion->last_user_id = $lastPost->user_id;
$discussion->last_post_id = $lastPost->id;
$discussion->last_post_number = $lastPost->number;
$discussion->number_index = $lastPost->number;
}
$discussion->save();
// Give some users some random discussion state data.
for ($j = rand(0, 100); $j < 100; $j++) {
try {
DiscussionState::create([
'discussion_id' => $discussion->id,
'user_id' => rand(1, $users),
'read_number' => rand(0, $posts_count - 1),
'read_time' => $faker->dateTimeBetween($discussion->start_time, 'now')
]);
} catch (Exception $e) {
}
}
}
// Update user post and discussion counts.
$prefix = DB::getTablePrefix();
DB::table('users')->update([
'discussions_count' => DB::raw('(SELECT COUNT(id) FROM '.$prefix.'discussions WHERE start_user_id = '.$prefix.'users.id)'),
'posts_count' => DB::raw('(SELECT COUNT(id) FROM '.$prefix.'posts WHERE user_id = '.$prefix.'users.id and type = "comment")'),
]);
}
}

View File

@ -0,0 +1,76 @@
<?php namespace Flarum\Core\Support\Seeders;
use Illuminate\Database\Seeder;
use DB;
use Flarum\Core\Users\User;
use Flarum\Core\Groups\Group;
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker\Factory::create();
$groups = ['Administrator', 'Guest', 'Member', 'Moderator', 'Staff'];
foreach ($groups as $group) {
Group::create(['name' => $group]);
}
for ($i = 0; $i < 100; $i++) {
$user = User::create([
'username' => $faker->userName,
'email' => $faker->safeEmail,
'password' => 'password',
'join_time' => $faker->dateTimeThisYear,
'time_zone' => $faker->timezone
]);
// Assign the users to the 'Member' group, and possibly some others.
$user->groups()->attach(3);
if (rand(1, 50) == 1) {
$user->groups()->attach(4);
}
if (rand(1, 20) == 1) {
$user->groups()->attach(5);
}
if (rand(1, 20) == 1) {
$user->groups()->attach(1);
}
}
// Set up the default permissions.
$permissions = [
// Guests can view the forum
['group.2' , 'forum' , 'view'],
// Members can create and reply to discussions + edit their own stuff
['group.3' , 'discussion' , 'create'],
['group.3' , 'discussion' , 'editOwn'],
['group.3' , 'discussion' , 'reply'],
['group.3' , 'post' , 'editOwn'],
// Moderators can edit + delete stuff and suspend users
['group.4' , 'discussion' , 'delete'],
['group.4' , 'discussion' , 'edit'],
['group.4' , 'post' , 'delete'],
['group.4' , 'post' , 'edit'],
['group.4' , 'user' , 'suspend'],
];
foreach ($permissions as &$permission) {
$permission = [
'grantee' => $permission[0],
'entity' => $permission[1],
'permission' => $permission[2]
];
}
DB::table('permissions')->insert($permissions);
}
}