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

Schema migration helpers: add generated imports #3396

Open
Romain-Guillot opened this issue Jan 2, 2025 · 1 comment
Open

Schema migration helpers: add generated imports #3396

Romain-Guillot opened this issue Jan 2, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@Romain-Guillot
Copy link

The generated file database.steps.dart does not include the necessary imports.

For example, consider the following table definition:

import 'enum/status.dart';

class TodoItems extends Table {
  IntColumn get id => integer().autoIncrement()();
  IntColumn get status => intEnum<Status>().withDefault(Constant(Status.draft.value))();
}

The generated file database.steps.dart references the Status enum due to the withDefault method.

i1.GeneratedColumn<int> _column_xxx(String aliasedName) => i1.GeneratedColumn<int>(
    'status', 
    aliasedName, 
    false, 
    type: i1.DriftSqlType.int, 
    defaultValue: Constant(Status.unrestricted.value));

However, the file does not include the required import for Status. The same issue arise for the generated test files.

As a result, developers must manually edit the generated file to include the missing import after each make-migrations command. Which is inconvenient.

Proposed Solutions:

  • Introduce an option in drift_dev to specify a list of imports to be included in the generated file.
  • Use a .g.dart convention (e.g., database.steps.g.dart) for the actual generated code. The main file (e.g., database.steps.dart) would include necessary imports and delegate functionality to the .g.dart file.
  • Automatically detect required imports (idk if it's possible)
@Romain-Guillot Romain-Guillot added the enhancement New feature or request label Jan 2, 2025
@simolus3
Copy link
Owner

simolus3 commented Jan 2, 2025

Could you retry this with the latest drift_dev version (2.23.0, which I've released just a few days ago)?

This used to be a longstanding problem, but that version fixes it by dynamically resolving the constant. When you export the schema, we run the drift generator in memory and use that to create an isolate that will collect all the CREATE TABLE statements that drift would generate. Then we replace the Dart expression (e.g. Status.draft) with the actual value (e.g. just 0).
It's kind of complicated, but we should be hiding that away from you and I'm convinced it's the only sound approach: Schema exports are supposed to represent an immutable snapshot of your schema, but if we keep the Dart expression with an import, you could change the definition of Status and then alter the meaning of the existing snapshots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants