fix: Use ON DELETE CASCADE for the anime watcher join table
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:anitrack/src/service/migrations/0000_score.dart';
|
|||||||
import 'package:anitrack/src/service/migrations/0001_anime_watcher.dart';
|
import 'package:anitrack/src/service/migrations/0001_anime_watcher.dart';
|
||||||
import 'package:anitrack/src/service/migrations/0002_anilist.dart';
|
import 'package:anitrack/src/service/migrations/0002_anilist.dart';
|
||||||
import 'package:anitrack/src/service/migrations/0003_other_titles.dart';
|
import 'package:anitrack/src/service/migrations/0003_other_titles.dart';
|
||||||
|
import 'package:anitrack/src/service/migrations/0004_fix_constraints.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||||
@@ -73,8 +74,8 @@ Future<void> _createDatabase(Database db, int version) async {
|
|||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
anime TEXT NOT NULL,
|
anime TEXT NOT NULL,
|
||||||
CONSTRAINT pk_watcher_join_table PRIMARY KEY(name, anime),
|
CONSTRAINT pk_watcher_join_table PRIMARY KEY(name, anime),
|
||||||
CONSTRAINT fk_watcher FOREIGN KEY (name) REFERENCES $animeWatcherTable(name),
|
CONSTRAINT fk_watcher FOREIGN KEY (name) REFERENCES $animeWatcherTable(name) ON DELETE CASCADE,
|
||||||
CONSTRAINT fk_anime FOREIGN KEY (anime) REFERENCES $animeTable(id)
|
CONSTRAINT fk_anime FOREIGN KEY (anime) REFERENCES $animeTable(id) ON DELETE CASCADE
|
||||||
)''',
|
)''',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -106,7 +107,7 @@ class DatabaseService {
|
|||||||
print('Opening database at $databasePath');
|
print('Opening database at $databasePath');
|
||||||
_db = await openDatabase(
|
_db = await openDatabase(
|
||||||
databasePath,
|
databasePath,
|
||||||
version: 6,
|
version: 7,
|
||||||
onConfigure: (db) async {
|
onConfigure: (db) async {
|
||||||
// In order to do schema changes during database upgrades, we disable foreign
|
// In order to do schema changes during database upgrades, we disable foreign
|
||||||
// keys in the onConfigure phase, but re-enable them here.
|
// keys in the onConfigure phase, but re-enable them here.
|
||||||
@@ -134,6 +135,9 @@ class DatabaseService {
|
|||||||
if (oldVersion < 6) {
|
if (oldVersion < 6) {
|
||||||
await migrateFromV5ToV6(db);
|
await migrateFromV5ToV6(db);
|
||||||
}
|
}
|
||||||
|
if (oldVersion < 7) {
|
||||||
|
await migrateFromV6ToV7(db);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
22
lib/src/service/migrations/0004_fix_constraints.dart
Normal file
22
lib/src/service/migrations/0004_fix_constraints.dart
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import 'package:anitrack/src/service/database.dart';
|
||||||
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
|
Future<void> migrateFromV6ToV7(Database db) async {
|
||||||
|
await db.execute(
|
||||||
|
'''
|
||||||
|
CREATE TABLE ${animeWatcherJoinTable}_2 (
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
anime TEXT NOT NULL,
|
||||||
|
CONSTRAINT pk_watcher_join_table PRIMARY KEY(name, anime),
|
||||||
|
CONSTRAINT fk_watcher FOREIGN KEY (name) REFERENCES $animeWatcherTable(name) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT fk_anime FOREIGN KEY (anime) REFERENCES $animeTable(id) ON DELETE CASCADE
|
||||||
|
);''',
|
||||||
|
);
|
||||||
|
await db.execute(
|
||||||
|
'INSERT INTO ${animeWatcherJoinTable}_2 SELECT * from $animeWatcherJoinTable;',
|
||||||
|
);
|
||||||
|
await db.execute('DROP TABLE $animeWatcherJoinTable');
|
||||||
|
await db.execute(
|
||||||
|
'ALTER TABLE ${animeWatcherJoinTable}_2 RENAME TO $animeWatcherJoinTable',
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import 'package:anitrack/src/ui/helpers.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
/// A page that renders the anime list and the calendar as nested views.
|
|
||||||
class MainPage extends StatelessWidget {
|
|
||||||
const MainPage({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Scaffold(
|
|
||||||
drawer: getDrawer(context),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user