Skip to content
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

Use raw literal strings goodness #31031

Merged
merged 1 commit into from
Jun 6, 2023
Merged

Use raw literal strings goodness #31031

merged 1 commit into from
Jun 6, 2023

Conversation

roji
Copy link
Member

@roji roji commented Jun 5, 2023

No description provided.

@roji roji requested a review from a team June 5, 2023 08:53
@roji roji changed the title Use raw literal strings in some tests Use raw literal strings goodness Jun 5, 2023
.Append("BEGIN")
.ToString();
return
$"""
Copy link
Contributor

@stevendarby stevendarby Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just picking one example in these changes- couldn't this whole string be indented all the way to its more natural position (e.g. one tab space in from the above return) and take advantage of the fact that left whitespace is ignored with raw literal strings?

Copy link
Member Author

@roji roji Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but I tend to systematically send multiline SQL strings like this to the very left, to use up all that valuable whitespace (these tend to sometimes be long lines). It's also an embedded language at the end of the day (SQL), so it makes sense to me to read it like that - any particular argument/objection?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real objection, I just figured the main advantages of raw string literals were the handling of quotes and whitespace, neither of which seemed taken full advantage of here, so wanted to check your thinking. It's still very helpfully removing that initial whitespace and what you said makes sense, so all good :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. It's still useful in that it allows the first line to be aligned the same as the rest (without adding a leading newline), and it also frees us from having to escape double-quotes, which is super-useful for SQLite baselines where quotes appear very often inside SQL...

@@ -235,7 +248,10 @@ public override void Can_generate_idempotent_up_scripts()
BEGIN TRANSACTION;
GO

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000001_Migration1')
IF NOT EXISTS (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bricelam note this small formatting change around IF EXISTS/NOT EXISTS - I made this to align it more with the kind of SQL we produce in query, and generally make it a bit more readable. Let me know if you're OK with this.

@roji
Copy link
Member Author

roji commented Jun 6, 2023

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Comment on lines 66 to 70
var builder = new StringBuilder()
.Append("IF OBJECT_ID(")
.Append(
stringTypeMapping.GenerateSqlLiteral(
SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema)))
.AppendLine(") IS NULL")
.AppendLine("BEGIN");
var builder = new StringBuilder(
$"""
IF OBJECT_ID({stringTypeMapping.GenerateSqlLiteral(SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema))}) IS NULL
BEGIN

""");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☹️ This will embed the line endings of the compile-time platform instead of using runtime platform's line endings...

Copy link
Member Author

@roji roji Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll revert this part of the change and keep using Append/AppendLine. We should maybe have a function for taking care of that, i.e. pass a string and making sure its newlines correspond to the runtime platform (but not in this PR).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or just normalize all SQL to \n to save bytes 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, something or other :) We did have that recent discussion on varying newlines from different cilent machines causing duplicate plan cache entries in SQL Server (though that's really irrelevant to anything in migrations).

Anyway...

@roji roji merged commit b5ea6f5 into dotnet:main Jun 6, 2023
@roji roji deleted the StringBliss branch June 6, 2023 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants