Skip to content

Commit d43df9e

Browse files
authored
Fix workspace migration failure (#36911)
This fixes a regression on nightly introduced in #36714 Release Notes: - N/A
1 parent f8667a8 commit d43df9e

File tree

16 files changed

+588
-501
lines changed

16 files changed

+588
-501
lines changed

crates/command_palette/src/persistence.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use anyhow::Result;
22
use db::{
3-
define_connection, query,
4-
sqlez::{bindable::Column, statement::Statement},
3+
query,
4+
sqlez::{
5+
bindable::Column, domain::Domain, statement::Statement,
6+
thread_safe_connection::ThreadSafeConnection,
7+
},
58
sqlez_macros::sql,
69
};
710
use serde::{Deserialize, Serialize};
@@ -50,16 +53,21 @@ impl Column for SerializedCommandInvocation {
5053
}
5154
}
5255

53-
define_connection!(pub static ref COMMAND_PALETTE_HISTORY: CommandPaletteDB<()> =
54-
&[sql!(
56+
pub struct CommandPaletteDB(ThreadSafeConnection);
57+
58+
impl Domain for CommandPaletteDB {
59+
const NAME: &str = stringify!(CommandPaletteDB);
60+
const MIGRATIONS: &[&str] = &[sql!(
5561
CREATE TABLE IF NOT EXISTS command_invocations(
5662
id INTEGER PRIMARY KEY AUTOINCREMENT,
5763
command_name TEXT NOT NULL,
5864
user_query TEXT NOT NULL,
5965
last_invoked INTEGER DEFAULT (unixepoch()) NOT NULL
6066
) STRICT;
6167
)];
62-
);
68+
}
69+
70+
db::static_connection!(COMMAND_PALETTE_HISTORY, CommandPaletteDB, []);
6371

6472
impl CommandPaletteDB {
6573
pub async fn write_command_invocation(

crates/db/src/db.rs

Lines changed: 26 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,14 @@ pub async fn open_test_db<M: Migrator>(db_name: &str) -> ThreadSafeConnection {
110110
}
111111

112112
/// Implements a basic DB wrapper for a given domain
113+
///
114+
/// Arguments:
115+
/// - static variable name for connection
116+
/// - type of connection wrapper
117+
/// - dependencies, whose migrations should be run prior to this domain's migrations
113118
#[macro_export]
114-
macro_rules! define_connection {
115-
(pub static ref $id:ident: $t:ident<()> = $migrations:expr; $($global:ident)?) => {
116-
pub struct $t($crate::sqlez::thread_safe_connection::ThreadSafeConnection);
117-
119+
macro_rules! static_connection {
120+
($id:ident, $t:ident, [ $($d:ty),* ] $(, $global:ident)?) => {
118121
impl ::std::ops::Deref for $t {
119122
type Target = $crate::sqlez::thread_safe_connection::ThreadSafeConnection;
120123

@@ -123,16 +126,6 @@ macro_rules! define_connection {
123126
}
124127
}
125128

126-
impl $crate::sqlez::domain::Domain for $t {
127-
fn name() -> &'static str {
128-
stringify!($t)
129-
}
130-
131-
fn migrations() -> &'static [&'static str] {
132-
$migrations
133-
}
134-
}
135-
136129
impl $t {
137130
#[cfg(any(test, feature = "test-support"))]
138131
pub async fn open_test_db(name: &'static str) -> Self {
@@ -142,44 +135,8 @@ macro_rules! define_connection {
142135

143136
#[cfg(any(test, feature = "test-support"))]
144137
pub static $id: std::sync::LazyLock<$t> = std::sync::LazyLock::new(|| {
145-
$t($crate::smol::block_on($crate::open_test_db::<$t>(stringify!($id))))
146-
});
147-
148-
#[cfg(not(any(test, feature = "test-support")))]
149-
pub static $id: std::sync::LazyLock<$t> = std::sync::LazyLock::new(|| {
150-
let db_dir = $crate::database_dir();
151-
let scope = if false $(|| stringify!($global) == "global")? {
152-
"global"
153-
} else {
154-
$crate::RELEASE_CHANNEL.dev_name()
155-
};
156-
$t($crate::smol::block_on($crate::open_db::<$t>(db_dir, scope)))
157-
});
158-
};
159-
(pub static ref $id:ident: $t:ident<$($d:ty),+> = $migrations:expr; $($global:ident)?) => {
160-
pub struct $t($crate::sqlez::thread_safe_connection::ThreadSafeConnection);
161-
162-
impl ::std::ops::Deref for $t {
163-
type Target = $crate::sqlez::thread_safe_connection::ThreadSafeConnection;
164-
165-
fn deref(&self) -> &Self::Target {
166-
&self.0
167-
}
168-
}
169-
170-
impl $crate::sqlez::domain::Domain for $t {
171-
fn name() -> &'static str {
172-
stringify!($t)
173-
}
174-
175-
fn migrations() -> &'static [&'static str] {
176-
$migrations
177-
}
178-
}
179-
180-
#[cfg(any(test, feature = "test-support"))]
181-
pub static $id: std::sync::LazyLock<$t> = std::sync::LazyLock::new(|| {
182-
$t($crate::smol::block_on($crate::open_test_db::<($($d),+, $t)>(stringify!($id))))
138+
#[allow(unused_parens)]
139+
$t($crate::smol::block_on($crate::open_test_db::<($($d,)* $t)>(stringify!($id))))
183140
});
184141

185142
#[cfg(not(any(test, feature = "test-support")))]
@@ -190,9 +147,10 @@ macro_rules! define_connection {
190147
} else {
191148
$crate::RELEASE_CHANNEL.dev_name()
192149
};
193-
$t($crate::smol::block_on($crate::open_db::<($($d),+, $t)>(db_dir, scope)))
150+
#[allow(unused_parens)]
151+
$t($crate::smol::block_on($crate::open_db::<($($d,)* $t)>(db_dir, scope)))
194152
});
195-
};
153+
}
196154
}
197155

198156
pub fn write_and_log<F>(cx: &App, db_write: impl FnOnce() -> F + Send + 'static)
@@ -219,17 +177,12 @@ mod tests {
219177
enum BadDB {}
220178

221179
impl Domain for BadDB {
222-
fn name() -> &'static str {
223-
"db_tests"
224-
}
225-
226-
fn migrations() -> &'static [&'static str] {
227-
&[
228-
sql!(CREATE TABLE test(value);),
229-
// failure because test already exists
230-
sql!(CREATE TABLE test(value);),
231-
]
232-
}
180+
const NAME: &str = "db_tests";
181+
const MIGRATIONS: &[&str] = &[
182+
sql!(CREATE TABLE test(value);),
183+
// failure because test already exists
184+
sql!(CREATE TABLE test(value);),
185+
];
233186
}
234187

235188
let tempdir = tempfile::Builder::new()
@@ -251,25 +204,15 @@ mod tests {
251204
enum CorruptedDB {}
252205

253206
impl Domain for CorruptedDB {
254-
fn name() -> &'static str {
255-
"db_tests"
256-
}
257-
258-
fn migrations() -> &'static [&'static str] {
259-
&[sql!(CREATE TABLE test(value);)]
260-
}
207+
const NAME: &str = "db_tests";
208+
const MIGRATIONS: &[&str] = &[sql!(CREATE TABLE test(value);)];
261209
}
262210

263211
enum GoodDB {}
264212

265213
impl Domain for GoodDB {
266-
fn name() -> &'static str {
267-
"db_tests" //Notice same name
268-
}
269-
270-
fn migrations() -> &'static [&'static str] {
271-
&[sql!(CREATE TABLE test2(value);)] //But different migration
272-
}
214+
const NAME: &str = "db_tests"; //Notice same name
215+
const MIGRATIONS: &[&str] = &[sql!(CREATE TABLE test2(value);)];
273216
}
274217

