forked from authpass/postgres_utils.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
106 lines (89 loc) · 2.73 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import 'package:logging/logging.dart';
import 'package:logging_appenders/logging_appenders.dart';
import 'package:postgres/postgres.dart';
import 'package:postgres_utils/postgres_utils.dart';
import 'package:uuid/data.dart';
import 'package:uuid/rng.dart';
import 'package:uuid/uuid.dart';
final _logger = Logger('main');
final Uuid _uuid = Uuid(goptions: GlobalOptions(CryptoRNG()));
class DatabaseTransaction extends DatabaseTransactionBase<MyTables> {
DatabaseTransaction(TxSession conn, MyTables tables) : super(conn, tables);
}
class DatabaseAccess extends DatabaseAccessBase<DatabaseTransaction, MyTables> {
DatabaseAccess({
required DatabaseConfig config,
}) : super(
config: config,
tables: MyTables(),
migrations: MyMigrationsProvider(),
);
@override
DatabaseTransaction createDatabaseTransaction(
TxSession conn, MyTables tables) {
return DatabaseTransaction(conn, tables);
}
}
class MyTables extends TablesBase {
MyTables();
late final UserTable user = UserTable();
@override
List<TableBase> get tables => [
user,
];
}
class UserTable extends TableBase {
UserTable();
static const TABLE_USER = 'example_user';
@override
List<String> get tables => [
TABLE_USER,
];
Future<void> createTables(DatabaseTransaction db) async {
await db.execute('''
CREATE TABLE $TABLE_USER (id uuid primary key, username varchar)
''');
}
Future<void> createUser(DatabaseTransactionBase db, String userName) async {
await db.executeInsert(TABLE_USER, {
'id': _uuid.v4(),
'username': userName,
});
}
}
class MyMigrationsProvider
extends MigrationsProvider<DatabaseTransaction, MyTables> {
@override
List<Migrations<DatabaseTransaction, MyTables>> get migrations {
return [
Migrations(
id: 1,
up: (conn) async {
await conn.tables.user.createTables(conn);
}),
];
}
}
Future<void> main() async {
PrintAppender.setupLogging();
const dbName = 'example_tmp';
final config = DatabaseConfig.fromEnvironment();
await _createDb(dbName, config);
final access = DatabaseAccess(config: config.copyWith(databaseName: dbName));
await access.prepareDatabase();
await access.run((db) async {
await db.tables.user.createUser(db, 'foo');
_logger.info('Successfully created user.');
});
await access.dispose();
}
Future<void> _createDb(String dbName, DatabaseConfig config) async {
final tmp = DatabaseAccess(
config: config,
);
// ignore: invalid_use_of_visible_for_testing_member
await tmp.forTestDropDatabase(dbName, ifExists: true);
// ignore: invalid_use_of_visible_for_testing_member
await tmp.forTestCreateDatabase(dbName);
await tmp.dispose();
}