Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schultek committed Jan 23, 2023
1 parent f3518fb commit 7f08347
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pubspec.lock
coverage/
doc/api/

test/**.schema.dart
test/**/*.schema.dart

4 changes: 3 additions & 1 deletion lib/src/cli/migration/inspector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Future<DatabaseSchema> inspectDatabaseSchema(Database db) async {
(columnMap['column_default'] as String?)?.startsWith('nextval') ?? false;
tableScheme.columns[columnName] = ColumnSchema(
columnName,
type: columnIsAutoIncrement && columnType == 'int8' ? 'serial' : columnType,
type: columnIsAutoIncrement && (columnType == 'int4' || columnType == 'int8')
? 'serial'
: columnType,
isNullable: columnIsNullable == 'YES',
isAutoIncrement: columnIsAutoIncrement,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cli/migration/patcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Future<void> patchSchema(Database db, DatabaseSchemaDiff diff) async {
}),
...table.columns.modified.expand((c) sync* {
if (c.prev.type != 'serial' && c.newly.type == 'serial') {
yield 'ALTER COLUMN "${c.prev.name}" SET DATA TYPE int8 USING ${c.newly.name}::int8';
yield 'ALTER COLUMN "${c.prev.name}" SET DATA TYPE int4 USING ${c.newly.name}::int4';
yield "ALTER COLUMN \"${c.prev.name}\" SET DEFAULT nextval('${table.name}_${c.newly.name}_seq')";
} else {
var update = c.prev.type != c.newly.type
Expand Down
7 changes: 7 additions & 0 deletions test/migration/migration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ void testMigrations() {
expect(diff2Cols.modified, isEmpty);
expect(diff2Cols.added, hasLength(1));
expect(diff2Cols.added.first.name, equals('rating'));

var diff2Idx = diff2.tables.modified.first.indexes;

expect(diff2Idx.removed, isEmpty);
expect(diff2Idx.modified, isEmpty);
expect(diff2Idx.added, hasLength(1));
expect(diff2Idx.added.first.name, equals('rating_index'));
});
});
}
3 changes: 2 additions & 1 deletion test/migration/schemas/schema_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ abstract class Author {

@Model()
abstract class Book {
@AutoIncrement()
@PrimaryKey()
String get id;
int get id;

String get title;

Expand Down
7 changes: 5 additions & 2 deletions test/migration/schemas/schema_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ abstract class Author {
String get name;
}

@Model()
@Model(indexes: [
TableIndex(name: 'rating_index', columns: ['rating'])
])
abstract class Book {
@AutoIncrement()
@PrimaryKey()
String get id;
int get id;

String get title;

Expand Down

0 comments on commit 7f08347

Please sign in to comment.