feat(sqlite): support attached database schema changes

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-20 10:51:16 +08:00
1 parent 890fd70c05
commit 85836f8e95
41 files changed
+1219 -129

No files matched your search

@@ -26,18 +26,22 @@ export function buildDuckDbAttachDatabaseSql(path: string, name: string): Promis
return api.buildDuckDbAttachDatabaseSql(path, name);
}
export function duckDbAttachedDatabaseNameFromPath(path: string): string {
export function buildSqliteAttachDatabaseSql(path: string, name: string): Promise<string> {
return api.buildSqliteAttachDatabaseSql(path, name);
}
export function attachedDatabaseNameFromPath(path: string, fallbackName = "database"): string {
const fileName = path.split(/[\\/]/).pop() ?? "";
const withoutExtension = fileName.replace(/\.[^.\\/]+$/, "");
const normalized = withoutExtension
.trim()
.replace(/[^\p{L}\p{N}_]+/gu, "_")
.replace(/^_+|_+$/g, "");
return normalized || "duckdb_database";
return normalized || fallbackName;
}
export function uniqueDuckDbAttachedDatabaseName(baseName: string, existingNames: string[]): string {
const existing = new Set(existingNames.map((name) => name.toLowerCase()));
export function uniqueAttachedDatabaseName(baseName: string, existingNames: string[], reservedNames: string[] = []): string {
const existing = new Set([...existingNames, ...reservedNames].map((name) => name.toLowerCase()));
if (!existing.has(baseName.toLowerCase())) return baseName;
for (let index = 2; index < Number.MAX_SAFE_INTEGER; index++) {
const candidate = `${baseName}_${index}`;
@@ -11,13 +11,13 @@ export interface DatabaseNamespaceCreationMatrixEntry {
deferred?: string;
}
type CreationConnection = Pick<ConnectionConfig, "db_type" | "driver_profile" | "read_only"> | undefined;
type CreationConnection = (Pick<ConnectionConfig, "db_type" | "driver_profile" | "read_only"> & Partial<Pick<ConnectionConfig, "host" | "password">>) | undefined;
// Keep creation target-specific: many products expose schemas, files, or provider-managed namespaces instead of a top-level database.
export const DATABASE_NAMESPACE_CREATION_MATRIX = {
mysql: { connection: "database" },
postgres: { connection: "database", database: "schema" },
sqlite: { deferred: "file-backed; create a new connection/file instead" },
sqlite: { connection: "attach" },
rqlite: { deferred: "single SQLite-compatible database per node" },
turso: { deferred: "remote libSQL database lifecycle is provider-managed" },
"cloudflare-d1": { deferred: "Cloudflare D1 database lifecycle is provider-managed" },
@@ -85,6 +85,9 @@ export const DATABASE_NAMESPACE_CREATION_MATRIX = {
export function connectionNamespaceCreationTarget(connection: CreationConnection): ConnectionCreationTarget | null {
if (!connection || connection.read_only) return null;
if (connection.db_type === "mongodb" && connection.driver_profile === "mongodb-legacy") return null;
if (connection.db_type === "sqlite" && (connection.host?.trim().toLowerCase() === ":memory:" || Boolean(connection.password))) {
return null;
}
const entry: DatabaseNamespaceCreationMatrixEntry = DATABASE_NAMESPACE_CREATION_MATRIX[connection.db_type];
return entry.connection ?? null;
}
@@ -101,6 +101,7 @@ export function connectionObjectTreeNodeSchema(connection: JdbcDialectConnection
if (connectionUsesDatabaseObjectTreeMode(connection)) return undefined;
if (schema) return schema;
const type = effectiveDatabaseTypeForConnection(connection);
if (type === "sqlite") return database;
return isSchemaAware(type) ? database : undefined;
}