Dynamic database schema based on user modules #3286
-
Hi, I am working on an internship project for a company that wants to implement a modular flutter application with an offline-first functionality. Therefore, I decided to use the Drift library to help implement the application's persistence layer as soon as possible and help handle the relations between modules. Although I've read the entire documentation, I'm still new to Drift so please excuse my possible misunderstandings. :D I was trying to leverage Migrations to update the database schema and create new tables without defining them into @DriftDatabase annotations from different modules, but I haven't succeeded. Is it possible to update the initial database schema with new tables and relations at runtime using Drift Migrations? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
So my first question here is: Why? Drift (at runtime) can support flexible tables, but the generated code obviously assumes a static schema. If dynamic schema only refers to enabling different tables (as a subset of a known set of available tables) depending on modules, that's easily possible (add an If you want to define tables in different Dart packages to keep the architecture of your app modular, you can! Drift supports module code generation for this, which you can enable on all packages to generate code per-module. You can still use a central app database that references tables from other modules, which drift will then import and use in generated code. Does that go into the direction you were looking for? |
Beta Was this translation helpful? Give feedback.
Hi Simon! Thank you so much for your reply!
I completely agree with your point for creating empty tables this will be the most straightforward approach.
However, the only reason why I was thinking to implement a dynamic table inclusion by using migration is because over time the actual Database class that creates the connection and the schema from the all possible modules will become quite cluttered with imports for DAOs and Tables. Therefore, I approached the problem by generating the code for each module by enabling the modular build from build.yaml. Afterwards, I wanted to just get the TableInfo that is generated by a module and migrate it into the initialised database.
And of course I…