migrate users db
This commit is contained in:
parent
6eb6a0cf16
commit
afd004b980
@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:developer';
|
||||||
|
|
||||||
import 'package:filcnaplo/utils/jwt.dart';
|
import 'package:filcnaplo/utils/jwt.dart';
|
||||||
import 'package:filcnaplo_kreta_api/providers/absence_provider.dart';
|
import 'package:filcnaplo_kreta_api/providers/absence_provider.dart';
|
||||||
import 'package:filcnaplo_kreta_api/providers/event_provider.dart';
|
import 'package:filcnaplo_kreta_api/providers/event_provider.dart';
|
||||||
|
@ -9,7 +9,7 @@ Future<Database> initDB() async {
|
|||||||
var settingsDB = await createSettingsTable(db);
|
var settingsDB = await createSettingsTable(db);
|
||||||
|
|
||||||
// Create table Users
|
// Create table Users
|
||||||
await db.execute("CREATE TABLE IF NOT EXISTS users (id TEXT NOT NULL, name TEXT, username TEXT, password TEXT, institute_code TEXT, student TEXT)");
|
var usersDB = await createUsersTable(db);
|
||||||
await db.execute("CREATE TABLE IF NOT EXISTS user_data ("
|
await db.execute("CREATE TABLE IF NOT EXISTS user_data ("
|
||||||
"id TEXT NOT NULL, grades TEXT, timetable TEXT, exams TEXT, homework TEXT, messages TEXT, notes TEXT, events TEXT, absences TEXT)");
|
"id TEXT NOT NULL, grades TEXT, timetable TEXT, exams TEXT, homework TEXT, messages TEXT, notes TEXT, events TEXT, absences TEXT)");
|
||||||
|
|
||||||
@ -18,7 +18,9 @@ Future<Database> initDB() async {
|
|||||||
await db.insert("settings", SettingsProvider.defaultSettings().toMap());
|
await db.insert("settings", SettingsProvider.defaultSettings().toMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
await migrateDB(db, settingsDB.struct.keys);
|
// Migrate Databases
|
||||||
|
await migrateDB(db, "settings", settingsDB.struct.keys, SettingsProvider.defaultSettings().toMap(), createSettingsTable);
|
||||||
|
await migrateDB(db, "users", usersDB.struct.keys, {"role": 0}, createUsersTable);
|
||||||
|
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
@ -38,35 +40,59 @@ Future<DatabaseStruct> createSettingsTable(Database db) async {
|
|||||||
return settingsDB;
|
return settingsDB;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> migrateDB(Database db, Iterable<String> keys) async {
|
Future<DatabaseStruct> createUsersTable(Database db) async {
|
||||||
var settings = (await db.query("settings"))[0];
|
var usersDB = DatabaseStruct(
|
||||||
|
{"id": String, "name": String, "username": String, "password": String, "institute_code": String, "student": String, "role": String});
|
||||||
|
|
||||||
bool migrationRequired = keys.any((key) => !settings.containsKey(key) || settings[key] == null);
|
// Create table Users
|
||||||
|
await db.execute("CREATE TABLE IF NOT EXISTS users ($usersDB)");
|
||||||
|
|
||||||
if (migrationRequired) {
|
return usersDB;
|
||||||
var defaultSettings = SettingsProvider.defaultSettings();
|
}
|
||||||
var settingsCopy = Map<String, dynamic>.from(settings);
|
|
||||||
|
Future<void> migrateDB(
|
||||||
// Delete settings
|
Database db,
|
||||||
await db.execute("drop table settings");
|
String table,
|
||||||
|
Iterable<String> keys,
|
||||||
// Fill missing columns
|
Map<String, Object?> defaultValues,
|
||||||
keys.forEach((key) {
|
Future<DatabaseStruct> Function(Database) create,
|
||||||
if (!keys.contains(key)) {
|
) async {
|
||||||
print("debug: dropping $key");
|
var originalRows = (await db.query(table));
|
||||||
settingsCopy.remove(key);
|
|
||||||
}
|
if (originalRows.length == 0) {
|
||||||
|
await db.execute("drop table $table");
|
||||||
if (!settings.containsKey(key) || settings[key] == null) {
|
await create(db);
|
||||||
print("DEBUG: migrating $key");
|
return;
|
||||||
settingsCopy[key] = defaultSettings.toMap()[key];
|
}
|
||||||
}
|
|
||||||
});
|
await Future.forEach<Map<String, Object?>>(originalRows, (original) async {
|
||||||
|
bool migrationRequired = keys.any((key) => !original.containsKey(key) || original[key] == null);
|
||||||
// Recreate settings
|
|
||||||
await createSettingsTable(db);
|
if (migrationRequired) {
|
||||||
await db.insert("settings", settingsCopy);
|
print("INFO: Migrating $table");
|
||||||
|
var copy = Map<String, dynamic>.from(original);
|
||||||
print("INFO: Database migrated");
|
|
||||||
}
|
// Delete table
|
||||||
|
await db.execute("drop table $table");
|
||||||
|
|
||||||
|
// Fill missing columns
|
||||||
|
keys.forEach((key) {
|
||||||
|
if (!keys.contains(key)) {
|
||||||
|
print("DEBUG: dropping $key");
|
||||||
|
copy.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!original.containsKey(key) || original[key] == null) {
|
||||||
|
print("DEBUG: migrating $key");
|
||||||
|
copy[key] = defaultValues[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Recreate table
|
||||||
|
await create(db);
|
||||||
|
await db.insert(table, copy);
|
||||||
|
|
||||||
|
print("INFO: Database migrated");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ class DatabaseStruct {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "${name} ${typeName.toUpperCase()}";
|
return "${name} ${typeName.toUpperCase()} ${name == 'id' ? 'NOT NULL' : ''}";
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user