implement GRIDs
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AddRowSequenceGenerator extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
// https://www.depesz.com/2008/03/20/getting-multiple-values-from-sequences/
|
||||
DB::unprepared(/** @lang PostgreSQL */
|
||||
<<<PGSQL
|
||||
CREATE OR REPLACE FUNCTION multi_nextval(use_seqname TEXT, use_increment INT4) RETURNS INT4 AS $$
|
||||
DECLARE
|
||||
reply int4;
|
||||
BEGIN
|
||||
perform pg_advisory_lock(20180801);
|
||||
execute 'ALTER SEQUENCE ' || quote_ident(use_seqname) || ' INCREMENT BY ' || use_increment::text;
|
||||
reply := nextval(use_seqname);
|
||||
execute 'ALTER SEQUENCE ' || quote_ident(use_seqname) || ' INCREMENT BY 1';
|
||||
perform pg_advisory_unlock(20180801);
|
||||
return reply;
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
PGSQL
|
||||
);
|
||||
DB::unprepared('CREATE SEQUENCE global_row_id_seq START 0 MINVALUE 0;');
|
||||
// We have to increment manually once before the above function can be used - that is because
|
||||
// nextval will return the initial value the first time it's called, so it would not advance by
|
||||
// the given step at all. This would give us negative values - not a problem in postgres without unsigned
|
||||
// types, but it loooks bad.
|
||||
DB::select("SELECT nextval('global_row_id_seq') as nv;");
|
||||
|
||||
// The multi_nextval() is concurrency-safe, except it can sometimes happen that
|
||||
// nextval in other connection also increments by the bigger step. The only downside is "wasted IDs".
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
DB::unprepared('DROP FUNCTION IF EXISTS multi_nextval(TEXT, INT4);');
|
||||
DB::unprepared('DROP SEQUENCE IF EXISTS global_row_id_seq;');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class MakeGridColumnMandatoryInRowsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
DB::unprepared("ALTER TABLE rows ADD CONSTRAINT grid_must_exist CHECK (data ? '_grid');");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
DB::unprepared("ALTER TABLE rows DROP CONSTRAINT grid_must_exist;");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user