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

     1  package migrations
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/jackc/pgx/v5"
     7  )
     8  
     9  const (
    10  	addTransactionXIDColumns = `
    11  		ALTER TABLE relation_tuple_transaction
    12  			ALTER COLUMN id SET DEFAULT (pg_current_xact_id()::text::bigint),
    13  			ADD COLUMN xid xid8 NOT NULL DEFAULT (pg_current_xact_id()),
    14  			ADD COLUMN snapshot pg_snapshot`
    15  
    16  	addTupleXIDColumns = `
    17  		ALTER TABLE relation_tuple
    18  			ADD COLUMN created_xid xid8,
    19  			ADD COLUMN deleted_xid xid8 NOT NULL DEFAULT ('9223372036854775807');`
    20  
    21  	addNamespaceXIDColumns = `
    22  		ALTER TABLE namespace_config
    23  			ADD COLUMN created_xid xid8,
    24  			ADD COLUMN deleted_xid xid8 NOT NULL DEFAULT ('9223372036854775807');`
    25  
    26  	addCaveatXIDColumns = `
    27  		ALTER TABLE caveat
    28  			ADD COLUMN created_xid xid8,
    29  			ADD COLUMN deleted_xid xid8 NOT NULL DEFAULT ('9223372036854775807');`
    30  
    31  	addTransactionDefault = `
    32  		ALTER TABLE relation_tuple_transaction
    33  			ALTER COLUMN snapshot SET DEFAULT (pg_current_snapshot());`
    34  )
    35  
    36  func init() {
    37  	if err := DatabaseMigrations.Register("add-xid-columns", "add-caveats",
    38  		noNonatomicMigration,
    39  		func(ctx context.Context, tx pgx.Tx) error {
    40  			for _, stmt := range []string{
    41  				addTransactionXIDColumns,
    42  				addTupleXIDColumns,
    43  				addNamespaceXIDColumns,
    44  				addCaveatXIDColumns,
    45  				addTransactionDefault,
    46  			} {
    47  				if _, err := tx.Exec(ctx, stmt); err != nil {
    48  					return err
    49  				}
    50  			}
    51  
    52  			return nil
    53  		}); err != nil {
    54  		panic("failed to register migration: " + err.Error())
    55  	}
    56  }