Skip to content

Commit

Permalink
changed SslMode to String for isolate messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
j4qfrost committed May 18, 2024
1 parent 755c073 commit e062931
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 70 deletions.
10 changes: 5 additions & 5 deletions packages/cli/lib/src/migration_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class MigrationSource {
MigrationSource(this.source, this.uri, int nameStartIndex, int nameEndIndex) {
originalName = source!.substring(nameStartIndex, nameEndIndex);
name = "M${md5.convert(source!.codeUnits)}";
source = source!.replaceRange(nameStartIndex, nameEndIndex, name!);
source = source!.replaceRange(nameStartIndex, nameEndIndex, name);
}

MigrationSource.fromMap(Map<String, dynamic> map) {
originalName = map["originalName"] as String?;
originalName = map["originalName"] as String;
source = map["source"] as String?;
name = map["name"] as String?;
name = map["name"] as String;
uri = map["uri"] as String?;
}

Expand Down Expand Up @@ -58,9 +58,9 @@ class MigrationSource {

String? source;

String? originalName;
late final String originalName;

String? name;
late final String name;

String? uri;

Expand Down
17 changes: 1 addition & 16 deletions packages/cli/lib/src/mixins/database_connecting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ import 'package:conduit/src/metadata.dart';
import 'package:conduit/src/mixins/project.dart';
import 'package:conduit_core/conduit_core.dart';
import 'package:conduit_postgresql/conduit_postgresql.dart';
import 'package:postgres/postgres.dart';

extension ToSslMode on String {
SslMode toSslMode() {
switch (this) {
case "disable":
return SslMode.disable;
case "require":
return SslMode.require;
case "verifyFull":
return SslMode.verifyFull;
}
return SslMode.disable;
}
}

mixin CLIDatabaseConnectingCommand implements CLICommand, CLIProject {
static const String flavorPostgreSQL = "postgres";
Expand All @@ -38,7 +23,7 @@ mixin CLIDatabaseConnectingCommand implements CLICommand, CLIProject {
help:
"Whether or not the database connection should use SSL (disable/require/verifyFull)",
defaultsTo: "disable")
SslMode get sslMode => (decode("ssl-mode") as String).toSslMode();
String get sslMode => decode("ssl-mode");

@Option(
"connect",
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/lib/src/scripts/run_upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class DBInfo {
this.port,
this.databaseName,
this.timeZone, {
this.sslMode = SslMode.disable,
this.sslMode,
});

DBInfo.fromMap(Map<String, dynamic> map)
Expand All @@ -140,7 +140,7 @@ class DBInfo {
port = map["port"] as int?,
databaseName = map["databaseName"] as String?,
timeZone = map["timeZone"] as String?,
sslMode = (map["sslMode"] ?? SslMode.disable) as SslMode;
sslMode = map["sslMode"] as String?;

final String? flavor;
final String? username;
Expand All @@ -149,7 +149,7 @@ class DBInfo {
final int? port;
final String? databaseName;
final String? timeZone;
final SslMode? sslMode;
final String? sslMode;

Map<String, dynamic>? asMap() {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/src/scripts/schema_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SchemaBuilderExecutable extends Executable<Map<String, dynamic>> {
Schema outputSchema = inputSchema;
for (final source in sources) {
final Migration instance = instanceOf(
source.name!,
source.name,
positionalArguments: const [],
namedArguments: const <Symbol, dynamic>{},
constructorName: Symbol.empty,
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/command_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:conduit/src/command.dart';
import 'package:conduit/src/metadata.dart';
import 'package:conduit/src/mixins/database_connecting.dart';
import 'package:conduit_postgresql/conduit_postgresql.dart';
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -172,12 +172,12 @@ void main() {
test('Enum checks on sslMode, correct value', () {
final cmd = TestCLICommand();

final args = ['--sslMode=disable'];
final args = ['--sslMode=require'];

final results = cmd.options.parse(args);
cmd.process(results);

expect(cmd.sslMode, equals(SslMode.disable));
expect(cmd.sslMode.toSslMode(), equals(SslMode.require));
});
}

Expand Down Expand Up @@ -227,7 +227,7 @@ class TestCLICommand extends CLICommand {
bool get useSSLWithDefault => decode("useSSLWithDefault");

@Option("sslMode")
SslMode get sslMode => (decode("sslMode") as String).toSslMode();
String get sslMode => decode("sslMode");

@Option(
"scopes",
Expand Down
70 changes: 36 additions & 34 deletions packages/cli/test/migration_execution_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ String connectString =
"postgres://${connectInfo.username}:${connectInfo.password}@${connectInfo.host}:${connectInfo.port}/${connectInfo.databaseName}";

void main() {
late PostgreSQLPersistentStore store;
PostgreSQLPersistentStore? store;

setUpAll(() async {
final t =
Expand Down Expand Up @@ -54,10 +54,10 @@ void main() {

await Future.wait(
tables.map((t) {
return store.execute("DROP TABLE IF EXISTS $t");
}),
return store?.execute("DROP TABLE IF EXISTS $t");
}).nonNulls,
);
await store.close();
await store?.close();
});

tearDownAll(DartProjectAgent.tearDownAll);
Expand All @@ -69,28 +69,28 @@ void main() {

test("Generate and execute initial schema makes workable DB", () async {
expect(await runMigrationCases(["Case1"]), isZero);
final version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
final version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1]
]);
expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
});

test(
"Database already up to date returns 0 status code, does not change version",
() async {
expect(await runMigrationCases(["Case2"]), isZero);

var versionRow = await store.execute(
var versionRow = await store!.execute(
"SELECT versionNumber, dateOfUpgrade FROM _conduit_version_pgsql",
) as List<List<dynamic>>;
expect(versionRow.first.first, 1);
final updateDate = versionRow.first.last;

cli.clearOutput();
expect(await runMigrationCases(["Case2"]), isZero);
versionRow = await store.execute(
versionRow = await store!.execute(
"SELECT versionNumber, dateOfUpgrade FROM _conduit_version_pgsql",
) as List<List>;
expect(versionRow.length, 1);
Expand All @@ -102,45 +102,45 @@ void main() {
test("Multiple migration files are ran", () async {
expect(await runMigrationCases(["Case31", "Case32"]), isZero);

final version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
final version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1],
[2]
]);
expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store, "_foo"), ["id", "testobject_id"]);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store!, "_foo"), ["id", "testobject_id"]);
});

test("Only later migration files are ran if already at a version", () async {
expect(await runMigrationCases(["Case41"]), isZero);
var version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
var version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1]
]);
cli.clearOutput();

expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await tableExists(store, "_foo"), isFalse);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
expect(await tableExists(store!, "_foo"), isFalse);

expect(await runMigrationCases(["Case42"], fromVersion: 1), isZero);
version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1],
[2]
]);

expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store, "_foo"), ["id", "testobject_id"]);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store!, "_foo"), ["id", "testobject_id"]);
});

