Skip to content

Commit

Permalink
refactor(postgres): BREAKING CHANGE major release for postgres driver
Browse files Browse the repository at this point in the history
  • Loading branch information
j4qfrost committed Nov 22, 2023
1 parent 2a92e02 commit d6bf116
Show file tree
Hide file tree
Showing 33 changed files with 172 additions and 185 deletions.
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 @@ -93,9 +93,9 @@ class RunUpgradeExecutable extends Executable<Map<String, dynamic>> {
"error":
"There was an issue with the schema generated by a migration file. Reason: ${e.message}"
};
} on PostgreSQLException catch (e) {
if (e.severity == PostgreSQLSeverity.error &&
e.message!.contains("contains null values")) {
} on ServerException catch (e) {
if (e.severity == Severity.error &&
e.message.contains("contains null values")) {
return {
"error": "There was an issue when adding or altering column '${e.tableName}.${e.columnName}'. "
"This column cannot be null, but there already exist rows that would violate this constraint. "
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ dependencies:
logging: ^1.0.0
meta: ^1.1.5
path: ^1.8.0
postgres: ^2.3.1
postgres: ^3.0.0
pub_semver: ^2.0.0
yaml: ^3.1.0
dev_dependencies:
conduit_test:
path: ../test_harness
fs_test_agent:
path: ../fs_test_agent
http: ^0.13.0
lints: ^2.0.1
http: ^1.0.0
lints: ^3.0.0
matcher: ^0.12.12
mockito: ^5.3.2
test: ^1.21.6
Expand Down
2 changes: 1 addition & 1 deletion packages/codable/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ environment:
dependencies:
meta: ^1.3.0
dev_dependencies:
lints: ^2.0.1
lints: ^3.0.0
test: ^1.21.6
2 changes: 1 addition & 1 deletion packages/common/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ environment:
dependencies:
conduit_open_api: ^4.3.7
dev_dependencies:
lints: ^2.0.1
lints: ^3.0.0
2 changes: 1 addition & 1 deletion packages/config/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ dependencies:
meta: ^1.3.0
yaml: ^3.1.0
dev_dependencies:
lints: ^2.0.1
lints: ^3.0.0
test: ^1.21.6
8 changes: 4 additions & 4 deletions packages/core/lib/src/db/managed/backing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final ArgumentError _invalidValueConstruction = ArgumentError(

class ManagedValueBacking extends ManagedBacking {
@override
Map<String?, dynamic> contents = {};
Map<String, dynamic> contents = {};

@override
dynamic valueForProperty(ManagedPropertyDescription property) {
Expand Down Expand Up @@ -38,12 +38,12 @@ class ManagedForeignKeyBuilderBacking extends ManagedBacking {
ManagedBacking backing,
) {
if (backing.contents!.containsKey(entity.primaryKey)) {
contents[entity.primaryKey] = backing.contents![entity.primaryKey];
contents[entity.primaryKey!] = backing.contents![entity.primaryKey];
}
}

@override
Map<String?, dynamic> contents = {};
Map<String, dynamic> contents = {};

@override
dynamic valueForProperty(ManagedPropertyDescription property) {
Expand Down Expand Up @@ -93,7 +93,7 @@ class ManagedBuilderBacking extends ManagedBacking {
}

@override
Map<String?, dynamic> contents = {};
Map<String, dynamic> contents = {};

@override
dynamic valueForProperty(ManagedPropertyDescription property) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/src/db/managed/object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class ManagedBacking {
}

/// A map of all set values of this instance.
Map<String?, dynamic>? get contents;
Map<String, dynamic>? get contents;
}

/// An object that represents a database row.
Expand Down Expand Up @@ -271,7 +271,7 @@ abstract class ManagedObject<T> extends Serializable {
final outputMap = <String, dynamic>{};

backing.contents!.forEach((k, v) {
if (!_isPropertyPrivate(k!)) {
if (!_isPropertyPrivate(k)) {
final property = properties[k];
final value = property!.convertToPrimitiveValue(v);
if (value == null && !_includeIfNull(property)) {
Expand Down
17 changes: 7 additions & 10 deletions packages/core/lib/src/db/query/predicate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class QueryPredicate {
/// Default constructor
///
/// The [format] and [parameters] of this predicate. [parameters] may be null.
QueryPredicate(this.format, this.parameters) {
parameters ??= {};
}
QueryPredicate(this.format, [this.parameters = const {}]);

/// Creates an empty predicate.
///
Expand Down Expand Up @@ -59,10 +57,9 @@ class QueryPredicate {
final allFormatStrings = [];
final valueMap = <String, dynamic>{};
for (final predicate in predicateList) {
final duplicateKeys = predicate!.parameters?.keys
.where((k) => valueMap.keys.contains(k))
.toList() ??
[];
final duplicateKeys = predicate!.parameters.keys
.where((k) => valueMap.keys.contains(k))
.toList();

if (duplicateKeys.isNotEmpty) {
var fmt = predicate.format;
Expand All @@ -75,12 +72,12 @@ class QueryPredicate {
}

allFormatStrings.add(fmt);
predicate.parameters?.forEach((key, value) {
predicate.parameters.forEach((key, value) {
valueMap[dupeMap[key] ?? key] = value;
});
} else {
allFormatStrings.add(predicate.format);
valueMap.addAll(predicate.parameters ?? {});
valueMap.addAll(predicate.parameters);
}
}

Expand All @@ -98,7 +95,7 @@ class QueryPredicate {
///
/// Input values should not be in the format string, but instead provided in this map.
/// Keys of this map will be searched for in the format string and be replaced by the value in this map.
Map<String, dynamic>? parameters;
Map<String, dynamic> parameters;
}

/// The operator in a comparison matcher.
Expand Down
6 changes: 3 additions & 3 deletions packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
logging: ^1.0.0
meta: ^1.1.5
path: ^1.8.0
postgres: ^2.3.1
postgres: ^3.0.0
pub_semver: ^2.0.0
recase: ^4.1.0
yaml: ^3.1.0
Expand All @@ -30,8 +30,8 @@ dev_dependencies:
path: ../test_harness
fs_test_agent:
path: ../fs_test_agent
http: ^0.13.0
lints: ^2.0.1
http: ^1.0.0
lints: ^3.0.0
matcher: ^0.12.12
mockito: ^5.3.2
test: ^1.21.6
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/db/predicate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void main() {
});

test("And'ing predicate with no parameters", () {
final valid = QueryPredicate("x=y", null);
final valid = QueryPredicate("x=y");
final p = QueryPredicate.and([valid]);
expect(p.format, valid.format);
expect(p.parameters, {});
Expand Down
2 changes: 1 addition & 1 deletion packages/fs_test_agent/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ dependencies:
path: ^1.0.0
pubspec: ^2.0.1
dev_dependencies:
lints: ^2.0.1
lints: ^3.0.0
test: ^1.21.6
2 changes: 1 addition & 1 deletion packages/isolate_exec/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
glob: ^2.0.0
path: ^1.8.0
dev_dependencies:
lints: ^2.0.1
lints: ^3.0.0
test: ^1.21.6
test_package:
path: ../isolate_exec_test_packages/test_package
2 changes: 1 addition & 1 deletion packages/open_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ dependencies:
conduit_codable: ^4.3.7
meta: ^1.1.5
dev_dependencies:
lints: ^2.0.1
lints: ^3.0.0
test: ^1.3.0
2 changes: 1 addition & 1 deletion packages/password_hash/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ environment:
dependencies:
crypto: ^3.0.2
dev_dependencies:
lints: ^2.0.1
lints: ^3.0.0
test: ^1.21.6
32 changes: 8 additions & 24 deletions packages/postgresql/lib/src/builders/column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ class ColumnBuilder extends Returnable {
return property;
}

static Map<ManagedPropertyType, PostgreSQLDataType> typeMap = {
ManagedPropertyType.integer: PostgreSQLDataType.integer,
ManagedPropertyType.bigInteger: PostgreSQLDataType.bigInteger,
ManagedPropertyType.string: PostgreSQLDataType.text,
ManagedPropertyType.datetime: PostgreSQLDataType.timestampWithoutTimezone,
ManagedPropertyType.boolean: PostgreSQLDataType.boolean,
ManagedPropertyType.doublePrecision: PostgreSQLDataType.double,
ManagedPropertyType.document: PostgreSQLDataType.jsonb
static Map<ManagedPropertyType, Type> typeMap = {
ManagedPropertyType.integer: Type.integer,
ManagedPropertyType.bigInteger: Type.bigInteger,
ManagedPropertyType.string: Type.text,
ManagedPropertyType.datetime: Type.timestampWithoutTimezone,
ManagedPropertyType.boolean: Type.boolean,
ManagedPropertyType.doublePrecision: Type.double,
ManagedPropertyType.document: Type.jsonb
};

static Map<PredicateOperator, String> symbolTable = {
Expand Down Expand Up @@ -130,19 +130,7 @@ class ColumnBuilder extends Returnable {
return value;
}

String get sqlTypeSuffix {
final type = PostgreSQLFormat.dataTypeStringForDataType(
typeMap[property!.type!.kind],
);
if (type != null) {
return ":$type";
}

return "";
}

String sqlColumnName({
bool withTypeSuffix = false,
bool withTableNamespace = false,
String? withPrefix,
}) {
Expand All @@ -159,10 +147,6 @@ class ColumnBuilder extends Returnable {
name = "$name->$keys";
}

if (withTypeSuffix) {
name = "$name$sqlTypeSuffix";
}

if (withTableNamespace) {
return "${table!.sqlTableReference}.$name";
} else if (withPrefix != null) {
Expand Down
33 changes: 20 additions & 13 deletions packages/postgresql/lib/src/builders/expression.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:postgres/postgres.dart';

import 'column.dart';
import 'table.dart';
import 'package:conduit_core/conduit_core.dart';
Expand Down Expand Up @@ -48,8 +50,11 @@ class ColumnExpressionBuilder extends ColumnBuilder {
final variableName = sqlColumnName(withPrefix: defaultPrefix);

return QueryPredicate(
"$name ${ColumnBuilder.symbolTable[operator!]} @$variableName$sqlTypeSuffix",
{variableName: convertValueForStorage(value)},
"$name ${ColumnBuilder.symbolTable[operator!]} @$variableName",
{
variableName: TypedValue(ColumnBuilder.typeMap[property!.type!.kind]!,
convertValueForStorage(value)),
},
);
}

Expand All @@ -58,15 +63,17 @@ class ColumnExpressionBuilder extends ColumnBuilder {
bool within = true,
}) {
final tokenList = [];
final pairedMap = <String, dynamic>{};
final pairedMap = <String, TypedValue>{};

var counter = 0;
for (final value in values) {
final prefix = "$defaultPrefix${counter}_";

final variableName = sqlColumnName(withPrefix: prefix);
tokenList.add("@$variableName$sqlTypeSuffix");
pairedMap[variableName] = convertValueForStorage(value);
tokenList.add("@$variableName");
pairedMap[variableName] = TypedValue(
ColumnBuilder.typeMap[property!.type!.kind]!,
convertValueForStorage(value));

counter++;
}
Expand All @@ -91,12 +98,12 @@ class ColumnExpressionBuilder extends ColumnBuilder {
final rhsName = sqlColumnName(withPrefix: "${defaultPrefix}rhs_");
final operation = insideRange ? "BETWEEN" : "NOT BETWEEN";

return QueryPredicate(
"$name $operation @$lhsName$sqlTypeSuffix AND @$rhsName$sqlTypeSuffix",
{
lhsName: convertValueForStorage(lhsValue),
rhsName: convertValueForStorage(rhsValue)
});
return QueryPredicate("$name $operation @$lhsName AND @$rhsName", {
lhsName: TypedValue(ColumnBuilder.typeMap[property!.type!.kind]!,
convertValueForStorage(lhsValue)),
rhsName: TypedValue(ColumnBuilder.typeMap[property!.type!.kind]!,
convertValueForStorage(rhsValue)),
});
}

QueryPredicate stringPredicate(
Expand Down Expand Up @@ -130,8 +137,8 @@ class ColumnExpressionBuilder extends ColumnBuilder {
}

return QueryPredicate(
"$n $operation @$variableName$sqlTypeSuffix",
{variableName: matchValue},
"$n $operation @$variableName",
{variableName: TypedValue(Type.text, matchValue)},
);
}

Expand Down
8 changes: 3 additions & 5 deletions packages/postgresql/lib/src/builders/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class TableBuilder implements Returnable {

final leftColumn = left.sqlColumnName(withTableNamespace: true);
final rightColumn = right.sqlColumnName(withTableNamespace: true);
return QueryPredicate("$leftColumn=$rightColumn", null);
return QueryPredicate("$leftColumn=$rightColumn");
}

String createTableAlias() {
Expand All @@ -138,17 +138,15 @@ class TableBuilder implements Returnable {
return "t$aliasCounter";
}

void finalize(Map<String?, dynamic> variables) {
void finalize(Map<String, dynamic> variables) {
final allExpressions = [
_manualPredicate,
...expressionBuilders.map((c) => c.predicate)
];

predicate = QueryPredicate.and(allExpressions);
if (predicate?.parameters != null) {
variables.addAll(predicate!.parameters!);
variables.addAll(predicate!.parameters);
}

returning.whereType<TableBuilder>().forEach((r) {
r.finalize(variables);
});
Expand Down
6 changes: 4 additions & 2 deletions packages/postgresql/lib/src/builders/value.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:conduit_core/conduit_core.dart';
import 'package:postgres/postgres.dart';
import 'column.dart';
import 'table.dart';

Expand All @@ -8,8 +9,9 @@ class ColumnValueBuilder extends ColumnBuilder {
ManagedPropertyDescription super.property,
dynamic value,
) {
this.value = convertValueForStorage(value);
this.value = TypedValue(ColumnBuilder.typeMap[property!.type!.kind]!,
convertValueForStorage(value));
}

dynamic value;
late TypedValue value;
}
Loading

0 comments on commit d6bf116

Please sign in to comment.