Include preamble in schema code

Closes https://github.com/simolus3/drift/issues/3593
This commit is contained in:
Simon Binder
2025-06-25 20:59:38 -04:00
parent 7230de30be
commit 16b459e8bc
18 changed files with 46 additions and 11 deletions
+1 -1
View File
@@ -84,7 +84,7 @@ At the moment, drift supports these options:
can be enabled to speed up builds. This option has no effect with the default or the modular builder.
- `fatal_warnings`: When enabled (defaults to `false`), warnings found by `drift_dev` in the build process (like syntax errors in SQL queries or
unresolved references in your Dart tables) will cause the build to fail.
- `preamble`: This option is useful when using drift as a standalone part "../Generation options"builder or when running a
- `preamble`: This option is useful when using drift as a [standalone part](in_other_builders.md) builder or when running a
modular build. In these setups, the `preamble` option defined by the [source_gen package](https://pub.dev/packages/source_gen#preamble)
would have no effect, which is why it has been added as an option for the drift builders.
- `generate_manager`: When enabled (defaults to `true`), managers will be generated for each table in the database. These managers help perform simple actions without boilerplate.
+4
View File
@@ -1,3 +1,7 @@
## 2.27.1-dev
- Include preamble from drift build options in generated schema code.
## 2.27.0
- Fix generating versioned schema code for columns referencing other columns in
@@ -468,6 +468,7 @@ class _DriftBuildRun {
if (mode.isMonolithic) {
final generationOptions = GenerationOptions(
imports: ImportManagerForPartFiles(await buildStep.inputLibrary),
writePreamble: !mode.appliesCombiningBuilderFromSourceGen,
);
writer = Writer(options, generationOptions: generationOptions);
} else {
@@ -483,14 +484,6 @@ class _DriftBuildRun {
Future<void> _emitCode() {
final output = StringBuffer();
if (!mode.appliesCombiningBuilderFromSourceGen) {
final preamble = options.preamble;
if (preamble != null) {
output.writeln(preamble);
}
}
output.writeln('// ignore_for_file: type=lint');
if (mode == DriftGenerationMode.monolithicPart) {
@@ -4,7 +4,6 @@ import 'package:args/command_runner.dart';
import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart';
import '../../../analysis/options.dart';
import '../../../writer/import_manager.dart';
import '../../../writer/schema_version_writer.dart';
import '../../../writer/writer.dart';
@@ -58,7 +57,7 @@ class StepsGenerationUtil {
DriftDevCli cli, Map<int, ExportedSchema> schemas) async {
final imports = LibraryImportManager();
final writer = Writer(
const DriftOptions.defaults(),
cli.project.options,
generationOptions: GenerationOptions(imports: imports),
);
imports.linkToWriter(writer);
+13
View File
@@ -36,6 +36,11 @@ class Writer extends _NodeOrWriter {
_root = Scope(parent: null, writer: this);
_header = leaf();
_imports = leaf();
final preamble = options.preamble;
if (preamble != null && generationOptions.writePreamble) {
leaf().writeln(preamble);
}
}
/// Returns the code generated by this [Writer].
@@ -534,6 +539,13 @@ class GenerationOptions {
/// DDL statements.
final bool avoidUserCode;
/// Whether the [DriftOptions.preamble] should be written.
///
/// We disable this when drift is generating part files picked up by the
/// combining builder from `source_gen`, because users should set their
/// preamble there.
final bool writePreamble;
final ImportManager imports;
const GenerationOptions({
@@ -543,6 +555,7 @@ class GenerationOptions {
this.writeCompanions = true,
this.isModular = false,
this.avoidUserCode = false,
this.writePreamble = true,
});
/// Whether, instead of generating the full database code, we're only
+2
View File
@@ -13,3 +13,5 @@ targets:
dialect: sqlite
options:
modules: [fts5]
preamble: |
// coverage:ignore-file
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
import 'package:drift/internal/versioned_schema.dart' as i0;
import 'package:drift/drift.dart' as i1;
import 'package:drift/src/runtime/query_builder/query_builder.dart' as i2;
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
import 'package:drift/src/runtime/query_builder/query_builder.dart' as i2;
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';
@@ -1,4 +1,6 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
import 'package:drift/drift.dart';