275218
let tempdir = tempfile::Builder::new()
@@ -305,25 +248,16 @@ mod tests {
305248
enum CorruptedDB {}
306249

307250
impl Domain for CorruptedDB {
308-
fn name() -> &'static str {
309-
"db_tests"
310-
}
251+
const NAME: &str = "db_tests";
311252

312-
fn migrations() -> &'static [&'static str] {
313-
&[sql!(CREATE TABLE test(value);)]
314-
}
253+
const MIGRATIONS: &[&str] = &[sql!(CREATE TABLE test(value);)];
315254
}
316255

317256
enum GoodDB {}
318257

319258
impl Domain for GoodDB {
320-
fn name() -> &'static str {
321-
"db_tests" //Notice same name
322-
}
323-
324-
fn migrations() -> &'static [&'static str] {
325-
&[sql!(CREATE TABLE test2(value);)] //But different migration
326-
}
259+
const NAME: &str = "db_tests"; //Notice same name
260+
const MIGRATIONS: &[&str] = &[sql!(CREATE TABLE test2(value);)]; // But different migration
327261
}
328262

329263
let tempdir = tempfile::Builder::new()

crates/db/src/kvp.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@ use gpui::App;
22
use sqlez_macros::sql;
33
use util::ResultExt as _;
44

5-
use crate::{define_connection, query, write_and_log};
5+
use crate::{
6+
query,
7+
sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection},
8+
write_and_log,
9+
};
610

