github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/mysql/migrations/zz_migration.0004_add_caveat.go (about)

     1  package migrations
     2  
     3  import "fmt"
     4  
     5  // primary key size limit in InnoDB is 3KB.
     6  // 3072 / 4 bytes per character = 768 - 2 bigints (4 bytes) = 764 bytes.
     7  // we choose 700 to leave headroom for potential future new columns in the PK
     8  func createCaveatTable(t *tables) string {
     9  	return fmt.Sprintf(`CREATE TABLE %s (
    10  		name VARCHAR(700) NOT NULL,
    11  		definition BLOB NOT NULL,
    12  		created_transaction BIGINT NOT NULL,
    13  		deleted_transaction BIGINT NOT NULL DEFAULT '9223372036854775807',
    14  		CONSTRAINT pk_caveat PRIMARY KEY (name, deleted_transaction),
    15  		CONSTRAINT uq_caveat UNIQUE (name, created_transaction, deleted_transaction)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
    16  		t.Caveat(),
    17  	)
    18  }
    19  
    20  // given column size subtracts from the max allowed row size, we define a conservative value of 128 bytes
    21  // for caveat name. This gives larger headroom for JSON column size.
    22  // See https://dev.mysql.com/doc/refman/5.7/en/char.html
    23  func addCaveatToRelationTuplesTable(t *tables) string {
    24  	return fmt.Sprintf(`ALTER TABLE %s
    25  			ADD COLUMN caveat_name VARCHAR(700),
    26  			ADD COLUMN caveat_context JSON;`,
    27  		t.RelationTuple(),
    28  	)
    29  }
    30  
    31  func init() {
    32  	mustRegisterMigration("add_caveat", "add_ns_config_id", noNonatomicMigration,
    33  		newStatementBatch(
    34  			createCaveatTable,
    35  			addCaveatToRelationTuplesTable,
    36  		).execute,
    37  	)
    38  }