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

feat(persistence): use sqlean v3.45.1 as sqlite engine #2947

Merged
merged 3 commits into from
Apr 23, 2024
Merged

Conversation

RogerHYang
Copy link
Contributor

This gives:

  • SQLite v3.45.1
  • JSONB
  • text_contains (case sensitive substring search)

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 22, 2024
@@ -147,6 +146,9 @@ def _insert_project_abc(session: Session) -> None:
start_time=datetime.fromisoformat("2021-01-01T00:00:00.000+00:00"),
end_time=datetime.fromisoformat("2021-01-01T00:00:05.000+00:00"),
attributes={
"input": {
"value": "XY%*Z",
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the %* symbol for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because % and * are special symbols in alternative implementations such as LIKE and GLOB, the test will fail (I tried it) if it's not implemented correctly.

url = get_db_url(driver="sqlite+aiosqlite", database=database)
engine = create_async_engine(url=url, echo=echo, json_serializer=_dumps)
async_url = get_async_db_url(url.render_as_string())
assert async_url.database
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this an assertion for not None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's gives the same result, because It's asserting on the truthiness of the expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is mainly needed for the type-checker to rule out the None type for the code below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check is already carried out by lines 39 and 63.

Comment on lines +42 to +43
if url.database.startswith(":memory:"):
url = url.set(query={"cache": "shared"})
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this only affect :memory: mode, or do we want this setting anytime we are running SQLite?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is setting ourselves up for using two separate engines. Otherwise each engine will gets its own database, which is not shared.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually a legacy feature that's no longer applicable to file-base databases.

Comment on lines 37 to 39
@compiles(JSONB, "sqlite") # type: ignore
def _(*args: Any, **kwargs: Any) -> str:
return "JSONB"
Copy link
Contributor

Choose a reason for hiding this comment

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

Docstring would help here.

Comment on lines 257 to 272
@compiles(TextContains) # type: ignore
def _(element: Any, compiler: Any, **kw: Any) -> Any:
string, substring = list(element.clauses)
return compiler.process(string.contains(substring), **kw)


@compiles(TextContains, "postgresql") # type: ignore
def _(element: Any, compiler: Any, **kw: Any) -> Any:
string, substring = list(element.clauses)
return compiler.process(func.strpos(string, substring) > 0, **kw)


@compiles(TextContains, "sqlite") # type: ignore
def _(element: Any, compiler: Any, **kw: Any) -> Any:
string, substring = list(element.clauses)
return compiler.process(func.text_contains(string, substring) > 0, **kw)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll just be linking it to the docs. I'm not sure how to explain this without confusing ppl.

Comment on lines 24 to 43
class JSONB(JSON):
__visit_name__ = "JSONB"


@compiles(JSONB, "sqlite") # type: ignore
def _(*args: Any, **kwargs: Any) -> str:
return "JSONB"


JSON_ = (
JSON()
.with_variant(
postgresql.JSONB(), # type: ignore
"postgresql",
)
.with_variant(
JSONB(),
"sqlite",
)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if it matters, but we've got some duplication here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea, I wasn't sure if we want to create a dependency here, given that the migration may be run in the command line

@RogerHYang RogerHYang merged commit 3b202d7 into sql Apr 23, 2024
11 checks passed
@RogerHYang RogerHYang deleted the sqlean branch April 23, 2024 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants