|
|
@@ -1234,6 +1234,10 @@ class FilesAsData {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+const maxItemsToSelect = 200;
|
|
|
+const maxItemsToInsert = 500;
|
|
|
+const maxItemsToRemove = 500;
|
|
|
+
|
|
|
processCsvData(File tempfile, SupabaseClient supabase) async {
|
|
|
List<FilesAsData> csvData = [];
|
|
|
final filename = tempfile.path.split('/').last;
|
|
|
@@ -1263,11 +1267,18 @@ processCsvData(File tempfile, SupabaseClient supabase) async {
|
|
|
final scopeInNew = mapsToInsert
|
|
|
.fold(<String>{}, (t, e) => t..add(e[scopeName] ?? "")).toList();
|
|
|
|
|
|
- final old = await supabase
|
|
|
- .from(tables[data.filename]!)
|
|
|
- .select()
|
|
|
- .inFilter(scopeName, scopeInNew)
|
|
|
- .limit(100000);
|
|
|
+ print("Loading old data scope: ${scopeInNew.length}");
|
|
|
+ List<Map<String, dynamic>> old = [];
|
|
|
+ for (var e in splitList(scopeInNew, headerToNb(scopeInNew))) {
|
|
|
+ final res = await supabase
|
|
|
+ .from(tables[data.filename]!)
|
|
|
+ .select()
|
|
|
+ .inFilter(scopeName, e)
|
|
|
+ .limit(100000);
|
|
|
+ old.addAll(res);
|
|
|
+ print("Loaded old data scope: ${e.length}");
|
|
|
+ }
|
|
|
+
|
|
|
// Replace with your actual table name
|
|
|
final oldComparable = old
|
|
|
.map((e) => filterMapByKeys(e, headers[data.filename] ?? []))
|
|
|
@@ -1275,6 +1286,7 @@ processCsvData(File tempfile, SupabaseClient supabase) async {
|
|
|
|
|
|
List<int> indexToRemove = [];
|
|
|
List<int> indexToMaintain = [];
|
|
|
+ print("Deleting old data scope: ${mapsToInsert.length}");
|
|
|
for (int i = 0; i < oldComparable.length; i++) {
|
|
|
final item = oldComparable[i];
|
|
|
final index = findIndex(mapsToInsert, item);
|
|
|
@@ -1290,17 +1302,38 @@ processCsvData(File tempfile, SupabaseClient supabase) async {
|
|
|
" indexToRemove: ${indexToRemove.length} , indexToMaintain: ${indexToMaintain.length}");
|
|
|
print("delete from db");
|
|
|
|
|
|
- splitList(indexToRemove.map((e) => old[e]['id']).toList(), 50).forEach(
|
|
|
- (e) async => await supabase
|
|
|
- .from(
|
|
|
- tables[data.filename]!) // Replace with your actual table name
|
|
|
- .delete()
|
|
|
- .inFilter('id', e));
|
|
|
+ print("delete from db ${indexToRemove.length}");
|
|
|
+
|
|
|
+ for (var e in splitList(indexToRemove.map((e) => old[e]['id']).toList(),
|
|
|
+ headerToNb(indexToRemove.map((e) => old[e]['id']).toList()))) {
|
|
|
+ await supabase
|
|
|
+ .from(tables[data.filename]!) // Replace with your actual table name
|
|
|
+ .delete()
|
|
|
+ .inFilter('id', e);
|
|
|
+ print("Deleted old data scope: ${e.length}");
|
|
|
+ }
|
|
|
+
|
|
|
+ // splitList(indexToRemove.map((e) => old[e]['id']).toList(), 200).forEach(
|
|
|
+ // (e) async => await supabase
|
|
|
+ // .from(
|
|
|
+ // tables[data.filename]!) // Replace with your actual table name
|
|
|
+ // .delete()
|
|
|
+ // .inFilter('id', e));
|
|
|
print("insert in db");
|
|
|
|
|
|
- splitList(mapsToInsert, 50).forEach((e) async => await supabase
|
|
|
+ await supabase
|
|
|
.from(tables[data.filename]!) // Replace with your actual table name
|
|
|
- .insert(e));
|
|
|
+ .insert(mapsToInsert);
|
|
|
+ // print("insert in db ${mapsToInsert.length}");
|
|
|
+ // for (var e in splitList(mapsToInsert, headerToNb(mapsToInsert))) {
|
|
|
+ // await supabase
|
|
|
+ // .from(tables[data.filename]!) // Replace with your actual table name
|
|
|
+ // .insert(e);
|
|
|
+ // print("Inserted old data scope: ${e.length}");
|
|
|
+ // }
|
|
|
+ // splitList(mapsToInsert, 200).forEach((e) async => await supabase
|
|
|
+ // .from(tables[data.filename]!) // Replace with your actual table name
|
|
|
+ // .insert(e));
|
|
|
print("end");
|
|
|
|
|
|
// print(createListOfMaps(headers[data.filename] ?? [], csv2list(data.data)));
|
|
|
@@ -1357,3 +1390,16 @@ List<List<T>> splitList<T>(List<T> originalList, int maxSize) {
|
|
|
|
|
|
return sublists;
|
|
|
}
|
|
|
+
|
|
|
+int headerToNb(List list) {
|
|
|
+ //header max of 16k
|
|
|
+ final maxheader = 4 * 1024 * 8;
|
|
|
+ if (list.isEmpty) {
|
|
|
+ return list.length;
|
|
|
+ }
|
|
|
+ final length1 = (list.toString().length / list.length).ceil() * 8;
|
|
|
+ final lengthurl = 200 * 8;
|
|
|
+ final res = ((maxheader - lengthurl) / length1).floor();
|
|
|
+ print("header2nb: $res");
|
|
|
+ return res;
|
|
|
+}
|