Skip to content

Commit

Permalink
feat(server): Restore default
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Mar 5, 2024
1 parent bdbda2a commit e78e02e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import 'entity_controller.dart';
class DatabaseController {
/// Reset all tables
Future<Response> reset(Request request) async {
Iterable<EntityController> entityControllers = dataTypes.map((t) => EntityController.getControllerFromDataType(t));
await Future.forEach(entityControllers, (e) => e.deleteMany());
return Response.ok('{"status": "success"}');
try {
await _restoreDefault();
Iterable<EntityController> entityControllers =
dataTypes.map((t) => EntityController.getControllerFromDataType(t));
await Future.forEach(entityControllers, (e) => e.deleteMany());
return Response.ok('{"status": "success"}');
} catch (err) {
return Response.internalServerError(body: '{"err": "$err"}');
}
}

/// Upgrade the existing database
Expand Down Expand Up @@ -46,18 +52,22 @@ class DatabaseController {
}
}

/// Restore a database dump
Future<Response> restore(Request request) async {
/// Restore the default database dump
Future<Response> restoreDefault(Request request) async {
try {
await _restoreDefault();
return Response.ok('{"status": "success"}');
} catch (err) {
return Response.internalServerError(body: '{"err": "$err"}');
}
}

Future<void> _restoreDefault() async {
final db = PostgresDb();
final conn = db.connection;
try {
{
await conn.execute('DROP SCHEMA IF EXISTS public CASCADE;');
await db.close();
// await conn.execute('CREATE SCHEMA public;');
// await conn.execute('GRANT ALL ON SCHEMA public TO postgres;');
// await conn.execute('GRANT ALL ON SCHEMA public TO public;');
} catch (e) {
return Response.internalServerError(body: '{"err": "$e"}');
}

final args = <String>[
Expand All @@ -71,27 +81,11 @@ class DatabaseController {
db.postgresPort.toString(),
db.postgresDatabaseName,
];
final process = await Process.run('psql', args, environment: {'PGPASSWORD': db.dbPW});

// final dumpFile = File('./database/dump/PostgreSQL-wrestling_scoreboard-dump.sql');
// final dumpContent = await dumpFile.readAsLines();
// dumpContent.removeWhere((element) => element.isEmpty);
// dumpContent.removeWhere((element) => element.startsWith('--'));
// final dumpStr = dumpContent.reduce((value, element) => value + element);
// final sqls = dumpStr.split(';');
// for (var sql in sqls) {
// try {
// await conn.execute('$sql;');
// } catch (_) {
// print('Executed SQL command: $sql');
// rethrow;
// }
// }
final processResult = await Process.run('psql', args, environment: {'PGPASSWORD': db.dbPW});
await db.open();
if (process.exitCode == 0) {
return Response.ok('{"status": "success"}');
} else {
return Response.internalServerError(body: '{"err": "${process.stderr}"}');

if (processResult.exitCode != 0) {
throw processResult.stderr;
}
}
}
2 changes: 1 addition & 1 deletion wrestling_scoreboard_server/lib/routes/api_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ApiRoute {
router.post('/database/reset', databaseController.reset);
router.post('/database/upgrade', databaseController.upgrade);
router.post('/database/export', databaseController.export);
router.post('/database/restore', databaseController.restore);
router.post('/database/restoreDefault', databaseController.restoreDefault);

final boutConfigController = BoutConfigController();
router.post('/bout_config', boutConfigController.postSingle);
Expand Down
6 changes: 3 additions & 3 deletions wrestling_scoreboard_server/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ <h1>Wrestling Scoreboard Server</h1>
onsubmit="return confirm('Are you sure you want to export the database?');">
<input type='submit' value="Export">
</form>
<form action='/api/database/restore' method='POST'
onsubmit="return confirm('Are you sure you want to restore the database?');">
<input type='submit' value="Restore">
<form action='/api/database/restore_default' method='POST'
onsubmit="return confirm('Are you sure you want to restore the default database?');">
<input type='submit' value="Restore Default">
</form>
</ul>
<li><a href="/ws">Websocket Endpoint</a>
Expand Down

0 comments on commit e78e02e

Please sign in to comment.