test("If migration throws exception, rollback any changes", () async {
expect(await runMigrationCases(["Case5"]), isNonZero);

expect(await tableExists(store, store.versionTable.name), isFalse);
expect(await tableExists(store, "_testobject"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isFalse);
expect(await tableExists(store!, "_testobject"), isFalse);
});

test(
Expand All @@ -164,9 +164,9 @@ void main() {
);
expect(cli.output, contains('relation "_unknowntable" does not exist'));

expect(await tableExists(store, store.versionTable.name), isFalse);
expect(await tableExists(store, "_testobject"), isFalse);
expect(await tableExists(store, "_foo"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isFalse);
expect(await tableExists(store!, "_testobject"), isFalse);
expect(await tableExists(store!, "_foo"), isFalse);
},
);

Expand All @@ -187,23 +187,23 @@ void main() {

expect(cli.output, contains('relation "_unknowntable" does not exist'));

final version = await store
final version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1],
]);

expect(await tableExists(store, store.versionTable.name), isTrue);
expect(await tableExists(store, "_testobject"), isTrue);
expect(await tableExists(store, "_foo"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isTrue);
expect(await tableExists(store!, "_testobject"), isTrue);
expect(await tableExists(store!, "_foo"), isFalse);
},
);

test("If seed fails, all schema changes are rolled back", () async {
expect(await runMigrationCases(["Case7"]), isNonZero);

expect(await tableExists(store, store.versionTable.name), isFalse);
expect(await tableExists(store, "_testobject"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isFalse);
expect(await tableExists(store!, "_testobject"), isFalse);
});

test(
Expand Down Expand Up @@ -256,7 +256,7 @@ MigrationSource migrationSourceFromClassDeclaration(ClassDeclaration cu) {
}

List<MigrationSource> getOrderedTestMigrations(
List<String?> names, {
List<String> names, {
int fromVersion = 0,
}) {
final uri = Directory.current.uri
Expand All @@ -279,7 +279,7 @@ List<MigrationSource> getOrderedTestMigrations(
}

Future runMigrationCases(
List<String?> migrationNames, {
List<String> migrationNames, {
int fromVersion = 0,
StringSink? log,
}) async {
Expand All @@ -298,6 +298,8 @@ Future runMigrationCases(

final String useSsl = Platform.environment["USE_SSL"] ?? "";

print(useSsl);

final res =
await cli.run("db", ["upgrade", useSsl, "--connect", connectString]);

Expand Down
3 changes: 3 additions & 0 deletions packages/isolate_exec/lib/src/executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class IsolateExecutor<U> {
automaticPackageResolution: packageConfigURI == null,
);
return await completer.future;
} catch (e) {
print(e);
rethrow;
} finally {
onErrorPort.close();
controlPort.close();
Expand Down
27 changes: 20 additions & 7 deletions packages/postgresql/lib/src/postgresql_persistent_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import 'postgresql_query.dart';
import 'postgresql_schema_generator.dart';
import 'package:postgres/postgres.dart';

extension ToSslMode on String? {
SslMode toSslMode() {
switch (this) {
case "disable":
return SslMode.disable;
case "require":
return SslMode.require;
case "verifyFull":
return SslMode.verifyFull;
}
return SslMode.disable;
}
}

/// The database layer responsible for carrying out [Query]s against PostgreSQL databases.
///
/// To interact with a PostgreSQL database, a [ManagedContext] must have an instance of this class.
Expand All @@ -21,7 +35,7 @@ class PostgreSQLPersistentStore extends PersistentStore
this.timeZone = "UTC",
this.sslMode,
@Deprecated('Use sslMode instead') bool useSSL = false,
}) : isSSLConnection = useSSL;
}) : isSSLConnection = useSSL || sslMode.toSslMode() != SslMode.disable;

/// Same constructor as default constructor.
///
Expand All @@ -35,10 +49,11 @@ class PostgreSQLPersistentStore extends PersistentStore
this.timeZone = "UTC",
this.sslMode,
@Deprecated('Use sslMode instead') bool useSSL = false,
}) : isSSLConnection = useSSL;
}) : isSSLConnection = useSSL || sslMode.toSslMode() != SslMode.disable;

