-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add support for differing capacities * format and lint * explicitely specify new column name * add back capacity_kw column * add trigger to set default value of inverter_capacity_kw to capacity_kw
- Loading branch information
1 parent
73e04c5
commit 0f600aa
Showing
4 changed files
with
409 additions
and
365 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
alembic/versions/8384de6b7c50_add_support_for_differing_inverter_and_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Add support for differing inverter and module capacities | ||
Revision ID: 8384de6b7c50 | ||
Revises: aeea08bcfc42 | ||
Create Date: 2023-04-08 12:26:23.042938 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.sql import table, column | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '8384de6b7c50' | ||
down_revision = 'aeea08bcfc42' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.add_column('sites', sa.Column('module_capacity_kw', sa.Float(), nullable=True, comment='The PV module nameplate capacity of the site')) | ||
op.add_column('sites', sa.Column('inverter_capacity_kw', sa.Float(), nullable=True, comment='The PV module nameplate capacity of the site')) | ||
|
||
op.execute("UPDATE sites SET inverter_capacity_kw=capacity_kw") | ||
|
||
# Create a trigger function to set inverter_capacity_kw to capacity_kw | ||
op.execute(""" | ||
CREATE OR REPLACE FUNCTION set_default_inverter_capacity_kw() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
NEW.inverter_capacity_kw := NEW.capacity_kw; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
""") | ||
|
||
# Create trigger on sites table, for when inverter_capacity_kw is NULL | ||
op.execute(""" | ||
CREATE TRIGGER set_default_inverter_capacity_kw_trigger | ||
BEFORE INSERT ON sites | ||
FOR EACH ROW | ||
WHEN (NEW.inverter_capacity_kw IS NULL) | ||
EXECUTE FUNCTION set_default_inverter_capacity_kw(); | ||
""") | ||
|
||
def downgrade() -> None: | ||
# Drop the trigger from the 'sites' table | ||
op.execute(""" | ||
DROP TRIGGER set_default_inverter_capacity_kw_trigger ON sites; | ||
""") | ||
|
||
# Drop the trigger function | ||
op.execute(""" | ||
DROP FUNCTION set_default_inverter_capacity_kw; | ||
""") | ||
|
||
op.drop_column('sites', 'module_capacity_kw') | ||
op.drop_column('sites', 'inverter_capacity_kw') | ||
|
Oops, something went wrong.