1
+ import 'package:logging_appenders/logging_appenders.dart' ;
1
2
import 'package:postgres/postgres.dart' ;
2
3
import 'package:postgres_utils/postgres_utils.dart' ;
4
+ import 'package:uuid/uuid.dart' ;
5
+ import 'package:uuid/uuid_util.dart' ;
6
+
7
+ import 'package:logging/logging.dart' ;
8
+
9
+ final _logger = Logger ('main' );
10
+
11
+ const Uuid _uuid = Uuid (options: < String , dynamic > {'grng' : UuidUtil .cryptoRNG});
3
12
4
13
class DatabaseTransaction extends DatabaseTransactionBase <MyTables > {
5
14
DatabaseTransaction (PostgreSQLExecutionContext conn, MyTables tables)
@@ -26,6 +35,7 @@ class MyTables extends TablesBase {
26
35
MyTables ();
27
36
28
37
late final UserTable user = UserTable ();
38
+
29
39
@override
30
40
List <TableBase > get tables => [
31
41
user,
@@ -35,7 +45,7 @@ class MyTables extends TablesBase {
35
45
class UserTable extends TableBase {
36
46
UserTable ();
37
47
38
- static const TABLE_USER = 'user ' ;
48
+ static const TABLE_USER = 'example_user ' ;
39
49
40
50
@override
41
51
List <String > get tables => [
@@ -47,6 +57,13 @@ class UserTable extends TableBase {
47
57
CREATE TABLE $TABLE_USER (id uuid primary key, username varchar)
48
58
''' );
49
59
}
60
+
61
+ Future <void > createUser (DatabaseTransactionBase db, String userName) async {
62
+ await db.executeInsert (TABLE_USER , {
63
+ 'id' : _uuid.v4 (),
64
+ 'username' : userName,
65
+ });
66
+ }
50
67
}
51
68
52
69
class MyMigrationsProvider
@@ -62,3 +79,29 @@ class MyMigrationsProvider
62
79
];
63
80
}
64
81
}
82
+
83
+ Future <void > main () async {
84
+ PrintAppender .setupLogging ();
85
+ const dbName = 'example_tmp' ;
86
+ final config = DatabaseConfig .fromEnvironment ();
87
+ await _createDb (dbName, config);
88
+
89
+ final access = DatabaseAccess (config: config.copyWith (databaseName: dbName));
90
+ await access.prepareDatabase ();
91
+ await access.run ((db) async {
92
+ await db.tables.user.createUser (db, 'foo' );
93
+ _logger.info ('Successfully created user.' );
94
+ });
95
+ await access.dispose ();
96
+ }
97
+
98
+ Future <void > _createDb (String dbName, DatabaseConfig config) async {
99
+ final tmp = DatabaseAccess (
100
+ config: config,
101
+ );
102
+ // ignore: invalid_use_of_visible_for_testing_member
103
+ await tmp.forTestDropDatabase (dbName, ifExists: true );
104
+ // ignore: invalid_use_of_visible_for_testing_member
105
+ await tmp.forTestCreateDatabase (dbName);
106
+ await tmp.dispose ();
107
+ }
0 commit comments