-
Notifications
You must be signed in to change notification settings - Fork 403
PostgreSQL support #1507
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
PostgreSQL support #1507
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Can we run Postgres server in the CI? |
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.
Thanks a lot for this PR 🚀 I just had a quick look at this with some initial comments. I'll likely have more comments once I actually try this out :D
Can we run Postgres server in the CI?
Do you have experience with GitHub actions? I think you can edit the tasks in .github/workflows/main.yml
to create a task with a postgres server. If you need help with this I can take care of this as well :)
Postgres server working in CI, initial queries seems working, but the test queries timeout. I'll try to debug CI action locally. |
Postgres CI timeout solved! Problem was 'tearDown' is not runs syncroniouly and db got deadlock. I can't make it synchronous so I put the "deleteDatabase" func to the end of each test. |
We need a good ORM for postgres on dart) |
You're welcome! I think Drift is the best |
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.
Sorry for the slow update, and thanks again for all your work ❤️ I have left some comments, but most of them are fairly minor.
Co-authored-by: Simon Binder <[email protected]>
We have a small problem with 64bit integers. SQLite supports 64bit (In fact, integer size can't be set), therefore I use bigint/bigserial types in Postgres dialect. The problem is, Dart web can't handle integers this size (53bit only). It would be necessary to warn the user in the documentation. Other question is, what if Postgres user want to use 32bit (or 8bit) integers only (for reduce storage size)? May we can add an IntSize enum (default Int64) parameter into integer() column definition function. |
Keywords is necessary for escaping. If user use "user" as column name, Drift won't escape it in generated queries and Postgres queries will fail. If I see well the issue |
Oh, now I get it. I think we could either encourage postgres users to (temporarily) disable |
I am also on the side of maintaining backward compatibility as much as possible. We could add a Oh I see the main problem: Then, we can leave then '?' and ':' in Drift files. 'postgres_compatible' mode also enables |
/// If [name] is a reserved sql keyword, wraps it in double ticks. Otherwise
/// just returns the [name] directly.
String get escapedName {
return isKeywordLexeme(name) ? '"$name"' : name;
} Won't be easier to escape all names regardless of it is keyword or not? |
I started to make a compatible mode, but it requires to rewrite drift and drift_dev in many places. Must update all "escapeIfNeed" call. |
Okay. Current solution:
|
Hello, when i can try alpha version of postgres support? |
Sure! dependencies:
drift: any
drift_postgres: any
dev_dependencies:
drift_dev: any
dependency_overrides:
drift:
git:
url: https://github.com/westito/drift.git
ref: pg_support
path: drift
drift_postgres:
git:
url: https://github.com/westito/drift.git
ref: pg_support
path: extras/drift_postgres
drift_dev:
git:
url: https://github.com/westito/drift.git
ref: pg_support
path: drift_dev
sqlparser:
git:
url: https://github.com/westito/drift.git
ref: pg_support
path: sqlparser |
OK, thanks. I'll try tomorrow with current commit. dependency_overrides:
drift:
git:
url: https://github.com/westito/drift.git
ref: cffbd428629691790935693d94757f2bf2043473
path: drift
drift_postgres:
git:
url: https://github.com/westito/drift.git
ref: cffbd428629691790935693d94757f2bf2043473
path: extras/drift_postgres
drift_dev:
git:
url: https://github.com/westito/drift.git
ref: cffbd428629691790935693d94757f2bf2043473
path: drift_dev
sqlparser:
git:
url: https://github.com/westito/drift.git
ref: cffbd428629691790935693d94757f2bf2043473
path: sqlparser |
Almost forget! Add these builder options too: targets:
$default:
builders:
drift_dev:
options:
new_sql_code_generation: true
compatible_mode_generation: true |
@westito I like your idea with the build option for compatibility 👍 . Is this ready from your side now? If so I'll take a deeper look tomorrow. |
I used "compatible mode" naming (could be "generic_sql"), because dialect builder option fixes that dialect in generated code. For ex. in my project the same generated code used on mobile and on backend too. The generated code must be dialect-independent. |
I understand where you're coming from, but that will be a hard trade off to make in the long term. If we want excellent support for both sqlite3 and postgres in drift, we need to know the exact dialect at compile-time for the accurate analysis and code generation. In your setup (which sounds super awesome btw, I ❤️ Dart on the backend), I assume you have separate packages for your app and the backend, right? Because then you could introduce a shared package that stores the |
I thinking on a solution, but I have a separate [core] package that contains all the data handling (DAO, Repos, stc.) and mobile and backend depend on that. Mobile/web/backend only gives the connection (Pg, Worker or NativeDatabase). If I generate two different .g.dart files, I would need write a lot of interfaces to decide between. This does make no sense. However, SQLite understands Postgres queries (at least we currently use), so database generated for postgres will work for SQLite too. Also, this dialect builder option only affects Drift files and custom queries. If not use these, there is only runtime generated queries that is depends on connection dialect. |
Yeah I just had the same problem applying that to the integration tests :D
Yeah, just setting |
This looks good to me now 🎉 . Thanks a lot for all your effort here, I really appreciate it!! |
I'm glad I could help 😊🥳 |
@westito Awesome job! I will give it a go with a shelf server app. I was using some adhoc query helpers until now 😄 |
PostgresSQL experimental support
All tests pass! (except migration) 🎉
Missing features:
- Migrations, versioning (in progress)Modifications:
- SQL type names changed for Postgres (SQLite compatible)