Skip to content

Commit

Permalink
refactor(core): Make ProjectInitService.init generate/set a `projec…
Browse files Browse the repository at this point in the history
…t_id` (meltano#6621)
  • Loading branch information
WillDaSilva committed Aug 15, 2022
1 parent 9c2d8ec commit b41a2e1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/meltano/core/project_init_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import os
import uuid

import click

Expand Down Expand Up @@ -54,6 +55,11 @@ def init(self, activate: bool = True, add_discovery: bool = False) -> Project:
self.create_files(add_discovery=add_discovery)

self.settings_service = ProjectSettingsService(self.project)
self.settings_service.set(
"project_id",
f"{self.project_name}-{uuid.uuid4()}",
store=SettingValueStore.MELTANO_YML,
)
self.set_send_anonymous_usage_stats()
if activate:
Project.activate(self.project)
Expand Down
5 changes: 1 addition & 4 deletions src/meltano/core/project_settings_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ def ensure_project_id(self) -> None:
"Unable to restore 'project_id' from 'analytics.json'", err=err
)
else:
self.update_meltano_yml_config(
{"project_id": project_id, **self.meltano_yml_config}
)
self.set("project_id", project_id)
self.set("project_id", project_id, store=SettingValueStore.MELTANO_YML)
logger.debug("Restored 'project_id' from 'analytics.json'")

@property
Expand Down
8 changes: 6 additions & 2 deletions tests/meltano/core/test_project_settings_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ def environment(self):
return Environment("testing", {})

def test_get_with_source(self, subject, monkeypatch):
# A warning is raised because the setting does not exist.
with pytest.warns(RuntimeWarning):
assert subject.get_with_source(
"and_now_for_something_completely_different"
) == (None, SettingValueStore.DEFAULT)

def assert_value_source(value, source):
assert subject.get_with_source("project_id") == (value, source)

assert_value_source(None, SettingValueStore.DEFAULT)

subject.set(
"project_id", "from_meltano_yml", store=SettingValueStore.MELTANO_YML
)
Expand Down
6 changes: 5 additions & 1 deletion tests/meltano/core/tracking/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ def test_restore_project_id_from_analytics_json(self, project: Project):
# Create a new `ProjectSettingsService` because it is what restores the project ID
restored_project_id = ProjectSettingsService(project).get("project_id")

assert original_project_id == restored_project_id
# Apply the same transformation that gets applied to the project ID
# when it is originally stored in `analytics.json`.
assert (
str(uuid.UUID(hash_sha256(original_project_id)[::2])) == restored_project_id
)

@pytest.mark.xfail(
platform.system() == "Windows",
Expand Down

0 comments on commit b41a2e1

Please sign in to comment.