-
Notifications
You must be signed in to change notification settings - Fork 30
/
565e092a6c5e_introduce_the_GenericAssetType_table.py
169 lines (142 loc) · 6.09 KB
/
565e092a6c5e_introduce_the_GenericAssetType_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""introduce the GenericAssetType table
Revision ID: 565e092a6c5e
Revises: 04f0e2d2924a
Create Date: 2021-07-20 16:16:50.872449
"""
import json
from alembic import context, op
from sqlalchemy import orm
import sqlalchemy as sa
from flexmeasures.data.models.generic_assets import GenericAssetType
# revision identifiers, used by Alembic.
revision = "565e092a6c5e"
down_revision = "04f0e2d2924a"
branch_labels = None
depends_on = None
def upgrade():
"""Add GenericAssetType table
A GenericAssetType is created for each AssetType, MarketType and WeatherSensorType.
Optionally, additional GenericAssetTypes can be created using:
flexmeasures db upgrade +1 -x '{"name": "waste power plant"}' -x '{"name": "EVSE", "description": "Electric Vehicle Supply Equipment"}'
The +1 makes sure we only upgrade by 1 revision, as these arguments are only meant to be used by this upgrade function.
"""
upgrade_schema()
upgrade_data()
def downgrade():
op.drop_table("generic_asset_type")
def upgrade_data():
"""Data migration adding 1 generic asset type for each user defined generic asset type,
plus 1 generic asset type for each AssetType, MarketType and WeatherSensorType.
"""
# Get user defined generic asset types
generic_asset_types = context.get_x_argument()
# Declare ORM table views
t_asset_types = sa.Table(
"asset_type",
sa.MetaData(),
sa.Column("name", sa.String(80)),
sa.Column("display_name", sa.String(80)),
)
t_market_types = sa.Table(
"market_type",
sa.MetaData(),
sa.Column("name", sa.String(80)),
sa.Column("display_name", sa.String(80)),
)
t_weather_sensor_types = sa.Table(
"weather_sensor_type",
sa.MetaData(),
sa.Column("name", sa.String(80)),
sa.Column("display_name", sa.String(80)),
)
# Use SQLAlchemy's connection and transaction to go through the data
connection = op.get_bind()
session = orm.Session(bind=connection)
# Select all existing ids that need migrating, while keeping names intact
asset_type_results = connection.execute(
sa.select(
[
t_asset_types.c.name,
t_asset_types.c.display_name,
]
)
).fetchall()
market_type_results = connection.execute(
sa.select(
[
t_market_types.c.name,
t_market_types.c.display_name,
]
)
).fetchall()
weather_sensor_type_results = connection.execute(
sa.select(
[
t_weather_sensor_types.c.name,
t_weather_sensor_types.c.display_name,
]
)
).fetchall()
# Prepare to build a list of new generic assets
new_generic_asset_types = []
# Construct generic asset type for each user defined generic asset type
asset_type_results_dict = {k: v for k, v in asset_type_results}
market_type_results_dict = {k: v for k, v in market_type_results}
weather_sensor_type_results_dict = {k: v for k, v in weather_sensor_type_results}
for i, generic_asset_type in enumerate(generic_asset_types):
generic_asset_type_dict = json.loads(generic_asset_type)
print(
f"Constructing one generic asset type according to: {generic_asset_type_dict}"
)
if generic_asset_type_dict["name"] in asset_type_results_dict.keys():
raise ValueError(
f"User defined generic asset type named '{generic_asset_type_dict['name']}' already exists as asset type."
)
if generic_asset_type_dict["name"] in market_type_results_dict.keys():
raise ValueError(
f"User defined generic asset type named '{generic_asset_type_dict['name']}' already exists as market type."
)
if generic_asset_type_dict["name"] in weather_sensor_type_results_dict.keys():
raise ValueError(
f"User defined generic asset type named '{generic_asset_type_dict['name']}' already exists as weather sensor type."
)
new_generic_asset_type = GenericAssetType(
name=generic_asset_type_dict["name"],
description=generic_asset_type_dict.get("description", None),
)
new_generic_asset_types.append(new_generic_asset_type)
# Construct generic asset types for each AssetType
print(
f"Constructing generic asset types for each of the following asset types: {asset_type_results_dict}"
)
for name, display_name in asset_type_results_dict.items():
# Create new GenericAssets with matching names
new_generic_asset_type = GenericAssetType(name=name, description=display_name)
new_generic_asset_types.append(new_generic_asset_type)
# Construct generic asset types for each MarketType
print(
f"Constructing generic asset types for each of the following market types: {market_type_results_dict}"
)
for name, display_name in market_type_results_dict.items():
# Create new GenericAssets with matching names
new_generic_asset_type = GenericAssetType(name=name, description=display_name)
new_generic_asset_types.append(new_generic_asset_type)
# Construct generic asset types for each WeatherSensorType
print(
f"Constructing generic asset types for each of the following weather sensor types: {weather_sensor_type_results_dict}"
)
for name, display_name in weather_sensor_type_results_dict.items():
# Create new GenericAssets with matching names
new_generic_asset_type = GenericAssetType(name=name, description=display_name)
new_generic_asset_types.append(new_generic_asset_type)
# Add the new generic asset types
session.add_all(new_generic_asset_types)
session.commit()
def upgrade_schema():
op.create_table(
"generic_asset_type",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(length=80), nullable=True),
sa.Column("description", sa.String(length=80), nullable=True),
sa.PrimaryKeyConstraint("id", name=op.f("generic_asset_type_pkey")),
)