Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageVersion Include="coverlet.collector" Version="10.0.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="10.0.301" />
<PackageVersion Include="Npgsql.DependencyInjection" Version="10.0.3" />
<PackageVersion Include="NSchema.Core" Version="5.0.0-alpha.5" />
<PackageVersion Include="NSchema.Core" Version="5.0.0-alpha.8" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
<PackageVersion Include="Npgsql" Version="10.0.3" />
<PackageVersion Include="NSubstitute" Version="6.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSchema.Postgres/NSchema.Postgres.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>5.0.0-alpha.5</Version>
<Version>5.0.0-alpha.6</Version>
<AssemblyVersion>$(Version.Split('-')[0])</AssemblyVersion>
<FileVersion>$(Version.Split('-')[0])</FileVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
29 changes: 18 additions & 11 deletions src/NSchema.Postgres/PostgresPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using NSchema.Configuration.Plugins;
using NSchema.Plugins;
using NSchema.Project.Nsql;
using NSchema.Project.Nsql.Syntax;
using NSchema.Project.Nsql.Syntax.Blocks;
using NSchema.Project.Nsql.Tokens;

namespace NSchema.Postgres;

Expand All @@ -24,17 +28,20 @@ private sealed class PostgresOptions
}

/// <inheritdoc />
public string GetScaffoldTemplate(ScaffoldContext context) =>
$"""
DATABASE postgres (
-- Prefer the {EnvConnectionString} environment variable, which
-- overrides the value below.
connection_string = ''
-- Credentials may be supplied separately from the connection string (e.g. from
-- a secret store) via {EnvUsername} / {EnvPassword}.
-- They override any user/password embedded in connection_string.
);
""";
public BlockStatement GetScaffoldTemplate(ScaffoldContext context) =>
new(BlockKeyword.Database, Identifier.Synthetic("postgres"), new SeparatedSyntaxList<BlockAttribute>(
[
new BlockAttribute("connection_string", string.Empty),
]))
{
DocComment = new Token(
TokenKind.DocComment,
$"Prefer the {EnvConnectionString} environment variable, which overrides the value below.\n" +
$"Credentials may be supplied separately from the connection string (e.g. from a secret\n" +
$"store) via {EnvUsername} / {EnvPassword}. They override any user/password embedded in\n" +
"connection_string.",
SourcePosition.None),
};

/// <inheritdoc />
public string GetSampleSchema() =>
Expand Down
5 changes: 4 additions & 1 deletion src/NSchema.Postgres/Sql/PostgresDatabaseIntrospector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public async ValueTask<Database> GetDatabase(PlanningScope scope, CancellationTo
await using var conn = await dataSource.OpenConnectionAsync(cancellationToken);

// The scope is a hint; null means "all visible schemas" and the engine re-applies the scope after the read.
var schemas = scope.IsUnscoped ? null : scope.SchemaNames.Select(s => s.Value).ToArray();
// Every address belongs to a schema (a schema address is its own), so the read narrows to those.
var schemas = scope.IsUnscoped
? null
: scope.Addresses.Select(a => a.SchemaName).OfType<SqlIdentifier>().Distinct().Select(s => s.Value).ToArray();

var tables = await QueryTables(conn, schemas, cancellationToken);
var columns = await QueryColumns(conn, schemas, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private NSchemaApplication BuildApp()

private async Task<Database> Introspect() =>
await new PostgresDatabaseIntrospector(_fixture.DataSource)
.GetDatabase(PlanningScope.To([new SqlIdentifier(_schema)]), TestContext.Current.CancellationToken);
.GetDatabase(PlanningScope.To(new SchemaAddress(_schema)), TestContext.Current.CancellationToken);

private async Task Exec(string sql)
{
Expand Down
9 changes: 8 additions & 1 deletion tests/NSchema.Postgres.Tests/PostgresPluginTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NSchema.Configuration.Plugins;
using NSchema.Plan.Backends;
using NSchema.Plugins;
using NSchema.Project.Nsql.Syntax.Blocks;

namespace NSchema.Postgres.Tests;

Expand Down Expand Up @@ -41,7 +42,13 @@ public void Dispose()

[Fact]
public void GetScaffoldTemplate_ReturnsDatabaseStatement()
=> _sut.GetScaffoldTemplate(new ScaffoldContext()).ShouldContain("DATABASE postgres");
{
var block = _sut.GetScaffoldTemplate(new ScaffoldContext());

block.Keyword.ShouldBe(BlockKeyword.Database);
block.Label!.Value.ShouldBe("postgres");
block.Attributes.ShouldContain(a => a.Key == "connection_string");
}

[Fact]
public void GetSampleSchema_ScaffoldsANamedSchema()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private async Task Exec(string sql)

/// <summary>Reads the live schema scoped to the given schema names.</summary>
private async Task<Database> Introspect(params string[] schemas) =>
await _sut.GetDatabase(PlanningScope.To(schemas.Select(s => new SqlIdentifier(s))), TestContext.Current.CancellationToken);
await _sut.GetDatabase(PlanningScope.To(schemas.Select(s => new SchemaAddress(s))), TestContext.Current.CancellationToken);

// ── Schema / table structure ──────────────────────────────────────────────

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ private async Task Run(params MigrationAction[] actions)

private async Task<Database> Introspect() =>
await new PostgresDatabaseIntrospector(_dataSource)
.GetDatabase(PlanningScope.To([new SqlIdentifier(_schema)]), TestContext.Current.CancellationToken);
.GetDatabase(PlanningScope.To(new SchemaAddress(_schema)), TestContext.Current.CancellationToken);

private async Task Exec(string sql)
{
Expand Down