mirror of https://github.com/AMT-Cheif/drift.git
Move `SchemaInstantiationHelper` to drift package
This commit is contained in:
parent
dec5769dcc
commit
add680b8bf
|
@ -0,0 +1,41 @@
|
|||
/// Contains tools for [verifying migrations] that don't depend on `sqlparser`
|
||||
/// and can thus be part of the core drift package.
|
||||
///
|
||||
/// [verifying migrations](https://drift.simonbinder.eu/docs/advanced-features/migrations/#verifying-migrations)
|
||||
library drift.internal.migrations;
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
/// A class that can create a [GeneratedDatabase] suitable for instantating an
|
||||
/// older version of your app's database.
|
||||
///
|
||||
/// The implementation of this class is generated through the `drift_dev`
|
||||
/// CLI tool.
|
||||
/// Typically, you don't use this class directly but rather through the
|
||||
/// `SchemaVerifier` class part of `package:drift_dev/api/migrations.dart`
|
||||
/// library.
|
||||
abstract class SchemaInstantiationHelper {
|
||||
/// Creates a database with the state of an old schema [version] and using the
|
||||
/// given underlying [db] connection.
|
||||
GeneratedDatabase databaseForVersion(QueryExecutor db, int version);
|
||||
}
|
||||
|
||||
/// Thrown by [SchemaInstantiationHelper.databaseForVersion] when trying to
|
||||
/// instantiate a schema that hasn't been saved.
|
||||
class MissingSchemaException implements Exception {
|
||||
/// The requested version that doesn't exist.
|
||||
final int requested;
|
||||
|
||||
/// All known schema versions.
|
||||
final Iterable<int> available;
|
||||
|
||||
/// A missing schema exception to be thrown when a requested schema snapshot
|
||||
/// is not available.
|
||||
const MissingSchemaException(this.requested, this.available);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Unknown schema version $requested. '
|
||||
'Known are ${available.join(', ')}.';
|
||||
}
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/internal/migrations.dart';
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:drift_dev/src/services/schema/verifier_impl.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:sqlite3/sqlite3.dart';
|
||||
|
||||
export 'package:drift/internal/migrations.dart';
|
||||
|
||||
abstract class SchemaVerifier {
|
||||
factory SchemaVerifier(SchemaInstantiationHelper helper) =
|
||||
VerifierImplementation;
|
||||
|
@ -117,29 +120,6 @@ class _GenerateFromScratch extends GeneratedDatabase {
|
|||
int get schemaVersion => 1;
|
||||
}
|
||||
|
||||
/// The implementation of this class is generated through the `drift_dev`
|
||||
/// CLI tool.
|
||||
abstract class SchemaInstantiationHelper {
|
||||
GeneratedDatabase databaseForVersion(QueryExecutor db, int version);
|
||||
}
|
||||
|
||||
/// Thrown when trying to instantiate a schema that hasn't been saved.
|
||||
class MissingSchemaException implements Exception {
|
||||
/// The requested version that doesn't exist.
|
||||
final int requested;
|
||||
|
||||
/// All known schema versions.
|
||||
final Iterable<int> available;
|
||||
|
||||
MissingSchemaException(this.requested, this.available);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Unknown schema version $requested. '
|
||||
'Known are ${available.join(', ')}.';
|
||||
}
|
||||
}
|
||||
|
||||
/// Thrown when the actual schema differs from the expected schema.
|
||||
class SchemaMismatch implements Exception {
|
||||
final String explanation;
|
||||
|
|
|
@ -152,7 +152,7 @@ class GenerateUtilsCommand extends Command {
|
|||
..writeln(_prefix)
|
||||
..writeln('//@dart=2.12')
|
||||
..writeln("import 'package:drift/drift.dart';")
|
||||
..writeln("import 'package:drift_dev/api/migrations.dart';");
|
||||
..writeln("import 'package:drift/internal/migrations.dart';");
|
||||
|
||||
for (final version in versions) {
|
||||
buffer.writeln("import '${_filenameForVersion(version)}' as v$version;");
|
||||
|
|
|
@ -1,38 +1,38 @@
|
|||
// GENERATED CODE, DO NOT EDIT BY HAND.
|
||||
//@dart=2.12
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift_dev/api/migrations.dart';
|
||||
import 'schema_v6.dart' as v6;
|
||||
import 'schema_v2.dart' as v2;
|
||||
import 'schema_v7.dart' as v7;
|
||||
import 'schema_v3.dart' as v3;
|
||||
import 'schema_v4.dart' as v4;
|
||||
import 'schema_v1.dart' as v1;
|
||||
import 'package:drift/internal/migrations.dart';
|
||||
import 'schema_v8.dart' as v8;
|
||||
import 'schema_v1.dart' as v1;
|
||||
import 'schema_v2.dart' as v2;
|
||||
import 'schema_v6.dart' as v6;
|
||||
import 'schema_v7.dart' as v7;
|
||||
import 'schema_v4.dart' as v4;
|
||||
import 'schema_v5.dart' as v5;
|
||||
import 'schema_v3.dart' as v3;
|
||||
|
||||
class GeneratedHelper implements SchemaInstantiationHelper {
|
||||
@override
|
||||
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
|
||||
switch (version) {
|
||||
case 6:
|
||||
return v6.DatabaseAtV6(db);
|
||||
case 2:
|
||||
return v2.DatabaseAtV2(db);
|
||||
case 7:
|
||||
return v7.DatabaseAtV7(db);
|
||||
case 3:
|
||||
return v3.DatabaseAtV3(db);
|
||||
case 4:
|
||||
return v4.DatabaseAtV4(db);
|
||||
case 1:
|
||||
return v1.DatabaseAtV1(db);
|
||||
case 8:
|
||||
return v8.DatabaseAtV8(db);
|
||||
case 1:
|
||||
return v1.DatabaseAtV1(db);
|
||||
case 2:
|
||||
return v2.DatabaseAtV2(db);
|
||||
case 6:
|
||||
return v6.DatabaseAtV6(db);
|
||||
case 7:
|
||||
return v7.DatabaseAtV7(db);
|
||||
case 4:
|
||||
return v4.DatabaseAtV4(db);
|
||||
case 5:
|
||||
return v5.DatabaseAtV5(db);
|
||||
case 3:
|
||||
return v3.DatabaseAtV3(db);
|
||||
default:
|
||||
throw MissingSchemaException(version, const {6, 2, 7, 3, 4, 1, 8, 5});
|
||||
throw MissingSchemaException(version, const {8, 1, 2, 6, 7, 4, 5, 3});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,38 +1,38 @@
|
|||
// GENERATED CODE, DO NOT EDIT BY HAND.
|
||||
//@dart=2.12
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift_dev/api/migrations.dart';
|
||||
import 'schema_v6.dart' as v6;
|
||||
import 'schema_v2.dart' as v2;
|
||||
import 'schema_v7.dart' as v7;
|
||||
import 'schema_v3.dart' as v3;
|
||||
import 'schema_v4.dart' as v4;
|
||||
import 'schema_v1.dart' as v1;
|
||||
import 'package:drift/internal/migrations.dart';
|
||||
import 'schema_v8.dart' as v8;
|
||||
import 'schema_v1.dart' as v1;
|
||||
import 'schema_v2.dart' as v2;
|
||||
import 'schema_v6.dart' as v6;
|
||||
import 'schema_v7.dart' as v7;
|
||||
import 'schema_v4.dart' as v4;
|
||||
import 'schema_v5.dart' as v5;
|
||||
import 'schema_v3.dart' as v3;
|
||||
|
||||
class GeneratedHelper implements SchemaInstantiationHelper {
|
||||
@override
|
||||
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
|
||||
switch (version) {
|
||||
case 6:
|
||||
return v6.DatabaseAtV6(db);
|
||||
case 2:
|
||||
return v2.DatabaseAtV2(db);
|
||||
case 7:
|
||||
return v7.DatabaseAtV7(db);
|
||||
case 3:
|
||||
return v3.DatabaseAtV3(db);
|
||||
case 4:
|
||||
return v4.DatabaseAtV4(db);
|
||||
case 1:
|
||||
return v1.DatabaseAtV1(db);
|
||||
case 8:
|
||||
return v8.DatabaseAtV8(db);
|
||||
case 1:
|
||||
return v1.DatabaseAtV1(db);
|
||||
case 2:
|
||||
return v2.DatabaseAtV2(db);
|
||||
case 6:
|
||||
return v6.DatabaseAtV6(db);
|
||||
case 7:
|
||||
return v7.DatabaseAtV7(db);
|
||||
case 4:
|
||||
return v4.DatabaseAtV4(db);
|
||||
case 5:
|
||||
return v5.DatabaseAtV5(db);
|
||||
case 3:
|
||||
return v3.DatabaseAtV3(db);
|
||||
default:
|
||||
throw MissingSchemaException(version, const {6, 2, 7, 3, 4, 1, 8, 5});
|
||||
throw MissingSchemaException(version, const {8, 1, 2, 6, 7, 4, 5, 3});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue