Skip to content

Commit

Permalink
Update ml id column sites (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukh-P authored Sep 20, 2024
1 parent 0fa40db commit 9a09bbc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Remove uniqueness from ml id for site table
Revision ID: 2a6e6975cd72
Revises: 34891d466985
Create Date: 2024-09-20 15:23:59.973409
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '2a6e6975cd72'
down_revision = '34891d466985'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('sites', 'ml_id',
existing_type=sa.INTEGER(),
comment='Auto-incrementing integer ID of the site for use in ML training',
existing_nullable=False,
autoincrement=True)
op.drop_constraint('sites_ml_id_key', 'sites', type_='unique')
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint('sites_ml_id_key', 'sites', ['ml_id'])
op.alter_column('sites', 'ml_id',
existing_type=sa.INTEGER(),
comment=None,
existing_comment='Auto-incrementing integer ID of the site for use in ML training',
existing_nullable=False,
autoincrement=True)
# ### end Alembic commands ###
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pvsite_datamodel/sqlmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ class SiteSQL(Base, CreatedMixin):
sa.Integer,
autoincrement=True,
nullable=False,
unique=True,
comment="Auto-incrementing integer ID of the site for use in ML training",
)

Expand Down
4 changes: 3 additions & 1 deletion pvsite_datamodel/write/user_and_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def create_site(
tilt: Optional[float] = None,
inverter_capacity_kw: Optional[float] = None,
module_capacity_kw: Optional[float] = None,
ml_id: Optional[int] = None,
) -> [SiteSQL, str]:
"""
Create a site and adds it to the database.
Expand All @@ -93,6 +94,7 @@ def create_site(
:param tilt: tilt of site, default is 35
:param inverter_capacity_kw: inverter capacity of site in kw
:param module_capacity_kw: module capacity of site in kw
:param ml_id: internal ML modelling id
"""
max_ml_id = session.query(func.max(SiteSQL.ml_id)).scalar()
Expand Down Expand Up @@ -133,7 +135,7 @@ def create_site(
dno = json.dumps(dno)

site = SiteSQL(
ml_id=max_ml_id + 1,
ml_id=ml_id if ml_id else max_ml_id + 1,
client_site_id=client_site_id,
client_site_name=client_site_name,
latitude=latitude,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ def test_create_new_site_twice(db_session):
assert site_2.ml_id == 2


# test for create_site to check ml_id duplicates are allowed for separate clients
def test_ml_id_duplicate_for_unique_clients(db_session):
"""Test create sites with duplicate ml_id for different clients"""

site_1, _ = create_site(
session=db_session,
client_site_id=6932,
client_site_name="test_site_name_1",
latitude=1.0,
longitude=1.0,
capacity_kw=1.0,
ml_id=1,
)

site_2, _ = create_site(
session=db_session,
client_site_id=6933,
client_site_name="test_site_name_2",
latitude=1.0,
longitude=1.0,
capacity_kw=1.0,
ml_id=1,
)

assert site_1.ml_id == 1
assert site_2.ml_id == 1


def test_create_user(db_session):
"Test to create a new user."

Expand Down

0 comments on commit 9a09bbc

Please sign in to comment.