github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/postgres/migrations/zz_migration.0001_1eaeba4b8a73_initial.go (about)

     1  package migrations
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/jackc/pgx/v5"
     7  )
     8  
     9  const createRelationTupleTransaction = `CREATE TABLE relation_tuple_transaction (
    10      id BIGSERIAL NOT NULL,
    11      timestamp TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
    12      CONSTRAINT pk_rttx PRIMARY KEY (id)
    13  );`
    14  
    15  const createNamespaceConfig = `CREATE TABLE namespace_config (
    16      namespace VARCHAR NOT NULL,
    17      serialized_config BYTEA NOT NULL,
    18      created_transaction BIGINT NOT NULL,
    19      deleted_transaction BIGINT NOT NULL DEFAULT '9223372036854775807',
    20      CONSTRAINT pk_namespace_config PRIMARY KEY (namespace, created_transaction)
    21  );`
    22  
    23  const createRelationTuple = `CREATE TABLE relation_tuple (
    24      id BIGSERIAL NOT NULL,
    25      namespace VARCHAR NOT NULL,
    26      object_id VARCHAR NOT NULL,
    27      relation VARCHAR NOT NULL,
    28      userset_namespace VARCHAR NOT NULL,
    29      userset_object_id VARCHAR NOT NULL,
    30      userset_relation VARCHAR NOT NULL,
    31      created_transaction BIGINT NOT NULL,
    32      deleted_transaction BIGINT NOT NULL DEFAULT '9223372036854775807',
    33      CONSTRAINT pk_relation_tuple PRIMARY KEY (id),
    34      CONSTRAINT uq_relation_tuple_namespace UNIQUE (namespace, object_id, relation, userset_namespace, userset_object_id, userset_relation, created_transaction, deleted_transaction),
    35      CONSTRAINT uq_relation_tuple_living UNIQUE (namespace, object_id, relation, userset_namespace, userset_object_id, userset_relation, deleted_transaction)
    36  );`
    37  
    38  const insertFirstTransaction = "INSERT INTO relation_tuple_transaction (timestamp) VALUES (to_timestamp(0));"
    39  
    40  const createAlembicVersion = `CREATE TABLE alembic_version (
    41  	version_num VARCHAR NOT NULL
    42  );`
    43  
    44  const insertEmptyVersion = `INSERT INTO alembic_version (version_num) VALUES ('');`
    45  
    46  func init() {
    47  	if err := DatabaseMigrations.Register("1eaeba4b8a73", "", noNonatomicMigration, func(ctx context.Context, tx pgx.Tx) error {
    48  		statements := []string{
    49  			createRelationTupleTransaction,
    50  			createNamespaceConfig,
    51  			createRelationTuple,
    52  			insertFirstTransaction,
    53  			createAlembicVersion,
    54  			insertEmptyVersion,
    55  		}
    56  		for _, stmt := range statements {
    57  			if _, err := tx.Exec(ctx, stmt); err != nil {
    58  				return err
    59  			}
    60  		}
    61  		return nil
    62  	}); err != nil {
    63  		panic("failed to register migration: " + err.Error())
    64  	}
    65  }