-
Notifications
You must be signed in to change notification settings - Fork 198
Replace raw SQL schema with SQLAlchemy Table definitions #237
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """SQLAlchemy table definitions for the Chainlit persistence layer. | ||
|
|
||
| Defines a single schema that emits dialect-appropriate DDL for both SQLite | ||
| and PostgreSQL via ``metadata.create_all(engine)``. | ||
|
|
||
| On PostgreSQL, columns use native ``UUID``, ``JSONB``, ``TEXT[]``, and | ||
| ``BOOLEAN`` types. On SQLite the same definitions fall back to ``TEXT`` | ||
| and ``INTEGER`` — no hand-written SQL required. | ||
| """ | ||
|
|
||
| from sqlalchemy import ( | ||
| Boolean, | ||
| Column, | ||
| ForeignKey, | ||
| Integer, | ||
| MetaData, | ||
| Table, | ||
| Text, | ||
| ) | ||
| from sqlalchemy.dialects.postgresql import ARRAY as PG_ARRAY | ||
| from sqlalchemy.dialects.postgresql import JSONB | ||
| from sqlalchemy.dialects.postgresql import UUID as PG_UUID | ||
|
|
||
| metadata = MetaData() | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Dialect-aware column types | ||
| # | ||
| # On PostgreSQL these resolve to native UUID / JSONB / TEXT[]. | ||
| # On SQLite they fall back to plain TEXT so Chainlit's raw-SQL data layer | ||
| # (which serialises everything to strings) keeps working unchanged. | ||
| # --------------------------------------------------------------------------- | ||
| _UUID = Text().with_variant(PG_UUID(as_uuid=False), "postgresql") | ||
| _JSON = Text().with_variant(JSONB(), "postgresql") | ||
| _TAGS = Text().with_variant(PG_ARRAY(Text), "postgresql") | ||
|
|
||
| users = Table( | ||
| "users", | ||
| metadata, | ||
| Column("id", _UUID, primary_key=True), | ||
| Column("identifier", Text, nullable=False, unique=True), | ||
| Column("metadata", _JSON, nullable=False), | ||
| Column("createdAt", Text), | ||
| ) | ||
|
michaelchu marked this conversation as resolved.
|
||
|
|
||
| threads = Table( | ||
| "threads", | ||
| metadata, | ||
| Column("id", _UUID, primary_key=True), | ||
| Column("createdAt", Text), | ||
| Column("name", Text), | ||
| Column("userId", _UUID, ForeignKey("users.id", ondelete="CASCADE")), | ||
| Column("userIdentifier", Text), | ||
| Column("tags", _TAGS), | ||
| Column("metadata", _JSON), | ||
| ) | ||
|
|
||
| steps = Table( | ||
| "steps", | ||
| metadata, | ||
| Column("id", _UUID, primary_key=True), | ||
| Column("name", Text, nullable=False), | ||
| Column("type", Text, nullable=False), | ||
| Column( | ||
| "threadId", | ||
| _UUID, | ||
| ForeignKey("threads.id", ondelete="CASCADE"), | ||
| nullable=False, | ||
| ), | ||
| Column("parentId", _UUID), | ||
| Column("streaming", Boolean, nullable=False), | ||
| Column("waitForAnswer", Boolean), | ||
| Column("isError", Boolean), | ||
|
michaelchu marked this conversation as resolved.
|
||
| Column("metadata", _JSON), | ||
| Column("tags", _TAGS), | ||
| Column("input", Text), | ||
| Column("output", Text), | ||
| Column("createdAt", Text), | ||
| Column("command", Text), | ||
| Column("start", Text), | ||
| Column("end", Text), | ||
| Column("generation", _JSON), | ||
| Column("showInput", Text), | ||
| Column("language", Text), | ||
| Column("indent", Integer), | ||
| Column("defaultOpen", Boolean), | ||
| ) | ||
|
|
||
| elements = Table( | ||
| "elements", | ||
| metadata, | ||
| Column("id", _UUID, primary_key=True), | ||
| Column("threadId", _UUID, ForeignKey("threads.id", ondelete="CASCADE")), | ||
| Column("type", Text), | ||
| Column("url", Text), | ||
| Column("chainlitKey", Text), | ||
| Column("name", Text, nullable=False), | ||
| Column("display", Text), | ||
| Column("objectKey", Text), | ||
| Column("size", Text), | ||
| Column("page", Integer), | ||
| Column("language", Text), | ||
| Column("forId", _UUID), | ||
| Column("mime", Text), | ||
| Column("props", _JSON), | ||
| ) | ||
|
|
||
| feedbacks = Table( | ||
| "feedbacks", | ||
| metadata, | ||
| Column("id", _UUID, primary_key=True), | ||
| Column("forId", _UUID, nullable=False), | ||
| Column("threadId", _UUID, ForeignKey("threads.id", ondelete="CASCADE")), | ||
| Column("value", Integer, nullable=False), | ||
|
michaelchu marked this conversation as resolved.
|
||
| Column("comment", Text), | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.