7-
define_connection!(pub static ref KEY_VALUE_STORE: KeyValueStore<()> =
8-
&[sql!(
11+
pub struct KeyValueStore(crate::sqlez::thread_safe_connection::ThreadSafeConnection);
12+
13+
impl Domain for KeyValueStore {
14+
const NAME: &str = stringify!(KeyValueStore);
15+
16+
const MIGRATIONS: &[&str] = &[sql!(
917
CREATE TABLE IF NOT EXISTS kv_store(
1018
key TEXT PRIMARY KEY,
1119
value TEXT NOT NULL
1220
) STRICT;
1321
)];
14-
);
22+
}
23+
24+
crate::static_connection!(KEY_VALUE_STORE, KeyValueStore, []);
1525

1626
pub trait Dismissable {
1727
const KEY: &'static str;
@@ -91,15 +101,19 @@ mod tests {
91101
}
92102
}
93103

94-
define_connection!(pub static ref GLOBAL_KEY_VALUE_STORE: GlobalKeyValueStore<()> =
95-
&[sql!(
104+
pub struct GlobalKeyValueStore(ThreadSafeConnection);
105+
106+
impl Domain for GlobalKeyValueStore {
107+
const NAME: &str = stringify!(GlobalKeyValueStore);
108+
const MIGRATIONS: &[&str] = &[sql!(
96109
CREATE TABLE IF NOT EXISTS kv_store(
97110
key TEXT PRIMARY KEY,
98111
value TEXT NOT NULL
99112
) STRICT;
100113
)];
101-
global
102-
);
114+
}
115+
116+
crate::static_connection!(GLOBAL_KEY_VALUE_STORE, GlobalKeyValueStore, [], global);
103117

104118
impl GlobalKeyValueStore {
105119
query! {

crates/editor/src/persistence.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
use anyhow::Result;
2-
use db::sqlez::bindable::{Bind, Column, StaticColumnCount};
3-
use db::sqlez::statement::Statement;
2+
use db::{
3+
query,
4+
sqlez::{
5+
bindable::{Bind, Column, StaticColumnCount},
6+
domain::Domain,
7+
statement::Statement,
8+
},
9+
sqlez_macros::sql,
10+
};
411
use fs::MTime;
512
use itertools::Itertools as _;
613
use std::path::PathBuf;
714

8-
use db::sqlez_macros::sql;
9-
use db::{define_connection, query};
10-
1115
use workspace::{ItemId, WorkspaceDb, WorkspaceId};
1216

1317
#[derive(Clone, Debug, PartialEq, Default)]
@@ -83,7 +87,11 @@ impl Column for SerializedEditor {
8387
}
8488
}
8589

86-
define_connection!(
90+
pub struct EditorDb(db::sqlez::thread_safe_connection::ThreadSafeConnection);
91+
92+
impl Domain for EditorDb {
93+
const NAME: &str = stringify!(EditorDb);
94+
8795
// Current schema shape using pseudo-rust syntax:
8896
// editors(
8997
// item_id: usize,
@@ -113,7 +121,8 @@ define_connection!(
113121
// start: usize,
114122
// end: usize,
115123
// )
116-
pub static ref DB: EditorDb<WorkspaceDb> = &[
124+
125+
const MIGRATIONS: &[&str] = &[
117126
sql! (
118127
CREATE TABLE editors(
119128
item_id INTEGER NOT NULL,
@@ -189,7 +198,9 @@ define_connection!(
189198
) STRICT;
190199
),
191200
];
192-
);
201+
}
202+
203+
db::static_connection!(DB, EditorDb, [WorkspaceDb]);
193204

194205
// https://www.sqlite.org/limits.html
195206
// > <..> the maximum value of a host parameter number is SQLITE_MAX_VARIABLE_NUMBER,

crates/image_viewer/src/image_viewer.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,19 @@ pub fn init(cx: &mut App) {
401401
mod persistence {
402402
use std::path::PathBuf;
403403

404-
use db::{define_connection, query, sqlez_macros::sql};
404+
use db::{
405+
query,
406+
sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection},
407+
sqlez_macros::sql,
408+
};
405409
use workspace::{ItemId, WorkspaceDb, WorkspaceId};
406410

407-
define_connection! {
408-
pub static ref IMAGE_VIEWER: ImageViewerDb<WorkspaceDb> =
409-
&[sql!(
411+
pub struct ImageViewerDb(ThreadSafeConnection);
412+
413+
impl Domain for ImageViewerDb {
414+
const NAME: &str = stringify!(ImageViewerDb);
415+
416+
const MIGRATIONS: &[&str] = &[sql!(
410417
CREATE TABLE image_viewers (
411418
workspace_id INTEGER,
412419
item_id INTEGER UNIQUE,
@@ -417,9 +424,11 @@ mod persistence {
417424
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
418425
ON DELETE CASCADE
419426
) STRICT;
420-
)];
427+
)];
421428
}
422429

430+
db::static_connection!(IMAGE_VIEWER, ImageViewerDb, [WorkspaceDb]);
431+
423432
impl ImageViewerDb {
424433
query! {
425434
pub async fn save_image_path(

0 commit comments

Comments
 (0)