Skip to content

Commit 744bf42

Browse files
authored
Merge pull request #4 from managedcode/kovtun/fix
Add check for existing 'age' extension before loading in AgeConnectio…
2 parents 87545a2 + 8f41b76 commit 744bf42

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<RepositoryUrl>https://github.com/managedcode/graphrag</RepositoryUrl>
2727
<PackageProjectUrl>https://github.com/managedcode/graphrag</PackageProjectUrl>
2828
<Product>Managed Code GraphRag</Product>
29-
<Version>10.0.5</Version>
30-
<PackageVersion>10.0.5</PackageVersion>
29+
<Version>10.0.6</Version>
30+
<PackageVersion>10.0.6</PackageVersion>
3131

3232
</PropertyGroup>
3333
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,38 @@ private async Task LoadAgeAsync(NpgsqlConnection connection, CancellationToken c
132132
{
133133
try
134134
{
135+
await using var checkCommand = connection.CreateCommand();
136+
checkCommand.CommandText = "SELECT 1 FROM pg_extension WHERE extname = 'age';";
137+
checkCommand.CommandTimeout = 0;
138+
var result = await checkCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
139+
140+
if (result is null)
141+
{
142+
throw new AgeException("AGE extension is not installed.");
143+
}
144+
135145
await using var load = connection.CreateCommand();
136146
load.CommandText = "LOAD 'age';";
137147
load.CommandTimeout = 0;
138148
await load.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
139149
LogMessages.ExtensionLoaded(_logger, ConnectionString);
140150
}
151+
catch (PostgresException ex) when (ex.SqlState == "42501")
152+
{
153+
await using var initCommand = connection.CreateCommand();
154+
initCommand.CommandText = "SELECT ag_catalog.create_graph('__age_init__'); SELECT ag_catalog.drop_graph('__age_init__', true);";
155+
initCommand.CommandTimeout = 0;
156+
157+
try
158+
{
159+
await initCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
160+
}
161+
catch (PostgresException)
162+
{
163+
}
164+
165+
LogMessages.ExtensionLoaded(_logger, ConnectionString);
166+
}
141167
catch (PostgresException ex)
142168
{
143169
LogMessages.ExtensionNotLoadedError(_logger, ConnectionString, ex.MessageText);

0 commit comments

Comments
 (0)