Skip to content

Commit

Permalink
refactor: allow creating multiple migrations as part of test setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
SandPod committed Nov 17, 2023
1 parent 730c7b3 commit 73d3199
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import 'package:path/path.dart' as path;
import 'package:serverpod_cli/src/migrations/migration_registry.dart';
import 'package:serverpod_service_client/serverpod_service_client.dart';
import 'package:serverpod/protocol.dart' as serverProtocol;
import 'package:uuid/uuid.dart';

abstract class MigrationTestUtils {
static Future<void> createInitialState({
required Map<String, String> protocols,
required List<Map<String, String>> migrationProtocols,
String tag = 'test',
}) async {
assert(
await createMigrationFromProtocols(protocols: protocols, tag: tag) == 0,
'Failed to create migration.',
);
for (var protocols in migrationProtocols) {
assert(
await createMigrationFromProtocols(protocols: protocols, tag: tag) == 0,
'Failed to create migration.',
);
}

assert(
await runApplyMigrations() == 0,
'Failed to create migration.',
Expand All @@ -38,12 +42,13 @@ abstract class MigrationTestUtils {
protocolFile.writeAsStringSync(contents);
});

var suffixedTag = '$tag-${Uuid().v4()}';
return await _runProcess(
'serverpod',
arguments: [
'create-migration',
'--tag',
tag,
suffixedTag,
if (force) '--force',
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = <String, String>{};
var createMigrationExitCode =
Expand Down Expand Up @@ -191,7 +191,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = <String, String>{};
var createMigrationExitCode =
Expand Down Expand Up @@ -246,7 +246,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var addedColumn = 'addedColumn';
var targetStateProtocols = {
Expand Down Expand Up @@ -322,7 +322,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down Expand Up @@ -395,7 +395,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down Expand Up @@ -438,7 +438,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var addedColumn = 'addedColumn';
var targetStateProtocols = {
Expand Down Expand Up @@ -485,7 +485,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var addedColumn = 'addedColumn';
var targetStateProtocols = {
Expand Down Expand Up @@ -562,7 +562,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down Expand Up @@ -646,7 +646,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down Expand Up @@ -729,7 +729,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down Expand Up @@ -774,7 +774,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var addedIndex = 'migrated_table_index';
var targetStateProtocols = {
Expand Down Expand Up @@ -857,7 +857,7 @@ indexes:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down Expand Up @@ -930,7 +930,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down Expand Up @@ -1002,7 +1002,7 @@ fields:
'''
};
await MigrationTestUtils.createInitialState(
protocols: initialStateProtocols, tag: tag);
migrationProtocols: [initialStateProtocols], tag: tag);

var targetStateProtocols = {
'migrated_table': '''
Expand Down

0 comments on commit 73d3199

Please sign in to comment.