From 52ab2c474e637cea39ce7d3e7913ab4f531ae313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 12 Jul 2018 20:50:48 +0200 Subject: [PATCH] reworked migrations AGAIN and added relations to models --- app/Models/Changeset.php | 20 +++++++++ app/Models/DataRow.php | 14 ++++++ app/Models/DataTable.php | 42 +++++++++++++++++- app/Models/DiscussionFollow.php | 10 ----- app/Models/TableComment.php | 10 ----- app/Models/TableFavourite.php | 10 ----- app/Models/TableRevision.php | 10 ----- app/Models/User.php | 19 ++++++++ app/Models/UserFollow.php | 10 ----- ..._07_08_193638_create_data_tables_table.php | 43 +++--------------- ...8_07_08_193639_create_changesets_table.php | 44 +++++++++++++++++++ ...18_07_08_193640_create_data_rows_table.php | 35 +++++++++++++++ ...8_204449_create_oauth_identities_table.php | 4 +- 13 files changed, 180 insertions(+), 91 deletions(-) create mode 100644 app/Models/Changeset.php create mode 100644 app/Models/DataRow.php delete mode 100644 app/Models/DiscussionFollow.php delete mode 100644 app/Models/TableComment.php delete mode 100644 app/Models/TableFavourite.php delete mode 100644 app/Models/TableRevision.php delete mode 100644 app/Models/UserFollow.php create mode 100644 database/migrations/2018_07_08_193639_create_changesets_table.php create mode 100644 database/migrations/2018_07_08_193640_create_data_rows_table.php diff --git a/app/Models/Changeset.php b/app/Models/Changeset.php new file mode 100644 index 0000000..26d9335 --- /dev/null +++ b/app/Models/Changeset.php @@ -0,0 +1,20 @@ +belongsTo(DataTable::class); + } + + /** Author who created it user */ + public function author() + { + return $this->belongsTo(User::class, 'author_id'); + } +} diff --git a/app/Models/DataRow.php b/app/Models/DataRow.php new file mode 100644 index 0000000..8b47458 --- /dev/null +++ b/app/Models/DataRow.php @@ -0,0 +1,14 @@ +belongsTo(DataTable::class); + } +} diff --git a/app/Models/DataTable.php b/app/Models/DataTable.php index ba4c916..34acb71 100644 --- a/app/Models/DataTable.php +++ b/app/Models/DataTable.php @@ -6,5 +6,45 @@ use Illuminate\Database\Eloquent\Model; class DataTable extends Model { - // + /** Owning user */ + public function owner() + { + return $this->belongsTo(User::class, 'owner_id'); + } + + /** Fork precursor */ + public function precursor() + { + return $this->belongsTo(DataTable::class, 'precursor_id'); + } + + /** Fork descendants */ + public function descendants() + { + return $this->hasMany(DataTable::class, 'precursor_id'); + } + + /** Changesets */ + public function changesets() + { + return $this->hasMany(Changeset::class); + } + + /** Data rows */ + public function dataRows() + { + return $this->hasMany(DataRow::class); + } + + /** Changesets already merged */ + public function appliedChangesets() + { + return $this->hasMany(Changeset::class)->where('applied', true); + } + + /** Changesets already merged */ + public function pendingChangesets() + { + return $this->hasMany(Changeset::class)->where('applied', false); + } } diff --git a/app/Models/DiscussionFollow.php b/app/Models/DiscussionFollow.php deleted file mode 100644 index 9fb2d28..0000000 --- a/app/Models/DiscussionFollow.php +++ /dev/null @@ -1,10 +0,0 @@ -hasMany(DataTable::class); + } + + /** Assigned OAuth identities */ + public function socialIdentities() + { + return $this->hasMany(OAuthIdentity::class); + } + + /** Assigned OAuth identities */ + public function changesets() + { + return $this->hasMany(Changeset::class); + } } diff --git a/app/Models/UserFollow.php b/app/Models/UserFollow.php deleted file mode 100644 index 72b0b33..0000000 --- a/app/Models/UserFollow.php +++ /dev/null @@ -1,10 +0,0 @@ -increments('id'); $table->timestamps(); $table->unsignedInteger('owner_id'); - $table->unsignedInteger('forked_from_id')->nullable(); + $table->unsignedInteger('precursor_id')->nullable(); // fork source $table->unsignedInteger('revision'); // incremented with each applied changeset - $table->string('title'); // indexable - $table->string('keywords'); // indexable + $table->string('title')->index(); // indexable $table->text('description'); - $table->string('license'); // license name (indexable) - $table->mediumText('license_text'); // space for custom license text, if needed + $table->text('license'); + $table->text('source_link'); $table->longText('content'); - $table->foreign('owner_id') - ->references('id')->on('users') + $table->foreign('owner_id')->references('id')->on('users') ->onDelete('cascade'); - $table->foreign('forked_from_id') - ->references('id')->on('data_tables') + $table->foreign('precursor_id')->references('id')->on('data_tables') ->onDelete('set null'); - - $table->index('title'); // for text search - $table->index('keywords'); // for text search - $table->index('owner_id'); - }); - - Schema::create('changesets', function (Blueprint $table) { - $table->increments('id'); - $table->timestamps(); - $table->unsignedInteger('data_table_id'); - $table->unsignedInteger('target_revision'); - $table->boolean('applied'); - $table->unsignedInteger('author_id')->nullable(); - $table->longText('content'); // incremental changeset including original row values so that it can be reversed - - $table->foreign('data_table_id') - ->references('id')->on('data_tables') - ->onDelete('cascade'); - - // Deleting a user should not delete their applied patches, as that would break history. - // We set the author to null and unmerged patches may be cleaned manually or by a separate query. - $table->foreign('author_id') - ->references('id')->on('users') - ->onDelete('set null'); - - $table->index('author_id'); - $table->index('data_table_id'); }); } @@ -70,7 +40,6 @@ class CreateDataTablesTable extends Migration */ public function down() { - Schema::dropIfExists('changesets'); Schema::dropIfExists('data_tables'); } } diff --git a/database/migrations/2018_07_08_193639_create_changesets_table.php b/database/migrations/2018_07_08_193639_create_changesets_table.php new file mode 100644 index 0000000..9219fc7 --- /dev/null +++ b/database/migrations/2018_07_08_193639_create_changesets_table.php @@ -0,0 +1,44 @@ +increments('id'); + $table->timestamp('created_at'); + $table->unsignedInteger('data_table_id')->index(); + $table->unsignedInteger('target_revision'); + $table->boolean('applied'); + $table->unsignedInteger('author_id')->index()->nullable(); + $table->longText('content'); // incremental changeset including original row values so that it can be reversed + + $table->foreign('data_table_id')->references('id')->on('data_tables') + ->onDelete('cascade'); + + // Deleting a user should not delete their applied patches, as that would break history. + // We set the author to null and unmerged patches may be cleaned manually or by a separate query. + $table->foreign('author_id')->references('id')->on('users') + ->onDelete('set null'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('changesets'); + } +} diff --git a/database/migrations/2018_07_08_193640_create_data_rows_table.php b/database/migrations/2018_07_08_193640_create_data_rows_table.php new file mode 100644 index 0000000..fcf7747 --- /dev/null +++ b/database/migrations/2018_07_08_193640_create_data_rows_table.php @@ -0,0 +1,35 @@ +increments('id'); + $table->unsignedInteger('data_table_id')->index(); + $table->longtext('data'); + + $table->foreign('data_table_id')->references('id')->on('data_tables') + ->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('data_rows'); + } +} diff --git a/database/migrations/2018_07_08_204449_create_oauth_identities_table.php b/database/migrations/2018_07_08_204449_create_oauth_identities_table.php index 8a6f7b2..d84dd69 100644 --- a/database/migrations/2018_07_08_204449_create_oauth_identities_table.php +++ b/database/migrations/2018_07_08_204449_create_oauth_identities_table.php @@ -16,7 +16,7 @@ class CreateOauthIdentitiesTable extends Migration Schema::create('oauth_identities', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); - $table->unsignedInteger('user_id'); + $table->unsignedInteger('user_id')->index(); $table->string('provider_user_id'); $table->string('provider'); $table->string('access_token'); @@ -24,8 +24,6 @@ class CreateOauthIdentitiesTable extends Migration $table->foreign('user_id') ->references('id')->on('users') ->onDelete('cascade'); - - $table->index('user_id'); }); }