PostgreSQLPersistentStore._from(PostgreSQLPersistentStore from)
: isSSLConnection = from.isSSLConnection,
: isSSLConnection =
from.isSSLConnection || from.sslMode.toSslMode() != SslMode.disable,
username = from.username,
password = from.password,
host = from.host,
Expand Down Expand Up @@ -79,7 +94,7 @@ class PostgreSQLPersistentStore extends PersistentStore
final bool isSSLConnection;

/// The SSL mode of the connection to the database.
final SslMode? sslMode;
final String? sslMode;

/// Whether or not the underlying database connection is open.
///
Expand Down Expand Up @@ -413,7 +428,6 @@ class PostgreSQLPersistentStore extends PersistentStore

Future<Connection> _connect() {
logger.info("PostgreSQL connecting, $username@$host:$port/$databaseName.");

return Connection.open(
Endpoint(
host: host!,
Expand All @@ -424,8 +438,7 @@ class PostgreSQLPersistentStore extends PersistentStore
),
settings: ConnectionSettings(
timeZone: timeZone!,
sslMode:
sslMode ?? (isSSLConnection ? SslMode.verifyFull : SslMode.disable),
sslMode: sslMode.toSslMode(),
ignoreSuperfluousParameters: true,
),
);
Expand Down

0 comments on commit e062931

Please sign in to comment.