From b032cd71e66174ccc006524b362b340667267462 Mon Sep 17 00:00:00 2001 From: Elena Grahovac Date: Fri, 8 Sep 2023 08:00:57 +0200 Subject: [PATCH] Implement `listDatabases` for SQLite (#3269) Closes #3260. --- integration/commands_administration_test.go | 5 ++--- internal/backends/sqlite/backend.go | 23 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/integration/commands_administration_test.go b/integration/commands_administration_test.go index e51aaf6604bc..8d7530e23fae 100644 --- a/integration/commands_administration_test.go +++ b/integration/commands_administration_test.go @@ -139,10 +139,9 @@ func TestCommandsAdministrationCreateDropListDatabases(t *testing.T) { assert.Equal(t, bson.D{{"ok", 1.0}}, res) } -func TestCommandsAdministrationListDatabases(tt *testing.T) { - tt.Parallel() +func TestCommandsAdministrationListDatabases(t *testing.T) { + t.Parallel() - t := setup.FailsForSQLite(tt, "https://github.com/FerretDB/FerretDB/issues/3260") ctx, collection := setup.Setup(t, shareddata.DocumentsStrings) db := collection.Database() diff --git a/internal/backends/sqlite/backend.go b/internal/backends/sqlite/backend.go index 2609e0e14fb4..12634ee17bb0 100644 --- a/internal/backends/sqlite/backend.go +++ b/internal/backends/sqlite/backend.go @@ -98,8 +98,27 @@ func (b *backend) ListDatabases(ctx context.Context, params *backends.ListDataba res := &backends.ListDatabasesResult{ Databases: make([]backends.DatabaseInfo, len(list)), } - for i, db := range list { - res.Databases[i] = backends.DatabaseInfo{Name: db} + + for i, dbName := range list { + db, err := b.Database(dbName) + if err != nil { + return nil, lazyerrors.Error(err) + } + + stats, err := db.Stats(ctx, new(backends.DatabaseStatsParams)) + if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseDoesNotExist) { + stats = new(backends.DatabaseStatsResult) + err = nil + } + + if err != nil { + return nil, lazyerrors.Error(err) + } + + res.Databases[i] = backends.DatabaseInfo{ + Name: dbName, + Size: stats.SizeTotal, + } } return res, nil