-
Notifications
You must be signed in to change notification settings - Fork 247
/
zz_migration.0001_initial_schema.go
71 lines (65 loc) · 3 KB
/
zz_migration.0001_initial_schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package migrations
import "fmt"
func createMigrationVersion(t *tables) string {
// we need the additional primary key column because github.com/github/gh-ost requires a shared, not-null
// key between the _to_ and _from_ table schemas to perform a schema migration.
// -- https://github.com/github/gh-ost/blob/master/doc/shared-key.md
return fmt.Sprintf(`CREATE TABLE %s (
id int(11) NOT NULL PRIMARY KEY,
_meta_version_ VARCHAR(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
t.migrationVersion(),
)
}
// namespace max size: https://buf.build/authzed/api/file/main/authzed/api/v0/core.proto#L29
func createNamespaceConfig(t *tables) string {
return fmt.Sprintf(`CREATE TABLE %s (
namespace VARCHAR(128) NOT NULL,
serialized_config BLOB NOT NULL,
created_transaction BIGINT NOT NULL,
deleted_transaction BIGINT NOT NULL DEFAULT '9223372036854775807',
CONSTRAINT pk_namespace_config PRIMARY KEY (namespace, created_transaction),
CONSTRAINT uq_namespace_living UNIQUE (namespace, deleted_transaction)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
t.Namespace(),
)
}
// relationship max size: https://buf.build/authzed/api/file/main:authzed/api/v1/core.proto#L33
// object id max size: https://buf.build/authzed/api/file/main:authzed/api/v1/core.proto#L45
func createRelationTuple(t *tables) string {
return fmt.Sprintf(`CREATE TABLE %s (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
namespace VARCHAR(128) NOT NULL,
object_id VARCHAR(128) NOT NULL,
relation VARCHAR(64) NOT NULL,
userset_namespace VARCHAR(128) NOT NULL,
userset_object_id VARCHAR(128) NOT NULL,
userset_relation VARCHAR(64) NOT NULL,
created_transaction BIGINT NOT NULL,
deleted_transaction BIGINT NOT NULL DEFAULT '9223372036854775807',
PRIMARY KEY (id),
CONSTRAINT uq_relation_tuple_namespace UNIQUE (namespace, object_id, relation, userset_namespace, userset_object_id, userset_relation, created_transaction, deleted_transaction),
CONSTRAINT uq_relation_tuple_living UNIQUE (namespace, object_id, relation, userset_namespace, userset_object_id, userset_relation, deleted_transaction),
INDEX ix_relation_tuple_by_subject (userset_object_id, userset_namespace, userset_relation, namespace, relation),
INDEX ix_relation_tuple_by_subject_relation (userset_namespace, userset_relation, namespace, relation),
INDEX ix_relation_tuple_by_deleted_transaction (deleted_transaction)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
t.RelationTuple(),
)
}
func createRelationTupleTransaction(t *tables) string {
return fmt.Sprintf(`CREATE TABLE %s (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
timestamp DATETIME(6) DEFAULT NOW(6) NOT NULL,
PRIMARY KEY (id),
INDEX ix_relation_tuple_transaction_by_timestamp (timestamp)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
t.RelationTupleTransaction(),
)
}
func init() {
mustRegisterMigration("initial", "", noNonatomicMigration,
newStatementBatch(
createMigrationVersion,
createNamespaceConfig,
createRelationTuple,
createRelationTupleTransaction,
).execute,
)
}