Skip to content

Commit 38497d6

Browse files
author
Justin Chung
committed
Add sqlite specific tests, update csproj with new .net8.0 dependency
1 parent d056909 commit 38497d6

File tree

4 files changed

+470
-59
lines changed

4 files changed

+470
-59
lines changed

PSReadLine/History.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ FROM ExecutionHistory eh
289289
createTablesCommand.ExecuteNonQuery();
290290

291291
// Migrate existing text file history if it exists
292-
// MigrateTextHistoryToSQLite(connection);
292+
MigrateTextHistoryToSQLite(connection);
293293
}
294294
}
295295
catch (SqliteException ex)
@@ -304,16 +304,15 @@ FROM ExecutionHistory eh
304304

305305
private void MigrateTextHistoryToSQLite(SqliteConnection connection)
306306
{
307-
// Check if text history file exists
308-
string textHistoryPath = _options.HistorySavePath.Replace(".sqlite", ".txt");
307+
// Derive the text history path from the SQLite path.
308+
// Both files share the same directory and host prefix:
309+
// ConsoleHost_history.txt (text)
310+
// ConsoleHost_history.db (SQLite)
311+
// By the time this runs, HistorySavePath is already the .db path.
312+
string textHistoryPath = Path.ChangeExtension(_options.HistorySavePath, ".txt");
309313
if (!File.Exists(textHistoryPath))
310314
{
311-
// Try default history path pattern
312-
textHistoryPath = Path.ChangeExtension(_options.HistorySavePath, ".txt");
313-
if (!File.Exists(textHistoryPath))
314-
{
315-
return; // No text history to migrate
316-
}
315+
return; // No text history to migrate
317316
}
318317

319318
try
@@ -518,12 +517,15 @@ private long GetOrCreateCommandId(SqliteConnection connection, string commandLin
518517
using var insertCommand = connection.CreateCommand();
519518
insertCommand.CommandText = @"
520519
INSERT INTO Commands (CommandLine, CommandHash)
521-
VALUES (@CommandLine, @CommandHash)
522-
RETURNING Id";
520+
VALUES (@CommandLine, @CommandHash)";
523521
insertCommand.Parameters.AddWithValue("@CommandLine", commandLine);
524522
insertCommand.Parameters.AddWithValue("@CommandHash", commandHash);
523+
insertCommand.ExecuteNonQuery();
525524

526-
return Convert.ToInt64(insertCommand.ExecuteScalar());
525+
// Get the inserted row ID
526+
using var lastIdCommand = connection.CreateCommand();
527+
lastIdCommand.CommandText = "SELECT last_insert_rowid()";
528+
return Convert.ToInt64(lastIdCommand.ExecuteScalar());
527529
}
528530

529531
// Helper method to get or create a location ID
@@ -544,11 +546,14 @@ private long GetOrCreateLocationId(SqliteConnection connection, string location)
544546
using var insertCommand = connection.CreateCommand();
545547
insertCommand.CommandText = @"
546548
INSERT INTO Locations (Path)
547-
VALUES (@Path)
548-
RETURNING Id";
549+
VALUES (@Path)";
549550
insertCommand.Parameters.AddWithValue("@Path", location);
551+
insertCommand.ExecuteNonQuery();
550552

551-
return Convert.ToInt64(insertCommand.ExecuteScalar());
553+
// Get the inserted row ID
554+
using var lastIdCommand = connection.CreateCommand();
555+
lastIdCommand.CommandText = "SELECT last_insert_rowid()";
556+
return Convert.ToInt64(lastIdCommand.ExecuteScalar());
552557
}
553558

554559
private void WriteHistoryToSQLite(int start, int end)
@@ -1442,17 +1447,7 @@ private void HistoryRecall(int direction)
14421447
}
14431448
_recallHistoryCommandCount += 1;
14441449

1445-
// For SQLite/location-filtered history, allow returning to the current line
1446-
if (_options.HistoryType == HistoryType.SQLite &&
1447-
(newHistoryIndex < 0 || newHistoryIndex >= _history.Count))
1448-
{
1449-
_currentHistoryIndex = _history.Count;
1450-
var moveCursor = InViCommandMode() && !_options.HistorySearchCursorMovesToEnd
1451-
? HistoryMoveCursor.ToBeginning
1452-
: HistoryMoveCursor.ToEnd;
1453-
UpdateFromHistory(moveCursor);
1454-
}
1455-
else if (newHistoryIndex >= 0 && newHistoryIndex <= _history.Count)
1450+
if (newHistoryIndex >= 0 && newHistoryIndex <= _history.Count)
14561451
{
14571452
_currentHistoryIndex = newHistoryIndex;
14581453
var moveCursor = InViCommandMode() && !_options.HistorySearchCursorMovesToEnd

PSReadLine/PSReadLine.csproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.4" />
2525
<PackageReference Include="Microsoft.PowerShell.Pager" Version="1.0.0" />
2626
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.11" />
27-
28-
<!-- Platform-specific SQLite bundles -->
29-
<!-- Use Windows system SQLite on Windows -->
30-
<PackageReference Include="SQLitePCLRaw.bundle_winsqlite3" Version="2.1.11" Condition="$([MSBuild]::IsOSPlatform('Windows'))" />
31-
<!-- Use embedded SQLite with all native libraries for Linux/macOS -->
32-
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.11" Condition="!$([MSBuild]::IsOSPlatform('Windows'))" />
33-
34-
<ProjectReference Include="..\Polyfill\Polyfill.csproj" />
3527
</ItemGroup>
3628

3729
<ItemGroup>

test/HistoryTest.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,30 +1221,5 @@ public void HistoryCount()
12211221
Test("cccc", Keys("cccc"));
12221222
Test("aaaa", Keys(Enumerable.Repeat(_.UpArrow, 4)));
12231223
}
1224-
1225-
[SkippableFact]
1226-
public void SetPSReadLineOption_HistoryType_AcceptsSQLite()
1227-
{
1228-
var method = typeof(PSConsoleReadLine).GetMethod("SetOptions");
1229-
Assert.NotNull(method);
1230-
1231-
var optionsType = typeof(SetPSReadLineOption);
1232-
var historyTypeProperty = optionsType.GetProperty("HistoryType");
1233-
Assert.NotNull(historyTypeProperty);
1234-
1235-
var options = new SetPSReadLineOption();
1236-
historyTypeProperty.SetValue(options, HistoryType.SQLite);
1237-
1238-
Exception ex = Record.Exception(() => PSConsoleReadLine.SetOptions(options));
1239-
Assert.Null(ex);
1240-
1241-
Assert.Equal(HistoryType.SQLite, historyTypeProperty.GetValue(options));
1242-
1243-
// Set the history type back to the default.
1244-
historyTypeProperty.SetValue(options, HistoryType.Text);
1245-
Assert.Equal(HistoryType.Text, historyTypeProperty.GetValue(options));
1246-
}
1247-
1248-
12491224
}
12501225
}

0 commit comments

Comments
 (0)