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

Package import mismatch in generated file #2985

Closed
petrnymsa opened this issue May 3, 2024 · 2 comments
Closed

Package import mismatch in generated file #2985

petrnymsa opened this issue May 3, 2024 · 2 comments

Comments

@petrnymsa
Copy link

Describe the bug

When using version package (https://pub.dev/packages/version) and sqlite3 at the same time, there is clash with imports even when of the package is aliased.

dependencies:
  drift: ^2.17.0
  version: ^3.0.2
  sqlcipher_flutter_libs: ^0.5.4
  sqlite3: ^2.0.0
  path_provider: ^2.1.3

dev_dependencies:
  build_runner: ^2.4.9
  drift_dev: ^2.17.0

db.dart

import 'dart:io';

import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:sqlite3/sqlite3.dart' as sqlite;
import 'package:version/version.dart';

part 'db.drift.dart';
part 'user_table.dart';

@DriftDatabase(tables: [UserProfiles])
class LocalDatabase extends _$LocalDatabase {
  @override
  int get schemaVersion => 1;

  LocalDatabase({QueryExecutor? executor}) : super(executor ?? _openConnection());

  static bool _debugCheckHasCipher(sqlite.Database database) {
    return database.select('PRAGMA cipher_version;').isNotEmpty;
  }

  static LazyDatabase _openConnection() {
    return LazyDatabase(() async {
      final dbFolder = await path_provider.getApplicationDocumentsDirectory();
      final file = File(p.join(dbFolder.path, 'db.sqlite'));

      return NativeDatabase(
        file,
        setup: (rawDb) {
          assert(_debugCheckHasCipher(rawDb), 'check cipher');
          rawDb.execute("PRAGMA key = 'XXX';");
        },
        logStatements: true,
      );
    });
  }
}

UserTable

part of 'db.dart';

@DataClassName('UserProfileRow')
class UserProfiles extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get userName => text()();
  TextColumn get apiVersion => text().map(const _VersionConverter())();

  @override
  List<Set<Column<Object>>> get uniqueKeys => [
        {userName},
      ];

  const UserProfiles();
}

// Version is from `Version` package
class _VersionConverter extends TypeConverter<Version, String> {
  const _VersionConverter();

  @override
  Version fromSql(String fromDb) {
    return Version.parse(fromDb);
  }

  @override
  String toSql(Version value) {
    return value.toString();
  }
}

sqlite3 package is aliased.

Generated part

db.drift.dart

// in 	$UserProfilesTable  

// error: A value of type '_VersionConverter' can't be assigned to a variable of type 'TypeConverter<Version, String>'.
// Try changing the type of the variable, or casting the right-hand type to 'TypeConverter<Version, String>
  static TypeConverter<sqlite.Version, String> $converterapiVersion =
      const _VersionConverter();

Error:

A value of type '_VersionConverter' can't be assigned to a variable of type 'TypeConverter<Version, String>'.
Try changing the type of the variable, or casting the right-hand type to 'TypeConverter<Version, String>

notice that Version is prefixed with sqlite.

build.yaml configuration

targets:
  $default:
    auto_apply_builders: true
    builders:
      drift_dev:not_shared:
        enabled: true
      drift_dev:preparing_builder:
        enabled: true
      drift_dev:drift_dev:
        enabled: false

Dart version

Dart SDK version: 3.3.0 (stable) (Tue Feb 13 10:25:19 2024 +0000) on "windows_x64"

Expected behavior

Drift will not mismatch imported aliases Version (from package Version) and sqlite3.Version

@petrnymsa
Copy link
Author

Just note that on version 2.14 it is working fine.

@simolus3
Copy link
Owner

simolus3 commented May 3, 2024

Thanks for the report. Unfortunately there's always a little bit of guesswork involved when finding import aliases (since we only have the URI and a name available at that point in the generator, and the URI is not always reliable). I have improved this so that we're able to at least distinguish between different packages, the fix will be part of the next drift release.

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

No branches or pull requests

2 participants