From 0427658dbfcf45fc941bce5fb875022621af2e31 Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Sat, 9 Jul 2016 06:44:49 -0700 Subject: [PATCH] #86: Fixed more queries for PostgreSQL. --- app/Commands/AddTrackToPlaylistCommand.php | 2 +- app/Models/Playlist.php | 16 ++- app/Models/ShowSong.php | 2 +- composer.json | 4 +- ...6_06_26_225535_create_activities_table.php | 4 +- ..._26_225535_create_failed_jobs_table_2.php} | 7 +- ...16_06_26_225535_create_images_table_2.php} | 9 +- ...016_06_26_225535_create_licenses_table.php | 88 +++++++++---- ...2016_06_26_225535_create_news_table_2.php} | 7 +- ...06_26_225535_create_revisions_table_2.php} | 8 +- .../2016_06_26_225535_create_roles_table.php | 49 ++++---- ..._26_225535_create_track_files_table_2.php} | 7 +- ..._06_26_225535_create_track_types_table.php | 36 ++++++ ...16_06_26_225535_create_tracks_table_2.php} | 7 +- ...016_06_26_225535_create_users_table_2.php} | 7 +- .../2016_06_27_014855_insert_static_data.php | 116 ------------------ database/seeds/ShowSongTableSeeder.php | 85 +++++++------ 17 files changed, 234 insertions(+), 220 deletions(-) rename database/migrations/{2016_06_26_225535_create_failed_jobs_table.php => 2016_06_26_225535_create_failed_jobs_table_2.php} (78%) rename database/migrations/{2016_06_26_225535_create_images_table.php => 2016_06_26_225535_create_images_table_2.php} (75%) rename database/migrations/{2016_06_26_225535_create_news_table.php => 2016_06_26_225535_create_news_table_2.php} (79%) rename database/migrations/{2016_06_26_225535_create_revisions_table.php => 2016_06_26_225535_create_revisions_table_2.php} (83%) rename database/migrations/{2016_06_26_225535_create_track_files_table.php => 2016_06_26_225535_create_track_files_table_2.php} (84%) rename database/migrations/{2016_06_26_225535_create_tracks_table.php => 2016_06_26_225535_create_tracks_table_2.php} (93%) rename database/migrations/{2016_06_26_225535_create_users_table.php => 2016_06_26_225535_create_users_table_2.php} (89%) delete mode 100644 database/migrations/2016_06_27_014855_insert_static_data.php diff --git a/app/Commands/AddTrackToPlaylistCommand.php b/app/Commands/AddTrackToPlaylistCommand.php index d2cead99..df556e8f 100644 --- a/app/Commands/AddTrackToPlaylistCommand.php +++ b/app/Commands/AddTrackToPlaylistCommand.php @@ -67,7 +67,7 @@ class AddTrackToPlaylistCommand extends CommandBase } - $songIndex = $this->_playlist->tracks()->count() + 1; + $songIndex = $this->_playlist->trackCount() + 1; $this->_playlist->tracks()->attach($this->_track, ['position' => $songIndex]); $this->_playlist->touch(); diff --git a/app/Models/Playlist.php b/app/Models/Playlist.php index f5d71342..9f2018a4 100644 --- a/app/Models/Playlist.php +++ b/app/Models/Playlist.php @@ -198,13 +198,23 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable ]; } - public function tracks() + public function tracks(bool $ordered = true) { - return $this + $query = $this ->belongsToMany(Track::class) ->withPivot('position') ->withTimestamps(); - //->orderBy('position', 'asc'); + + if ($ordered) { + $query = $query->orderBy('position', 'asc'); + } + + return $query; + } + + public function trackCount():int + { + return $this->tracks(false)->count(); } public function trackFiles() diff --git a/app/Models/ShowSong.php b/app/Models/ShowSong.php index 34667ea1..52c885a1 100644 --- a/app/Models/ShowSong.php +++ b/app/Models/ShowSong.php @@ -39,7 +39,7 @@ class ShowSong extends Model public function trackCountRelation() { return $this->belongsToMany(Track::class) ->select(['show_song_id', DB::raw('count(*) as track_count')]) - ->groupBy('show_song_id'); + ->groupBy('show_song_id', 'track_id'); } public function tracks(){ diff --git a/composer.json b/composer.json index e1a15e3c..dc7b86fa 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,9 @@ }, "autoload": { "classmap": [ - "database", + "database/factories", + "database/migrations", + "database/seeds", "app/Library" ], "psr-4": { diff --git a/database/migrations/2016_06_26_225535_create_activities_table.php b/database/migrations/2016_06_26_225535_create_activities_table.php index 9859557b..3fb6943c 100644 --- a/database/migrations/2016_06_26_225535_create_activities_table.php +++ b/database/migrations/2016_06_26_225535_create_activities_table.php @@ -17,8 +17,8 @@ class CreateActivitiesTable extends Migration { $table->bigInteger('id', true)->unsigned(); $table->dateTime('created_at')->index(); $table->integer('user_id')->unsigned(); - $table->boolean('activity_type'); - $table->boolean('resource_type'); + $table->unsignedTinyInteger('activity_type'); + $table->unsignedTinyInteger('resource_type'); $table->integer('resource_id')->unsigned(); }); } diff --git a/database/migrations/2016_06_26_225535_create_failed_jobs_table.php b/database/migrations/2016_06_26_225535_create_failed_jobs_table_2.php similarity index 78% rename from database/migrations/2016_06_26_225535_create_failed_jobs_table.php rename to database/migrations/2016_06_26_225535_create_failed_jobs_table_2.php index b5d13467..cf73737d 100644 --- a/database/migrations/2016_06_26_225535_create_failed_jobs_table.php +++ b/database/migrations/2016_06_26_225535_create_failed_jobs_table_2.php @@ -3,7 +3,12 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateFailedJobsTable extends Migration { +/** + * Class CreateFailedJobsTable2 + * + * This is the PostgreSQL version of CreateFailedJobsTable. + */ +class CreateFailedJobsTable2 extends Migration { /** * Run the migrations. diff --git a/database/migrations/2016_06_26_225535_create_images_table.php b/database/migrations/2016_06_26_225535_create_images_table_2.php similarity index 75% rename from database/migrations/2016_06_26_225535_create_images_table.php rename to database/migrations/2016_06_26_225535_create_images_table_2.php index 416b254a..0f19ccb0 100644 --- a/database/migrations/2016_06_26_225535_create_images_table.php +++ b/database/migrations/2016_06_26_225535_create_images_table_2.php @@ -3,7 +3,12 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateImagesTable extends Migration { +/** + * Class CreateImagesTable2 + * + * This is the PostgreSQL version of CreateImagesTable. + */ +class CreateImagesTable2 extends Migration { /** * Run the migrations. @@ -20,7 +25,7 @@ class CreateImagesTable extends Migration { $table->string('extension', 32); $table->integer('size'); $table->string('hash', 32)->index(); - $table->integer('uploaded_by')->unsigned()->index('images_uploaded_by_foreign'); + $table->unsignedInteger('uploaded_by')->index(); $table->timestamps(); }); } diff --git a/database/migrations/2016_06_26_225535_create_licenses_table.php b/database/migrations/2016_06_26_225535_create_licenses_table.php index 0ce99166..f929562b 100644 --- a/database/migrations/2016_06_26_225535_create_licenses_table.php +++ b/database/migrations/2016_06_26_225535_create_licenses_table.php @@ -5,33 +5,69 @@ use Illuminate\Database\Schema\Blueprint; class CreateLicensesTable extends Migration { - /** - * Run the migrations. - * - * @return void - */ - public function up() - { - Schema::create('licenses', function(Blueprint $table) - { - $table->increments('id'); - $table->string('title', 100); - $table->text('description', 65535); - $table->boolean('affiliate_distribution'); - $table->boolean('open_distribution'); - $table->boolean('remix'); - }); - } + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('licenses', function(Blueprint $table) + { + $table->increments('id'); + $table->string('title', 100); + $table->text('description', 65535); + $table->boolean('affiliate_distribution'); + $table->boolean('open_distribution'); + $table->boolean('remix'); + }); + + DB::table('licenses')->insert([ + 'id' => 1, + 'title' => 'Personal', + 'description' => 'Only you and Pony.fm are allowed to distribute and broadcast the track.', + 'affiliate_distribution' => 0, + 'open_distribution' => 0, + 'remix' => 0 + ]); + + DB::table('licenses')->insert([ + 'id' => 2, + 'title' => 'Broadcast', + 'description' => 'You, Pony.fm, and its affiliates may distribute and broadcast the track.', + 'affiliate_distribution' => 1, + 'open_distribution' => 0, + 'remix' => 0 + ]); + + DB::table('licenses')->insert([ + 'id' => 3, + 'title' => 'Open', + 'description' => 'Anyone is permitted to broadcast and distribute the song in its original form, with attribution to you.', + 'affiliate_distribution' => 1, + 'open_distribution' => 1, + 'remix' => 0 + ]); + + DB::table('licenses')->insert([ + 'id' => 4, + 'title' => 'Remix', + 'description' => 'Anyone is permitted to broadcast and distribute the song in any form, or create derivative works based on it for any purpose, with attribution to you.', + 'affiliate_distribution' => 1, + 'open_distribution' => 1, + 'remix' => 1 + ]); + } - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::drop('licenses'); - } + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('licenses'); + } } diff --git a/database/migrations/2016_06_26_225535_create_news_table.php b/database/migrations/2016_06_26_225535_create_news_table_2.php similarity index 79% rename from database/migrations/2016_06_26_225535_create_news_table.php rename to database/migrations/2016_06_26_225535_create_news_table_2.php index 1e928121..c0e5c874 100644 --- a/database/migrations/2016_06_26_225535_create_news_table.php +++ b/database/migrations/2016_06_26_225535_create_news_table_2.php @@ -3,7 +3,12 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateNewsTable extends Migration { +/** + * Class CreateNewsTable2 + * + * This is the PostgreSQL version of CreateNewsTable. + */ +class CreateNewsTable2 extends Migration { /** * Run the migrations. diff --git a/database/migrations/2016_06_26_225535_create_revisions_table.php b/database/migrations/2016_06_26_225535_create_revisions_table_2.php similarity index 83% rename from database/migrations/2016_06_26_225535_create_revisions_table.php rename to database/migrations/2016_06_26_225535_create_revisions_table_2.php index cf27cfb9..cf4d7249 100644 --- a/database/migrations/2016_06_26_225535_create_revisions_table.php +++ b/database/migrations/2016_06_26_225535_create_revisions_table_2.php @@ -3,7 +3,13 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateRevisionsTable extends Migration { + +/** + * Class CreateRevisionsTable2 + * + * This is the PostgreSQL version of CreateRevisionsTable. + */ +class CreateRevisionsTable2 extends Migration { /** * Run the migrations. diff --git a/database/migrations/2016_06_26_225535_create_roles_table.php b/database/migrations/2016_06_26_225535_create_roles_table.php index 33a3c7c4..38fa2000 100644 --- a/database/migrations/2016_06_26_225535_create_roles_table.php +++ b/database/migrations/2016_06_26_225535_create_roles_table.php @@ -5,29 +5,32 @@ use Illuminate\Database\Schema\Blueprint; class CreateRolesTable extends Migration { - /** - * Run the migrations. - * - * @return void - */ - public function up() - { - Schema::create('roles', function(Blueprint $table) - { - $table->increments('id'); - $table->string('name'); - }); - } + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('roles', function(Blueprint $table) + { + $table->increments('id'); + $table->string('name'); + }); + DB::table('roles')->insert(['name' => 'super_admin']); + DB::table('roles')->insert(['name' => 'admin']); + DB::table('roles')->insert(['name' => 'moderator']); + DB::table('roles')->insert(['name' => 'user']); + } - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::drop('roles'); - } - + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('roles'); + } } diff --git a/database/migrations/2016_06_26_225535_create_track_files_table.php b/database/migrations/2016_06_26_225535_create_track_files_table_2.php similarity index 84% rename from database/migrations/2016_06_26_225535_create_track_files_table.php rename to database/migrations/2016_06_26_225535_create_track_files_table_2.php index 7a01b86d..4556108d 100644 --- a/database/migrations/2016_06_26_225535_create_track_files_table.php +++ b/database/migrations/2016_06_26_225535_create_track_files_table_2.php @@ -3,7 +3,12 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateTrackFilesTable extends Migration { +/** + * Class CreateTrackFilesTable2 + * + * This is the PostgreSQL version of CreateTrackFilesTable. + */ +class CreateTrackFilesTable2 extends Migration { /** * Run the migrations. diff --git a/database/migrations/2016_06_26_225535_create_track_types_table.php b/database/migrations/2016_06_26_225535_create_track_types_table.php index 1e4e261c..28adcb0e 100644 --- a/database/migrations/2016_06_26_225535_create_track_types_table.php +++ b/database/migrations/2016_06_26_225535_create_track_types_table.php @@ -18,6 +18,42 @@ class CreateTrackTypesTable extends Migration { $table->string('title'); $table->string('editor_title'); }); + + DB::table('track_types')->insert([ + 'id' => 1, + 'title' => 'Original Song', + 'editor_title' => 'an original song' + ]); + + DB::table('track_types')->insert([ + 'id' => 2, + 'title' => 'Official Song Remix', + 'editor_title' => 'a remix of an official song' + ]); + + DB::table('track_types')->insert([ + 'id' => 3, + 'title' => 'Fan Song Remix', + 'editor_title' => 'a remix of a fan song' + ]); + + DB::table('track_types')->insert([ + 'id' => 4, + 'title' => 'Ponified Song', + 'editor_title' => 'a non-pony song, turned pony' + ]); + + DB::table('track_types')->insert([ + 'id' => 5, + 'title' => 'Official Show Audio Remix', + 'editor_title' => 'a remix of official show audio' + ]); + + DB::table('track_types')->insert([ + 'id' => 6, + 'title' => 'Unclassified', + 'editor_title' => 'an unclassified track' + ]); } diff --git a/database/migrations/2016_06_26_225535_create_tracks_table.php b/database/migrations/2016_06_26_225535_create_tracks_table_2.php similarity index 93% rename from database/migrations/2016_06_26_225535_create_tracks_table.php rename to database/migrations/2016_06_26_225535_create_tracks_table_2.php index 84691752..3872884f 100644 --- a/database/migrations/2016_06_26_225535_create_tracks_table.php +++ b/database/migrations/2016_06_26_225535_create_tracks_table_2.php @@ -3,7 +3,12 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateTracksTable extends Migration { +/** + * Class CreateTracksTable2 + * + * This is the PostgreSQL version of CreateTracksTable. + */ +class CreateTracksTable2 extends Migration { /** * Run the migrations. diff --git a/database/migrations/2016_06_26_225535_create_users_table.php b/database/migrations/2016_06_26_225535_create_users_table_2.php similarity index 89% rename from database/migrations/2016_06_26_225535_create_users_table.php rename to database/migrations/2016_06_26_225535_create_users_table_2.php index 876371b0..6508e040 100644 --- a/database/migrations/2016_06_26_225535_create_users_table.php +++ b/database/migrations/2016_06_26_225535_create_users_table_2.php @@ -3,7 +3,12 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateUsersTable extends Migration { +/** + * Class CreateUsersTable2 + * + * This is the PostgreSQL version of CreateUsersTable. + */ +class CreateUsersTable2 extends Migration { /** * Run the migrations. diff --git a/database/migrations/2016_06_27_014855_insert_static_data.php b/database/migrations/2016_06_27_014855_insert_static_data.php deleted file mode 100644 index fc460026..00000000 --- a/database/migrations/2016_06_27_014855_insert_static_data.php +++ /dev/null @@ -1,116 +0,0 @@ -. - */ - -use Illuminate\Database\Schema\Blueprint; -use Illuminate\Database\Migrations\Migration; - -class InsertStaticData extends Migration -{ - /** - * Run the migrations. - * - * @return void - */ - public function up() - { - DB::table('licenses')->insert([ - 'id' => 1, - 'title' => 'Personal', - 'description' => 'Only you and Pony.fm are allowed to distribute and broadcast the track.', - 'affiliate_distribution' => 0, - 'open_distribution' => 0, - 'remix' => 0 - ]); - - DB::table('licenses')->insert([ - 'id' => 2, - 'title' => 'Broadcast', - 'description' => 'You, Pony.fm, and its affiliates may distribute and broadcast the track.', - 'affiliate_distribution' => 1, - 'open_distribution' => 0, - 'remix' => 0 - ]); - - DB::table('licenses')->insert([ - 'id' => 3, - 'title' => 'Open', - 'description' => 'Anyone is permitted to broadcast and distribute the song in its original form, with attribution to you.', - 'affiliate_distribution' => 1, - 'open_distribution' => 1, - 'remix' => 0 - ]); - - DB::table('licenses')->insert([ - 'id' => 4, - 'title' => 'Remix', - 'description' => 'Anyone is permitted to broadcast and distribute the song in any form, or create derivative works based on it for any purpose, with attribution to you.', - 'affiliate_distribution' => 1, - 'open_distribution' => 1, - 'remix' => 1 - ]); - - DB::table('track_types')->insert([ - 'id' => 1, - 'title' => 'Original Song', - 'editor_title' => 'an original song' - ]); - - DB::table('track_types')->insert([ - 'id' => 2, - 'title' => 'Official Song Remix', - 'editor_title' => 'a remix of an official song' - ]); - - DB::table('track_types')->insert([ - 'id' => 3, - 'title' => 'Fan Song Remix', - 'editor_title' => 'a remix of a fan song' - ]); - - DB::table('track_types')->insert([ - 'id' => 4, - 'title' => 'Ponified Song', - 'editor_title' => 'a non-pony song, turned pony' - ]); - - DB::table('track_types')->insert([ - 'id' => 5, - 'title' => 'Official Show Audio Remix', - 'editor_title' => 'a remix of official show audio' - ]); - - DB::table('track_types')->insert([ - 'id' => 6, - 'title' => 'Unclassified', - 'editor_title' => 'an unclassified track' - ]); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - DB::table('licenses')->whereIn('id', [1, 2, 3, 4])->delete(); - DB::table('track_types')->whereIn('id', [1, 2, 3, 4, 5, 6])->delete(); - } -} diff --git a/database/seeds/ShowSongTableSeeder.php b/database/seeds/ShowSongTableSeeder.php index e6e4e77b..c6325a03 100644 --- a/database/seeds/ShowSongTableSeeder.php +++ b/database/seeds/ShowSongTableSeeder.php @@ -22,25 +22,12 @@ use Illuminate\Database\Seeder; class ShowSongTableSeeder extends Seeder { - /** - * Run the database seeds. - * - * @return void - */ - public function run() - { - // This table only needs to be filled once. - // - // Song lyrics used are (C) Hasbro and - // sourced from http://mlp.wikia.com/wiki/Songs - if (DB::table('show_songs')->count() === 0) { - DB::table('show_songs')->insert( - [ - [ - 'title' => 'My Little Pony Theme Song', - 'slug' => 'my-little-pony-theme-song', - 'lyrics' => -"[Backup singer] + private $showSongs = [ + [ + 'title' => 'My Little Pony Theme Song', + 'slug' => 'my-little-pony-theme-song', + 'lyrics' => + "[Backup singer] My Little Pony, My Little Pony Ahh, ahh, ahh, ahhh... @@ -71,12 +58,12 @@ And magic makes it all complete You have my little ponies Do you know you're all my very best friends?" - ], - [ - 'title' => 'Laughter Song (Giggle at the Ghostly)', - 'slug' => 'laughter-song', - 'lyrics' => -"[Pinkie Pie] + ], + [ + 'title' => 'Laughter Song (Giggle at the Ghostly)', + 'slug' => 'laughter-song', + 'lyrics' => + "[Pinkie Pie] When I was a little filly and the sun was going down... Twilight Sparkle: Tell me she's not... @@ -116,12 +103,12 @@ And tell that big dumb scary face to take a hike and leave you alone and if he t Laaaaaaauuugh!" - ], - [ - 'title' => 'Winter Wrap-Up', - 'slug' => 'winter-wrap-up', - 'lyrics' => -"[Rainbow Dash] + ], + [ + 'title' => 'Winter Wrap-Up', + 'slug' => 'winter-wrap-up', + 'lyrics' => + "[Rainbow Dash] Three months of winter coolness And awesome holidays @@ -256,12 +243,12 @@ Winter Wrap Up! Winter Wrap Up! 'Cause tomorrow spring is here 'Cause tomorrow spring is here 'Cause tomorrow spring is here!" - ], - [ - 'title' => 'EQG - Helping Twilight Win The Crown', - 'slug' => 'helping-twilight-win-the-crown', - 'lyrics' => -"[Pinkie Pie, Rainbow Dash, Applejack, Fluttershy, Rarity] + ], + [ + 'title' => 'EQG - Helping Twilight Win The Crown', + 'slug' => 'helping-twilight-win-the-crown', + 'lyrics' => + "[Pinkie Pie, Rainbow Dash, Applejack, Fluttershy, Rarity] Hey! Hey! Everybody! We've got something to say. We may seem as different, @@ -354,8 +341,28 @@ Jump up, make a sound. Hey! Stomp your hooves, turn around. Canterlot Wondercolts Help her win the crown..." - ] - ]); + ] + ]; + + /** + * Run the database seeds. + * + * @return void + */ + public function run() + { + // This table only needs to be filled once. + // + // Song lyrics used are (C) Hasbro and + // sourced from http://mlp.wikia.com/wiki/Songs + if (DB::table('show_songs')->count() === 0) { + $now = \Carbon\Carbon::now(); + $showSongs = array_map(function(array $item) use ($now) { + $item['created_at'] = $now; + $item['updated_at'] = $now; + return $item; + }, $this->showSongs); + DB::table('show_songs')->insert($showSongs); } } }