drift icon indicating copy to clipboard operation
drift copied to clipboard

Schema dump errors

Open robpot95 opened this issue 6 months ago • 6 comments

When i run the command for schema dump: dart run drift_dev schema dump

I get this errors and no schema is generated. I suspect is due to my models are having flutter releated stuff and the command is a dart command?

../../../Development/flutter/packages/flutter/lib/src/material/app.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' as ui;
       ^
../../../Development/flutter/packages/flutter/lib/src/material/app_bar_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../Development/flutter/packages/flutter/lib/src/material/arc.dart:6:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../Development/flutter/packages/flutter/lib/src/material/badge_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../Development/flutter/packages/flutter/lib/src/material/banner_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../Development/flutter/packages/flutter/lib/src/material/bottom_app_bar_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../Development/flutter/packages/flutter/lib/src/material/bottom_navigation_bar_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../Development/flutter/packages/flutter/lib/src/material/bottom_sheet_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;

robpot95 avatar Jun 05 '25 10:06 robpot95

due to my models are having flutter releated stuff and the command is a dart command?

Yes, unfortunately that is the issue (the reason is that in some cases, it's hard to figure out the exact SQL statement drift would generate for your schema statically. So we generate code for your database into a simple CLI program and run that in the background to obtain a reference schema in some cases, but that doesn't work if there are Flutter imports).

There are a little more details on this here, maybe it's possible to rewrite your database code to not rely on Flutter types?

simolus3 avatar Jun 05 '25 19:06 simolus3

Hm tbh the dump errors are not clear which file it has issue to generate. I've been dumping schemas before even tho those files imported flutter releated stuff

robpot95 avatar Jun 09 '25 12:06 robpot95

Hm tbh the dump errors are not clear which file it has issue to generate

So to be clear, drift writes an intermediate Dart file which it then tries to run with dart run. The file is responsible for printing the CREATE statements related to your schema. This can make the exported schema files more accurate because it now contains the exact statements.

As the linked section mentions, you can use --export-schema-startup-code=schema_description.dart first and then run dart run schema_description.dart to reproduce the failure and inspect the code drift generates & why it references Flutter APIs.

I've been dumping schemas before even tho those files imported flutter releated stuff

Yeah, the functionality to run database code against a mocked database to infer CREATE TABLE statements is a recent addition - but drift will fall back to the old approach if that doesn't work.

simolus3 avatar Jun 09 '25 14:06 simolus3

Hm tbh the dump errors are not clear which file it has issue to generate

So to be clear, drift writes an intermediate Dart file which it then tries to run with dart run. The file is responsible for printing the CREATE statements related to your schema. This can make the exported schema files more accurate because it now contains the exact statements.

As the linked section mentions, you can use --export-schema-startup-code=schema_description.dart first and then run dart run schema_description.dart to reproduce the failure and inspect the code drift generates & why it references Flutter APIs.

I've been dumping schemas before even tho those files imported flutter releated stuff

Yeah, the functionality to run database code against a mocked database to infer CREATE TABLE statements is a recent addition - but drift will fall back to the old approach if that doesn't work.

Thank you for clarifying. When i try to run the command i get this error:

Building package executable... 
Built drift_dev:drift_dev.
Could not find an option named "--export-schema-startup-code".

robpot95 avatar Jun 10 '25 06:06 robpot95

Interesting, which drift_dev version are you using?

simolus3 avatar Jun 11 '25 18:06 simolus3

Interesting, which drift_dev version are you using?

I've not upgraded yet to latest flutter version due to issues with new versions and drift requires it newer version of flutter

drift_dev: ^2.23.1

robpot95 avatar Jun 11 '25 18:06 robpot95