1
0
mirror of https://github.com/bitwarden/server synced 2026-01-29 07:43:22 +00:00

sql fixups

- prefer non-keyword column name
- i16 corresponds to smallint, not u8
- column name typo in batch read
This commit is contained in:
Matt Gibson
2025-10-28 15:18:26 -07:00
parent 8de07787e8
commit 2b4430eef7
3 changed files with 24 additions and 30 deletions

View File

@@ -1,9 +1,9 @@
IF OBJECT_ID('dbo.akd_azks', 'U') IS NULL
BEGIN
CREATE TABLE dbo.akd_azks (
[key] SMALLINT NOT NULL CHECK ([key] >= 0),
akd_key SMALLINT NOT NULL CHECK (akd_key >= 0),
epoch BIGINT NOT NULL CHECK (epoch >= 0),
num_nodes BIGINT NOT NULL CHECK (num_nodes >= 0),
PRIMARY KEY ([key])
PRIMARY KEY (akd_key)
);
END

View File

@@ -125,8 +125,8 @@ impl MsSqlStorable for DbRecord {
DbRecord::Azks(azks) => {
debug!(epoch = azks.latest_epoch, num_nodes = azks.num_nodes, "Building AZKS set statement");
let mut params = SqlParams::new();
params.add("key", Box::new(1u8)); // constant key
// TODO: Fixup as conversions
params.add("akd_key", Box::new(1i16)); // constant key
// TODO: Fixup as conversions
params.add("epoch", Box::new(azks.latest_epoch as i64));
params.add("num_nodes", Box::new(azks.num_nodes as i64));
@@ -134,7 +134,7 @@ impl MsSqlStorable for DbRecord {
r#"
MERGE INTO dbo.{TABLE_AZKS} AS t
USING (SELECT {}) AS source
ON t.[key] = source.[key]
ON t.akd_key = source.akd_key
WHEN MATCHED THEN
UPDATE SET {}
WHEN NOT MATCHED THEN
@@ -142,7 +142,7 @@ impl MsSqlStorable for DbRecord {
VALUES ({});
"#,
params.keys_as_columns().join(", "),
params.set_columns_equal_except("t.", "source.", vec!["key"]).join(", "),
params.set_columns_equal_except("t.", "source.", vec!["akd_key"]).join(", "),
params.columns().join(", "),
params.keys().join(", ")
);
@@ -306,14 +306,14 @@ impl MsSqlStorable for DbRecord {
r#"
MERGE INTO dbo.{TABLE_AZKS} AS t
USING {} AS source
ON t.[key] = source.[key]
ON t.[akd_key] = source.[akd_key]
WHEN MATCHED THEN
UPDATE SET
t.[epoch] = source.[epoch],
t.[num_nodes] = source.[num_nodes]
WHEN NOT MATCHED THEN
INSERT ([key], [epoch], [num_nodes])
VALUES (source.[key], source.[epoch], source.[num_nodes]);
INSERT ([akd_key], [epoch], [num_nodes])
VALUES (source.[akd_key], source.[epoch], source.[num_nodes]);
"#,
TempTable::Azks.to_string()
),
@@ -581,7 +581,7 @@ impl MsSqlStorable for DbRecord {
let left_child_len: Option<i32> = row.get("left_child_len");
let left_child_label_val: Option<&[u8]> = row.get("left_child_label_val");
let right_child_len: Option<i32> = row.get("right_child_len");
let right_child_label_val: Option<&[u8]> = row.get("right_child_len");
let right_child_label_val: Option<&[u8]> = row.get("right_child_label_val");
let hash: &[u8] = row
.get("hash")
.ok_or_else(|| StorageError::Other("hash is NULL or missing".to_string()))?;
@@ -674,7 +674,7 @@ impl MsSqlStorable for DbRecord {
match &self {
DbRecord::Azks(azks) => {
let row = (
1u8, // constant key
1i16, // constant key
azks.latest_epoch as i64,
azks.num_nodes as i64,
)

View File

@@ -44,17 +44,16 @@ impl TempTable {
match self {
TempTable::Azks => format!(
r#"
CREATE TABLE {} (
[key] SMALLINT NOT NULL PRIMARY KEY,
CREATE TABLE {TEMP_AZKS_TABLE} (
[akd_key] SMALLINT NOT NULL PRIMARY KEY,
[epoch] BIGINT NOT NULL,
[num_nodes] BIGINT NOT NULL
);
"#,
TEMP_AZKS_TABLE
"#
),
TempTable::HistoryTreeNodes => format!(
r#"
CREATE TABLE {} (
CREATE TABLE {TEMP_HISTORY_TREE_NODES_TABLE} (
label_len INT NOT NULL CHECK (label_len >= 0),
label_val VARBINARY(32) NOT NULL,
last_epoch BIGINT NOT NULL CHECK (last_epoch >= 0),
@@ -79,12 +78,11 @@ impl TempTable {
p_hash VARBINARY(32) NULL,
PRIMARY KEY (label_len, label_val)
);
"#,
TEMP_AZKS_TABLE
"#
),
TempTable::Values => format!(
r#"
CREATE TABLE {} (
CREATE TABLE {TEMP_VALUES_TABLE} (
raw_label VARBINARY(256) NOT NULL,
epoch BIGINT NOT NULL CHECK (epoch >= 0),
[version] BIGINT NOT NULL CHECK ([version] >= 0),
@@ -93,40 +91,36 @@ impl TempTable {
[data] VARBINARY(2000) NULL,
PRIMARY KEY (raw_label, epoch)
);
"#,
TEMP_VALUES_TABLE
"#
),
TempTable::Ids(storage_type) => match storage_type {
StorageType::Azks => panic!("TempTable::Ids should not be created for Azks"),
StorageType::TreeNode => format!(
r#"
CREATE TABLE {} (
CREATE TABLE {TEMP_IDS_TABLE} (
label_len INT NOT NULL,
label_val VARBINARY(32) NOT NULL,
PRIMARY KEY (label_len, label_val)
);
"#,
TEMP_IDS_TABLE
"#
),
StorageType::ValueState => format!(
r#"
CREATE TABLE {} (
CREATE TABLE {TEMP_IDS_TABLE} (
raw_label VARBINARY(256) NOT NULL,
epoch BIGINT NOT NULL,
PRIMARY KEY (raw_label, epoch)
);
"#,
TEMP_IDS_TABLE
"#
),
}
TempTable::RawLabelSearch => format!(
r#"
CREATE TABLE {} (
CREATE TABLE {TEMP_SEARCH_LABELS_TABLE} (
raw_label VARBINARY(256) NOT NULL,
PRIMARY KEY (raw_label)
);
"#,
TEMP_SEARCH_LABELS_TABLE
"#
)
}
}