-
Notifications
You must be signed in to change notification settings - Fork 6
Add Support for EFCore Schemas by Treating Them as Databases #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Felixzed
wants to merge
8
commits into
ClickHouse:main
Choose a base branch
from
Felixzed:feat/efc-schema-to-ch-db-conversion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6a0fcf
feat: efcore schema to clickhouse database conversion
Felixzed 1e56bd5
test: clickhouse database conversion tests
Felixzed 22900b2
chore; adjust CHANGELOG and RELEASENOTES
Felixzed 68f866f
chore: comment spelling
Felixzed e9a161d
fix: creation now translates to create if not exists
Felixzed a01f075
fix: proper handling of explicit nullable types
Felixzed c11f977
feat: DB resolution now favors schema
Felixzed 000bbdb
fix: changelog
Felixzed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/EFCore.ClickHouse/Infrastructure/Internal/ClickHouseIdentifierHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace ClickHouse.EntityFrameworkCore.Infrastructure.Internal; | ||
|
|
||
| internal static class ClickHouseIdentifierHelper | ||
| { | ||
| internal static string DelimitIdentifier(string name) | ||
| => $"`{name.Replace("`", "``")}`"; | ||
|
|
||
| internal static string BuildQualifiedTableName(string tableName, string? schema) | ||
| => string.IsNullOrWhiteSpace(schema) | ||
| ? DelimitIdentifier(tableName) | ||
| : $"{DelimitIdentifier(schema)}.{DelimitIdentifier(tableName)}"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| [assembly: InternalsVisibleTo("EFCore.ClickHouse.Tests")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/EFCore.ClickHouse/Storage/Internal/ClickHouseDatabaseNameResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace ClickHouse.EntityFrameworkCore.Storage.Internal; | ||
|
|
||
| internal static class ClickHouseDatabaseNameResolver | ||
| { | ||
| public static string Resolve( | ||
| string? connectionDatabase, | ||
| string? schema, | ||
| ILogger logger) | ||
| { | ||
| var hasConnectionDatabase = !string.IsNullOrWhiteSpace(connectionDatabase); | ||
| var hasSchema = !string.IsNullOrWhiteSpace(schema); | ||
|
|
||
| if (!hasSchema) | ||
| return connectionDatabase ?? string.Empty; | ||
|
|
||
| if (!hasConnectionDatabase) | ||
| return schema!; | ||
|
|
||
| if (string.Equals(connectionDatabase, schema, StringComparison.Ordinal)) | ||
| { | ||
| logger.LogInformation( | ||
| "The ClickHouse connection string database '{Database}' is the same as the configured EF Core schema." + | ||
| " The Database defined in the Schema overrides the Connection String database. Setting both is unnecessary.", | ||
| connectionDatabase); | ||
| } | ||
| else | ||
| { | ||
| logger.LogWarning( | ||
| "The configured EF Core schema '{Schema}' overrides the ClickHouse connection " + | ||
| "string database '{Database}'. The connection string database will not be used for this context.", | ||
| schema, | ||
| connectionDatabase); | ||
| } | ||
|
|
||
| return schema!; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using ClickHouse.EntityFrameworkCore.Storage.Internal; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Xunit; | ||
|
|
||
| namespace EFCore.ClickHouse.Tests; | ||
|
|
||
| public class DatabaseNameResolverTests | ||
| { | ||
| [Fact] | ||
| public void ConnectionDatabase_without_schema_is_used() | ||
| { | ||
| var database = ClickHouseDatabaseNameResolver.Resolve("connection_db", null, NullLogger.Instance); | ||
|
|
||
| Assert.Equal("connection_db", database); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Schema_without_connection_database_is_used() | ||
| { | ||
| var database = ClickHouseDatabaseNameResolver.Resolve(null, "schema_db", NullLogger.Instance); | ||
|
|
||
| Assert.Equal("schema_db", database); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Schema_overrides_different_connection_database() | ||
| { | ||
| var database = ClickHouseDatabaseNameResolver.Resolve("connection_db", "schema_db", NullLogger.Instance); | ||
|
|
||
| Assert.Equal("schema_db", database); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Identical_schema_and_connection_database_use_schema() | ||
| { | ||
| var database = ClickHouseDatabaseNameResolver.Resolve("same_db", "same_db", NullLogger.Instance); | ||
|
|
||
| Assert.Equal("same_db", database); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the looks of it, the EFCore EnsureSchema() function means "Let's make sure this is there" so for now it seems to make sense, would it not make more sense to alter ClickHouseCreateDatabase?
Would this be advisable in case the database already exists?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should definitely be with the
IF NOT EXISTS, we don't want this to throw when the db exists.