Skip to content

Commit d965b73

Browse files
feat(database-ui): provide directory as well as db file path for database discovery
1 parent 7e2e6ef commit d965b73

File tree

6 files changed

+305
-33
lines changed

6 files changed

+305
-33
lines changed

packages/cli/src/main/kotlin/elide/tool/cli/cmd/db/DbStudioCommand.kt

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ data class DiscoveredDatabase(
5757
val name: String,
5858
val size: Long,
5959
val lastModified: Long,
60-
val isLocal: Boolean = false, // Whether it's in the current working directory
6160
)
6261

6362
@Command(
@@ -88,7 +87,6 @@ internal class DbStudioCommand : AbstractSubcommand<ToolState, CommandContext>()
8887
name = name,
8988
size = fileSize(),
9089
lastModified = getLastModifiedTime().toMillis(),
91-
isLocal = true,
9290
)
9391
}.getOrNull()
9492

@@ -107,13 +105,13 @@ internal class DbStudioCommand : AbstractSubcommand<ToolState, CommandContext>()
107105
}
108106
}.getOrElse { emptyList() }
109107

110-
private fun discoverDatabases(): List<DiscoveredDatabase> {
111-
val cwd = Path.of(System.getProperty("user.dir"))
108+
private fun discoverDatabases(baseDir: Path? = null): List<DiscoveredDatabase> {
109+
val searchDir = baseDir ?: Path.of(System.getProperty("user.dir"))
112110

113111
// Search current directory and immediate subdirectories
114-
val currentDir = searchDirectory(cwd, depth = 0, maxDepth = 0)
112+
val currentDir = searchDirectory(searchDir, depth = 0, maxDepth = 0)
115113
val subDirs = runCatching {
116-
cwd.listDirectoryEntries()
114+
searchDir.listDirectoryEntries()
117115
.filter { it.isDirectory() }
118116
.flatMap { searchDirectory(it, depth = 1, maxDepth = 1) }
119117
}.getOrElse { emptyList() }
@@ -144,7 +142,7 @@ internal class DbStudioCommand : AbstractSubcommand<ToolState, CommandContext>()
144142

145143
@Parameters(
146144
index = "0",
147-
description = ["Path to SQLite database file"],
145+
description = ["Path to SQLite database file or directory for discovery"],
148146
arity = "0..1",
149147
paramLabel = "DATABASE_PATH",
150148
)
@@ -203,22 +201,38 @@ internal class DbStudioCommand : AbstractSubcommand<ToolState, CommandContext>()
203201

204202
discovered
205203
} else {
206-
val dbFile = Path.of(databasePath!!)
204+
val dbPath = Path.of(databasePath!!)
207205

208-
if (!dbFile.exists()) {
209-
return CommandResult.err(message = "Database file not found: $databasePath")
206+
if (!dbPath.exists()) {
207+
return CommandResult.err(message = "Path not found: $databasePath")
210208
}
211209

212-
// Create a single-item list with the specified database
213-
listOf(
214-
DiscoveredDatabase(
215-
path = dbFile.toAbsolutePath().toString(),
216-
name = dbFile.name,
217-
size = dbFile.fileSize(),
218-
lastModified = dbFile.getLastModifiedTime().toMillis(),
219-
isLocal = true,
220-
)
221-
)
210+
when {
211+
// If it's a directory, discover databases within it
212+
dbPath.isDirectory() -> {
213+
val discovered = discoverDatabases(dbPath)
214+
215+
if (discovered.isEmpty()) {
216+
return CommandResult.err(message = "No SQLite databases found in directory: $databasePath")
217+
}
218+
219+
discovered
220+
}
221+
// If it's a file, use it as a single database
222+
dbPath.isRegularFile() -> {
223+
listOf(
224+
DiscoveredDatabase(
225+
path = dbPath.toAbsolutePath().toString(),
226+
name = dbPath.name,
227+
size = dbPath.fileSize(),
228+
lastModified = dbPath.getLastModifiedTime().toMillis(),
229+
)
230+
)
231+
}
232+
else -> {
233+
return CommandResult.err(message = "Path must be a file or directory: $databasePath")
234+
}
235+
}
222236
}
223237

224238
// Generate config.ts with port and databases

packages/cli/src/projects/db-studio/api/config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const config = {
1717
name: "sample.db",
1818
size: 0,
1919
lastModified: Date.now(),
20-
isLocal: true,
2120
}
2221
] as DiscoveredDatabase[],
2322
};

packages/cli/src/projects/db-studio/api/database.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export interface DiscoveredDatabase {
2020
name: string;
2121
size: number;
2222
lastModified: number;
23-
isLocal: boolean;
2423
}
2524

2625
export interface TableInfo {
@@ -40,7 +39,6 @@ export interface DatabaseInfo {
4039
name: string;
4140
size: number;
4241
lastModified: number;
43-
isLocal: boolean;
4442
tableCount: number;
4543
}
4644

@@ -119,7 +117,6 @@ export function getDatabaseInfo(db: Database, dbPath: string): DatabaseInfo {
119117
name,
120118
size: 0, // Will be populated by calling code if available
121119
lastModified: 0, // Will be populated by calling code if available
122-
isLocal: false, // Will be populated by calling code if available
123120
tableCount: tablesResult?.count ?? 0,
124121
};
125122
}

0 commit comments

Comments
 (0)