runTableRegistry function
Runs the table registry process inside a Dart isolate.
This function is typically called automatically by the Flint CLI when
running database synchronization commands (e.g., flint db:sync).
It compares the current database schema with the defined table schemas
and produces a list of SQL statements to create or modify tables.
Parameters
tables(List<Table>): A list ofTableobjects representing the desired schema for each database table._(dynamic, optional): An unused positional parameter (reserved for isolate call compatibility).sendPort(SendPort?, optional): ASendPortused to send the computed schema differences back to the main isolate. Ifnull, the function will print an error and exit.
Behavior
- If a table does not exist in the database, its
CREATE TABLESQL is added to the diff list. - If a table exists, the
compareWithmethod is used to generate the necessaryALTER TABLEstatements. - All differences are sent back to the main isolate via
sendPort.
Example
// Inside table_registry.dart
runTableRegistry([
usersTable,
postsTable,
], null, sendPort);
Throws no exceptions directly — errors are logged and an empty list may be sent.
Used internally by the Flint Dart CLI.
Implementation
// ignore: no_wildcard_variable_uses
/// - [_] (`dynamic`, optional):
/// An unused positional parameter (reserved for isolate call compatibility).
/// - [sendPort] (`SendPort?`, optional):
/// A [`SendPort`](dart:isolate) used to send the computed schema differences
/// back to the main isolate. If `null`, the function will print an error
/// and exit.
///
/// ### Behavior
/// - If a table does not exist in the database, its `CREATE TABLE` SQL is added
/// to the diff list.
/// - If a table exists, the `compareWith` method is used to generate the
/// necessary `ALTER TABLE` statements.
/// - All differences are sent back to the main isolate via [sendPort].
///
/// ### Example
/// ```dart
/// // Inside table_registry.dart
/// runTableRegistry([
/// usersTable,
/// postsTable,
/// ], null, sendPort);
/// ```
///
/// Throws no exceptions directly — errors are logged and an empty list may be sent.
///
/// Used internally by the Flint Dart CLI.
///
void runTableRegistry(
List<Table> tables, [
dynamic _,
SendPort? sendPort,
]) async {
if (sendPort == null) {
print(
"❌ Error: runTableRegistry must be called via the Flint CLI isolate.");
Isolate.exit(); // ← Add this
// return;
}
final List<String> diffs = [];
try {
for (final table in tables) {
final existingTable = await getTableSchema(table.name);
if (existingTable == null) {
// Table doesn't exist → create it
diffs.add(table.toCreateSQL());
} else {
// Table exists → diff it
final diff = existingTable.compareWith(table);
if (diff != null) diffs.add(diff);
}
}
} catch (e, st) {
print("⚠️ Error in table registry: $e\n$st");
}
print(diffs);
// ✅ Always send result back, even if empty
sendPort.send(diffs);
// ✅ CRITICAL: Exit the isolate to stop the CLI process
Isolate.exit();
}