Skip to content

Commit a20ba73

Browse files
committed
added microseconds to the migration ID
Otherwise unit tests could fail, if generating migrations in quick succession.
1 parent aa1ae69 commit a20ba73

File tree

1 file changed

+26
-11
lines changed
  • piccolo/apps/migrations/commands

1 file changed

+26
-11
lines changed

piccolo/apps/migrations/commands/new.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ class NewMigrationMeta:
6060
migration_path: str
6161

6262

63-
async def _create_new_migration(
64-
app_config: AppConfig, auto=False
65-
) -> NewMigrationMeta:
63+
def _generate_migration_meta(app_config: AppConfig) -> NewMigrationMeta:
6664
"""
67-
Creates a new migration file on disk.
65+
Generates the migration ID and filename.
6866
"""
69-
_id = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
67+
# The microseconds originally weren't part of the ID, but there was a
68+
# chance that the IDs would clash if the migrations are generated
69+
# programatically in quick succession (e.g. in a unit test), so they had
70+
# to be added. The trade off is a longer ID.
71+
_id = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S:%f")
7072

7173
# Originally we just used the _id as the filename, but colons aren't
7274
# supported in Windows, so we need to sanitize it. We don't want to
@@ -76,6 +78,19 @@ async def _create_new_migration(
7678

7779
path = os.path.join(app_config.migrations_folder_path, f"{filename}.py")
7880

81+
return NewMigrationMeta(
82+
migration_id=_id, migration_filename=filename, migration_path=path
83+
)
84+
85+
86+
async def _create_new_migration(
87+
app_config: AppConfig, auto=False
88+
) -> NewMigrationMeta:
89+
"""
90+
Creates a new migration file on disk.
91+
"""
92+
meta = _generate_migration_meta(app_config=app_config)
93+
7994
if auto:
8095
alter_statements = await AutoMigrationManager().get_alter_statements(
8196
app_config=app_config
@@ -97,27 +112,27 @@ async def _create_new_migration(
97112
sys.exit(0)
98113

99114
file_contents = render_template(
100-
migration_id=_id,
115+
migration_id=meta.migration_id,
101116
auto=True,
102117
alter_statements=_alter_statements,
103118
extra_imports=extra_imports,
104119
extra_definitions=extra_definitions,
105120
app_name=app_config.app_name,
106121
)
107122
else:
108-
file_contents = render_template(migration_id=_id, auto=False)
123+
file_contents = render_template(
124+
migration_id=meta.migration_id, auto=False
125+
)
109126

110127
# Beautify the file contents a bit.
111128
file_contents = black.format_str(
112129
file_contents, mode=black.FileMode(line_length=82)
113130
)
114131

115-
with open(path, "w") as f:
132+
with open(meta.migration_path, "w") as f:
116133
f.write(file_contents)
117134

118-
return NewMigrationMeta(
119-
migration_id=_id, migration_filename=filename, migration_path=path
120-
)
135+
return meta
121136

122137

123138
###############################################################################

0 commit comments

Comments
 (0)