climate_ref.database
#
Database adapter layer
This module provides a database adapter layer that abstracts the database connection and migrations. This allows us to easily switch between different database backends, and to run migrations when the database is loaded.
The Database class is the main entry point for interacting with the database.
It provides a session object that can be used to interact with the database and run queries.
Database
#
Manage the database connection and migrations
The database migrations are optionally run after the connection to the database is established.
Source code in packages/climate-ref/src/climate_ref/database.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
alembic_config(config)
#
Get the Alembic configuration object for the database
This includes an open connection with the database engine and the REF configuration.
Returns:
| Type | Description |
|---|---|
Config
|
The Alembic configuration object that can be used with alembic commands |
Source code in packages/climate-ref/src/climate_ref/database.py
from_config(config, run_migrations=True)
staticmethod
#
Create a Database instance from a Config instance
The REF_DATABASE_URL environment variable will take preference,
and override the database URL specified in the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
The Config instance that includes information about where the database is located |
required |
Returns:
| Type | Description |
|---|---|
Database
|
A new Database instance |
Source code in packages/climate-ref/src/climate_ref/database.py
get_or_create(model, defaults=None, **kwargs)
#
Get or create an instance of a model
This doesn't commit the transaction,
so you will need to call session.commit() after this method
or use a transaction context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Table]
|
The model to get or create |
required |
defaults
|
dict[str, Any] | None
|
Default values to use when creating a new instance |
None
|
kwargs
|
Any
|
The filter parameters to use when querying for an instance |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[Table, ModelState | None]
|
A tuple containing the instance and enum indicating if the instance was created |
Source code in packages/climate-ref/src/climate_ref/database.py
migrate(config)
#
Migrate the database to the latest revision
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
REF Configuration This is passed to alembic |
required |
Source code in packages/climate-ref/src/climate_ref/database.py
update_or_create(model, defaults=None, **kwargs)
#
Update an existing instance or create a new one
This doesn't commit the transaction,
so you will need to call session.commit() after this method
or use a transaction context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Table]
|
The model to update or create |
required |
defaults
|
dict[str, Any] | None
|
Default values to use when creating a new instance, or values to update on existing instance |
None
|
kwargs
|
Any
|
The filter parameters to use when querying for an instance |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[Table, ModelState | None]
|
A tuple containing the instance and a state enum indicating if the instance was created or updated |
Source code in packages/climate-ref/src/climate_ref/database.py
ModelState
#
validate_database_url(database_url)
#
Validate a database URL
We support sqlite databases, and we create the directory if it doesn't exist. We may aim to support PostgreSQL databases, but this is currently experimental and untested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database_url
|
str
|
The database URL to validate See climate_ref.config.DbConfig.database_url for more information on the format of the URL. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the database scheme is not supported |
Returns:
| Type | Description |
|---|---|
str
|
The validated database URL |