From a029e6232f3b6f04fe93cf3fcd9ba7593b3bb6a4 Mon Sep 17 00:00:00 2001 From: Tim Shedor Date: Thu, 19 Sep 2024 23:40:50 -0700 Subject: [PATCH] rebase fixes --- example_supabase/.gitignore | 49 ++++++++++++++ example_supabase/.metadata | 45 +++++++++++++ .../brick/adapters/customer_adapter.g.dart | 67 +------------------ .../lib/brick/adapters/pizza_adapter.g.dart | 27 +++++++- example_supabase/lib/brick/brick.g.dart | 2 +- ...ion.dart => 20240920063917.migration.dart} | 50 +++++--------- example_supabase/lib/brick/db/schema.g.dart | 29 +++----- .../lib/brick/models/customer.model.dart | 4 -- .../lib/brick/models/pizza.model.dart | 2 + example_supabase/lib/main.dart | 34 ++++------ 10 files changed, 162 insertions(+), 147 deletions(-) create mode 100644 example_supabase/.gitignore create mode 100644 example_supabase/.metadata rename example_supabase/lib/brick/db/{20240906052847.migration.dart => 20240920063917.migration.dart} (53%) diff --git a/example_supabase/.gitignore b/example_supabase/.gitignore new file mode 100644 index 00000000..43b6bf66 --- /dev/null +++ b/example_supabase/.gitignore @@ -0,0 +1,49 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +**/ios/Flutter/.last_build_id +.dart_tool/ +.flutterplugins +.flutterpluginsdependencies +.pubcache/ +.pub/ +/build/ + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json + +# Android Studio will place build artifacts here +/android/app/debug +/android/app/profile +/android/app/release +windows/* +web/* +macos/* +linux/* +android/* +ios/* diff --git a/example_supabase/.metadata b/example_supabase/.metadata new file mode 100644 index 00000000..ac711aae --- /dev/null +++ b/example_supabase/.metadata @@ -0,0 +1,45 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "2663184aa79047d0a33a14a3b607954f8fdd8730" + channel: "stable" + +project_type: app + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + - platform: android + create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + - platform: ios + create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + - platform: linux + create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + - platform: macos + create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + - platform: web + create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + - platform: windows + create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730 + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - "lib/main.dart" + - "ios/Runner.xcodeproj/project.pbxproj" diff --git a/example_supabase/lib/brick/adapters/customer_adapter.g.dart b/example_supabase/lib/brick/adapters/customer_adapter.g.dart index 781964f1..04dbecd0 100644 --- a/example_supabase/lib/brick/adapters/customer_adapter.g.dart +++ b/example_supabase/lib/brick/adapters/customer_adapter.g.dart @@ -6,25 +6,12 @@ Future _$CustomerFromSupabase(Map data, return Customer( id: data['id'] as String, firstName: data['first_name'] as String?, - lastName: data['last_name'] as String?, - pizzas: await Future.wait(data['pizzas'] - ?.map( - (d) => PizzaAdapter().fromSupabase(d, provider: provider, repository: repository)) - .toList() - .cast>() ?? - [])); + lastName: data['last_name'] as String?); } Future> _$CustomerToSupabase(Customer instance, {required SupabaseProvider provider, OfflineFirstWithSupabaseRepository? repository}) async { - return { - 'id': instance.id, - 'first_name': instance.firstName, - 'last_name': instance.lastName, - 'pizzas': await Future.wait>(instance.pizzas - .map((s) => PizzaAdapter().toSupabase(s, provider: provider, repository: repository)) - .toList()) - }; + return {'id': instance.id, 'first_name': instance.firstName, 'last_name': instance.lastName}; } Future _$CustomerFromSqlite(Map data, @@ -32,19 +19,7 @@ Future _$CustomerFromSqlite(Map data, return Customer( id: data['id'] as String, firstName: data['first_name'] == null ? null : data['first_name'] as String?, - lastName: data['last_name'] == null ? null : data['last_name'] as String?, - pizzas: (await provider.rawQuery( - 'SELECT DISTINCT `f_Pizza_brick_id` FROM `_brick_Customer_pizzas` WHERE l_Customer_brick_id = ?', - [data['_brick_id'] as int]).then((results) { - final ids = results.map((r) => r['f_Pizza_brick_id']); - return Future.wait(ids.map((primaryKey) => repository! - .getAssociation( - Query.where('primaryKey', primaryKey, limit1: true), - ) - .then((r) => r!.first))); - })) - .toList() - .cast()) + lastName: data['last_name'] == null ? null : data['last_name'] as String?) ..primaryKey = data['_brick_id'] as int; } @@ -74,12 +49,6 @@ class CustomerAdapter extends OfflineFirstWithSupabaseAdapter { 'lastName': const RuntimeSupabaseColumnDefinition( association: false, columnName: 'last_name', - ), - 'pizzas': const RuntimeSupabaseColumnDefinition( - association: true, - columnName: 'pizzas', - associationType: Pizza, - associationIsNullable: false, ) }; @override @@ -111,12 +80,6 @@ class CustomerAdapter extends OfflineFirstWithSupabaseAdapter { columnName: 'last_name', iterable: false, type: String, - ), - 'pizzas': const RuntimeSqliteColumnDefinition( - association: true, - columnName: 'pizzas', - iterable: true, - type: Pizza, ) }; @override @@ -134,30 +97,6 @@ class CustomerAdapter extends OfflineFirstWithSupabaseAdapter { @override final String tableName = 'Customer'; - @override - Future afterSave(instance, {required provider, repository}) async { - if (instance.primaryKey != null) { - final pizzasOldColumns = await provider.rawQuery( - 'SELECT `f_Pizza_brick_id` FROM `_brick_Customer_pizzas` WHERE `l_Customer_brick_id` = ?', - [instance.primaryKey]); - final pizzasOldIds = pizzasOldColumns.map((a) => a['f_Pizza_brick_id']); - final pizzasNewIds = instance.pizzas.map((s) => s.primaryKey).whereType(); - final pizzasIdsToDelete = pizzasOldIds.where((id) => !pizzasNewIds.contains(id)); - - await Future.wait(pizzasIdsToDelete.map((id) async { - return await provider.rawExecute( - 'DELETE FROM `_brick_Customer_pizzas` WHERE `l_Customer_brick_id` = ? AND `f_Pizza_brick_id` = ?', - [instance.primaryKey, id]).catchError((e) => null); - })); - - await Future.wait(instance.pizzas.map((s) async { - final id = s.primaryKey ?? await provider.upsert(s, repository: repository); - return await provider.rawInsert( - 'INSERT OR IGNORE INTO `_brick_Customer_pizzas` (`l_Customer_brick_id`, `f_Pizza_brick_id`) VALUES (?, ?)', - [instance.primaryKey, id]); - })); - } - } @override Future fromSupabase(Map input, diff --git a/example_supabase/lib/brick/adapters/pizza_adapter.g.dart b/example_supabase/lib/brick/adapters/pizza_adapter.g.dart index 9add5e2a..9f10071e 100644 --- a/example_supabase/lib/brick/adapters/pizza_adapter.g.dart +++ b/example_supabase/lib/brick/adapters/pizza_adapter.g.dart @@ -3,7 +3,11 @@ part of '../brick.g.dart'; Future _$PizzaFromSupabase(Map data, {required SupabaseProvider provider, OfflineFirstWithSupabaseRepository? repository}) async { - return Pizza(id: data['id'] as String, frozen: data['frozen'] as bool); + return Pizza( + id: data['id'] as String, + frozen: data['frozen'] as bool, + customer: await CustomerAdapter() + .fromSupabase(data['customer'], provider: provider, repository: repository)); } Future> _$PizzaToSupabase(Pizza instance, @@ -19,13 +23,24 @@ Future> _$PizzaToSupabase(Pizza instance, Future _$PizzaFromSqlite(Map data, {required SqliteProvider provider, OfflineFirstWithSupabaseRepository? repository}) async { - return Pizza(id: data['id'] as String, frozen: data['frozen'] == 1) + return Pizza( + id: data['id'] as String, + frozen: data['frozen'] == 1, + customer: (await repository!.getAssociation( + Query.where('primaryKey', data['customer_Customer_brick_id'] as int, limit1: true), + ))! + .first) ..primaryKey = data['_brick_id'] as int; } Future> _$PizzaToSqlite(Pizza instance, {required SqliteProvider provider, OfflineFirstWithSupabaseRepository? repository}) async { - return {'id': instance.id, 'frozen': instance.frozen ? 1 : 0}; + return { + 'id': instance.id, + 'frozen': instance.frozen ? 1 : 0, + 'customer_Customer_brick_id': instance.customer.primaryKey ?? + await provider.upsert(instance.customer, repository: repository) + }; } /// Construct a [Pizza] @@ -81,6 +96,12 @@ class PizzaAdapter extends OfflineFirstWithSupabaseAdapter { columnName: 'frozen', iterable: false, type: bool, + ), + 'customer': const RuntimeSqliteColumnDefinition( + association: true, + columnName: 'customer_Customer_brick_id', + iterable: false, + type: Customer, ) }; @override diff --git a/example_supabase/lib/brick/brick.g.dart b/example_supabase/lib/brick/brick.g.dart index 6d5e2496..ea3f9f6b 100644 --- a/example_supabase/lib/brick/brick.g.dart +++ b/example_supabase/lib/brick/brick.g.dart @@ -9,7 +9,7 @@ import 'package:brick_sqlite/brick_sqlite.dart'; // ignore: unused_import, unused_shown_name, unnecessary_import import 'package:brick_supabase/brick_supabase.dart'; // ignore: unused_import, unused_shown_name, unnecessary_import -import 'package:pizza_shoppe/brick/models/pizza.model.dart'; // GENERATED CODE DO NOT EDIT +import 'package:pizza_shoppe/brick/models/customer.model.dart'; // GENERATED CODE DO NOT EDIT // ignore: unused_import import 'dart:convert'; import 'package:brick_sqlite/brick_sqlite.dart' diff --git a/example_supabase/lib/brick/db/20240906052847.migration.dart b/example_supabase/lib/brick/db/20240920063917.migration.dart similarity index 53% rename from example_supabase/lib/brick/db/20240906052847.migration.dart rename to example_supabase/lib/brick/db/20240920063917.migration.dart index 8bf14cfa..6e2aef80 100644 --- a/example_supabase/lib/brick/db/20240906052847.migration.dart +++ b/example_supabase/lib/brick/db/20240920063917.migration.dart @@ -9,48 +9,32 @@ part of 'schema.g.dart'; // The migration version must **always** mirror the file name -const List _migration_20240906052847_up = [ - InsertTable('_brick_Customer_pizzas'), +const List _migration_20240920063917_up = [ InsertTable('Customer'), InsertTable('Pizza'), - InsertForeignKey( - '_brick_Customer_pizzas', - 'Customer', - foreignKeyColumn: 'l_Customer_brick_id', - onDeleteCascade: true, - onDeleteSetDefault: false, - ), - InsertForeignKey( - '_brick_Customer_pizzas', - 'Pizza', - foreignKeyColumn: 'f_Pizza_brick_id', - onDeleteCascade: true, - onDeleteSetDefault: false, - ), InsertColumn('id', Column.varchar, onTable: 'Customer', unique: true), InsertColumn('first_name', Column.varchar, onTable: 'Customer'), InsertColumn('last_name', Column.varchar, onTable: 'Customer'), InsertColumn('id', Column.varchar, onTable: 'Pizza', unique: true), InsertColumn('frozen', Column.boolean, onTable: 'Pizza'), - CreateIndex( - columns: ['l_Customer_brick_id', 'f_Pizza_brick_id'], - onTable: '_brick_Customer_pizzas', - unique: true, + InsertForeignKey( + 'Pizza', + 'Customer', + foreignKeyColumn: 'customer_Customer_brick_id', + onDeleteCascade: false, + onDeleteSetDefault: false, ), ]; -const List _migration_20240906052847_down = [ - DropTable('_brick_Customer_pizzas'), +const List _migration_20240920063917_down = [ DropTable('Customer'), DropTable('Pizza'), - DropColumn('l_Customer_brick_id', onTable: '_brick_Customer_pizzas'), - DropColumn('f_Pizza_brick_id', onTable: '_brick_Customer_pizzas'), DropColumn('id', onTable: 'Customer'), DropColumn('first_name', onTable: 'Customer'), DropColumn('last_name', onTable: 'Customer'), DropColumn('id', onTable: 'Pizza'), DropColumn('frozen', onTable: 'Pizza'), - DropIndex('index__brick_Customer_pizzas_on_l_Customer_brick_id_f_Pizza_brick_id'), + DropColumn('customer_Customer_brick_id', onTable: 'Pizza'), ]; // @@ -58,15 +42,15 @@ const List _migration_20240906052847_down = [ // @Migratable( - version: '20240906052847', - up: _migration_20240906052847_up, - down: _migration_20240906052847_down, + version: '20240920063917', + up: _migration_20240920063917_up, + down: _migration_20240920063917_down, ) -class Migration20240906052847 extends Migration { - const Migration20240906052847() +class Migration20240920063917 extends Migration { + const Migration20240920063917() : super( - version: 20240906052847, - up: _migration_20240906052847_up, - down: _migration_20240906052847_down, + version: 20240920063917, + up: _migration_20240920063917_up, + down: _migration_20240920063917_down, ); } diff --git a/example_supabase/lib/brick/db/schema.g.dart b/example_supabase/lib/brick/db/schema.g.dart index 7750451f..4e51a98f 100644 --- a/example_supabase/lib/brick/db/schema.g.dart +++ b/example_supabase/lib/brick/db/schema.g.dart @@ -1,29 +1,13 @@ // GENERATED CODE DO NOT EDIT // This file should be version controlled import 'package:brick_sqlite/db.dart'; -part '20240906052847.migration.dart'; +part '20240920063917.migration.dart'; /// All intelligently-generated migrations from all `@Migratable` classes on disk -final migrations = {const Migration20240920034504()}; +final migrations = {const Migration20240920063917()}; /// A consumable database structure including the latest generated migration. -final schema = Schema(20240906052847, generatorVersion: 1, tables: { - SchemaTable('_brick_Customer_pizzas', columns: { - SchemaColumn('_brick_id', Column.integer, - autoincrement: true, nullable: false, isPrimaryKey: true), - SchemaColumn('l_Customer_brick_id', Column.integer, - isForeignKey: true, - foreignTableName: 'Customer', - onDeleteCascade: true, - onDeleteSetDefault: false), - SchemaColumn('f_Pizza_brick_id', Column.integer, - isForeignKey: true, - foreignTableName: 'Pizza', - onDeleteCascade: true, - onDeleteSetDefault: false) - }, indices: { - SchemaIndex(columns: ['l_Customer_brick_id', 'f_Pizza_brick_id'], unique: true) - }), +final schema = Schema(20240920063917, generatorVersion: 1, tables: { SchemaTable('Customer', columns: { SchemaColumn('_brick_id', Column.integer, autoincrement: true, nullable: false, isPrimaryKey: true), @@ -35,6 +19,11 @@ final schema = Schema(20240906052847, generatorVersion: 1, tables: SchemaColumn('_brick_id', Column.integer, autoincrement: true, nullable: false, isPrimaryKey: true), SchemaColumn('id', Column.varchar, unique: true), - SchemaColumn('frozen', Column.boolean) + SchemaColumn('frozen', Column.boolean), + SchemaColumn('customer_Customer_brick_id', Column.integer, + isForeignKey: true, + foreignTableName: 'Customer', + onDeleteCascade: false, + onDeleteSetDefault: false) }, indices: {}) }); diff --git a/example_supabase/lib/brick/models/customer.model.dart b/example_supabase/lib/brick/models/customer.model.dart index 0ae0cc8e..aa659e7d 100644 --- a/example_supabase/lib/brick/models/customer.model.dart +++ b/example_supabase/lib/brick/models/customer.model.dart @@ -1,7 +1,6 @@ import 'package:brick_offline_first_with_supabase/brick_offline_first_with_supabase.dart'; import 'package:brick_sqlite/brick_sqlite.dart'; import 'package:brick_supabase/brick_supabase.dart'; -import 'package:pizza_shoppe/brick/models/pizza.model.dart'; @ConnectOfflineFirstWithSupabase( supabaseConfig: SupabaseSerializable(), @@ -14,12 +13,9 @@ class Customer extends OfflineFirstWithSupabaseModel { final String? lastName; - final List pizzas; - Customer({ required this.id, this.firstName, this.lastName, - required this.pizzas, }); } diff --git a/example_supabase/lib/brick/models/pizza.model.dart b/example_supabase/lib/brick/models/pizza.model.dart index 6472eb2a..4bae5382 100644 --- a/example_supabase/lib/brick/models/pizza.model.dart +++ b/example_supabase/lib/brick/models/pizza.model.dart @@ -1,6 +1,7 @@ import 'package:brick_offline_first_with_supabase/brick_offline_first_with_supabase.dart'; import 'package:brick_sqlite/brick_sqlite.dart'; import 'package:brick_supabase/brick_supabase.dart'; +import 'package:pizza_shoppe/brick/models/customer.model.dart'; @ConnectOfflineFirstWithSupabase( supabaseConfig: SupabaseSerializable(), @@ -24,6 +25,7 @@ class Pizza extends OfflineFirstWithSupabaseModel { Pizza({ required this.id, required this.frozen, + required this.customer, }); } diff --git a/example_supabase/lib/main.dart b/example_supabase/lib/main.dart index 9fac2160..5083a00a 100644 --- a/example_supabase/lib/main.dart +++ b/example_supabase/lib/main.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:pizza_shoppe/brick/models/customer.model.dart'; +import 'package:pizza_shoppe/brick/models/pizza.model.dart'; import 'package:pizza_shoppe/brick/repository.dart'; const supabaseUrl = 'YOUR_SUPABASE_URL'; @@ -44,14 +44,14 @@ class MyHomePage extends StatelessWidget { body: Container( padding: const EdgeInsets.all(20.0), child: FutureBuilder( - future: Repository().get(), - builder: (context, AsyncSnapshot> customerList) { - final customers = customerList.data; + future: Repository().get(), + builder: (context, AsyncSnapshot> pizzaList) { + final pizzas = pizzaList.data; return ListView.builder( - itemCount: customers?.length ?? 0, + itemCount: pizzas?.length ?? 0, itemBuilder: (ctx, index) => - customers?[index] == null ? Container() : CustomerTile(customers![index]), + pizzas?[index] == null ? Container() : PizzaTile(pizzas![index]), ); }, ), @@ -60,29 +60,19 @@ class MyHomePage extends StatelessWidget { } } -class CustomerTile extends StatelessWidget { - final Customer customer; +class PizzaTile extends StatelessWidget { + final Pizza pizza; - CustomerTile(this.customer); + PizzaTile(this.pizza); @override Widget build(BuildContext context) { return Column( mainAxisAlignment: MainAxisAlignment.start, children: [ - Text('id: ${customer.id}'), - Text('name: ${customer.firstName} ${customer.lastName}'), - Text('pizzas:'), - if (customer.pizzas.isNotEmpty) - Padding( - padding: const EdgeInsets.only(left: 20.0), - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - for (var pizza in customer.pizzas) Text('id: ${pizza.id}\nfrozen: ${pizza.frozen}'), - ], - ), - ), + Text('id: ${pizza.id}'), + Text('frozen: ${pizza.frozen}'), + Text('name: ${pizza.customer.firstName} ${pizza.customer.lastName}'), ], ); }