Skip to content

fix: escape " in check constraint generator #576

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

Closed
wants to merge 3 commits into from

Conversation

frankdugan3
Copy link
Contributor

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

@frankdugan3
Copy link
Contributor Author

Actually, I think there's still some problems. Let me investigate further before you check on this.

@frankdugan3
Copy link
Contributor Author

OK, it's good now, derp! 🤪

zachdaniel
zachdaniel previously approved these changes Jun 27, 2025
@zachdaniel
Copy link
Contributor

Actually, we have to handle already escaped double quotes only right? otherwise this effectively unescapes them, turning \" into \\". So we ned to replace all unescaped double quotes i.e " that start w/ zero or an od number of \.

@frankdugan3
Copy link
Contributor Author

frankdugan3 commented Jun 27, 2025

This is one of those things that makes my brain a little dizzy, lol. I want to get the test case right so I can get the implementation correct.

Given:

check: ~S[title ~= '("\"\\"\\\"\\\\"\\\\\")']

If we replace just odd slashes, we should expect the migration file to have:

check: "title ~= '(\"\\"\\"\\\\"\\\\"\\\\\\")'"

Is that right? 😵‍💫

@frankdugan3
Copy link
Contributor Author

Thinking about it more, the issue is distinguishing quotes from plain slashes?

So a better check would be:

check: "title ~= '(\"\\\"\\\\\")'"

Becomes:

"check: \"title ~= '(\\\"\\\\\"\\\\\\\")'\""

@zachdaniel
Copy link
Contributor

Thinking about this more, I don't think we need to "fix" this? If you want double quotes in that code, you just need to do \\". closing for now, but feel free to discuss further if you disagree.

@zachdaniel zachdaniel closed this Jun 27, 2025
@frankdugan3
Copy link
Contributor Author

frankdugan3 commented Jun 27, 2025

Well, it's up to you but I do somewhat strongly disagree because it's inconsistent syntax compared to custom statements and fragments. And if you use multiline sigils, it's very strange, and if using something like ~SQL, it breaks syntax highlighting. It's kind of unexpected that the generated value of the check string differs from that in the resource.

I agree what I did is an ugly solution, though.

Custom statements do this, which bypasses the problem entirely:

def up(%{statement: %{up: up, code?: false}}) do
  """
  execute(\"\"\"
  #{String.trim(up)}
  \"\"\")
  """
end

Perhaps a simpler refactor to do this instead would be a better middle ground like this:

"""
create constraint(:#{as_atom(table)}, :#{as_atom(name)}, check: \"\"\"
#{String.trim(check)}#{option(:prefix, schema)}
\"\"\")
"""

That would be consistent with the other generators, and sidestep my original mess.

@zachdaniel
Copy link
Contributor

I'm down with that solution.

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.

2 participants