reworked migrations AGAIN and added relations to models

pull/26/head
Ondřej Hruška 7 years ago
parent 34e4ed5d9b
commit 52ab2c474e
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 20
      app/Models/Changeset.php
  2. 14
      app/Models/DataRow.php
  3. 42
      app/Models/DataTable.php
  4. 10
      app/Models/DiscussionFollow.php
  5. 10
      app/Models/TableComment.php
  6. 10
      app/Models/TableFavourite.php
  7. 10
      app/Models/TableRevision.php
  8. 19
      app/Models/User.php
  9. 10
      app/Models/UserFollow.php
  10. 43
      database/migrations/2018_07_08_193638_create_data_tables_table.php
  11. 44
      database/migrations/2018_07_08_193639_create_changesets_table.php
  12. 35
      database/migrations/2018_07_08_193640_create_data_rows_table.php
  13. 4
      database/migrations/2018_07_08_204449_create_oauth_identities_table.php

@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Changeset extends Model
{
/** Parent data table */
public function dataTable()
{
return $this->belongsTo(DataTable::class);
}
/** Author who created it user */
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
}

@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DataRow extends Model
{
/** Parent data table */
public function dataTable()
{
return $this->belongsTo(DataTable::class);
}
}

@ -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);
}
}

@ -1,10 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class DiscussionFollow extends Pivot
{
//
}

@ -1,10 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TableComment extends Model
{
//
}

@ -1,10 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class TableFavourite extends Pivot
{
//
}

@ -1,10 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TableRevision extends Model
{
//
}

@ -2,6 +2,7 @@
namespace App\Models;
use AdamWathan\EloquentOAuth\OAuthIdentity;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
@ -26,4 +27,22 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];
/** Owned tables */
public function dataTables()
{
return $this->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);
}
}

@ -1,10 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserFollow extends Pivot
{
//
}

@ -17,49 +17,19 @@ class CreateDataTablesTable extends Migration
$table->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');
}
}

@ -0,0 +1,44 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateChangesetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('changesets', function (Blueprint $table) {
$table->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');
}
}

@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDataRowsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('data_rows', function (Blueprint $table) {
$table->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');
}
}

@ -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');
});
}

Loading…
Cancel
Save