Macrostrat Database
Macrostrat's Database module provides a simplified wrapper over SQLAlchemy databases, making it easier to build common database management functionality.
macrostrat.database.Database
Bases: object
Source code in database/macrostrat/database/core.py
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 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
model
property
Map of all tables in the database as SQLAlchemy models
https://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html
table
property
Map of all tables in the database as SQLAlchemy table objects
__getitem__(name)
Subscript shorthand for get_table().
Source code in database/macrostrat/database/core.py
457 458 459 | |
__init__(db_conn, *, echo_sql=False, **kwargs)
Wrapper for interacting with a database using SQLAlchemy. Optimized for use with PostgreSQL, but usable with SQLite as well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_conn
|
str | URL | Engine
|
Connection string or engine for the database. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
echo_sql |
bool
|
If True, will echo SQL commands to the console. Default is False. |
instance_params |
dict
|
Parameters to pass to queries and other database operations. |
Source code in database/macrostrat/database/core.py
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 | |
create_tables()
Create all tables described by the database's metadata instance.
Source code in database/macrostrat/database/core.py
96 97 98 99 100 | |
entity_names(**kwargs)
Returns an iterator of names of schema objects (both tables and views) from a the database.
Source code in database/macrostrat/database/core.py
242 243 244 245 246 247 248 | |
exec_sql(sql, params=None, **kwargs)
Executes SQL files passed
Source code in database/macrostrat/database/core.py
201 202 203 204 205 206 207 | |
get_dataframe(*args)
Returns a Pandas DataFrame from a SQL query
Source code in database/macrostrat/database/core.py
209 210 211 | |
get_model(name, *, schema=None, automap=True)
Return the ORM model class for a table.
If the target schema has not yet been reflected and automap=True
(the default), it is reflected lazily before the lookup. Set
automap=False to raise LookupError instead, which is useful
when you want strict control over when reflection happens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Table name as |
required | |
schema
|
Explicit schema override (default |
None
|
|
automap
|
Lazily reflect the schema if not yet mapped. |
True
|
Raises:
| Type | Description |
|---|---|
LookupError
|
When the model is not found. |
Source code in database/macrostrat/database/core.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
get_or_create(model, **kwargs)
Get an instance of a model, or create it if it doesn't exist.
Source code in database/macrostrat/database/core.py
255 256 257 258 259 260 261 262 | |
get_table(name, *, schema=None)
Return a reflected SQLAlchemy Table object, with per-instance caching.
After the first call the result is cached; subsequent calls for the same table are instant. If automap has already been run the mapper's existing Table is reused, avoiding a second round-trip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Table name as |
required | |
schema
|
Explicit schema override (default |
None
|
Source code in database/macrostrat/database/core.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
reflect_table(*args, **kwargs)
One-off reflection of a database table or view. Note: for most purposes,
it will be better to use the database tables automapped at runtime using
self.automap(). Then, tables can be accessed using the
self.table object. However, this function can be useful for views (which
are not reflected automatically), or to customize type definitions for mapped
tables.
A set of column_args can be used to pass columns to override with the mapper, for
instance to set up foreign and primary key constraints.
https://docs.sqlalchemy.org/en/13/core/reflection.html#reflecting-views
Source code in database/macrostrat/database/core.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
refresh_schema(*, automap=None)
Refresh the current database connection
- closes the session and flushes
- removes the inspector
If automap is True, will automap the database after refreshing. If automap is False, will not automap the database after refreshing. If automap is None, it will re-map the database if it was previously mapped.
Source code in database/macrostrat/database/core.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
run_fixtures(fixtures, params=None, **kwargs)
Run a set of fixtures on the database object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fixtures
|
Path | list[Path]
|
Path to a directory of fixtures or a list of paths to fixture files. |
required |
params
|
dict
|
Parameters to pass to the query. |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
use_instance_params |
bool
|
If True, will use the instance_params set on the Database object. Default is True. |
Source code in database/macrostrat/database/core.py
170 171 172 173 174 175 176 177 178 179 180 181 182 | |
run_query(sql, params=None, **kwargs)
Run a single query on the database object, returning the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
SQL file or query to execute. |
required |
params
|
dict
|
Parameters to pass to the query. |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
use_instance_params |
bool
|
If True, will use the instance_params set on the Database object. Default is True. |
Source code in database/macrostrat/database/core.py
156 157 158 159 160 161 162 163 164 165 166 167 168 | |
run_sql(fn, params=None, **kwargs)
Executes SQL files or query strings using the run_sql function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
str | Path
|
SQL file or query string to execute. |
required |
params
|
dict
|
Parameters to pass to the query. |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
use_instance_params |
bool
|
If True, will use the instance_params set on the Database object. Default is True. |
Returns: Iterator of results from the query.
Source code in database/macrostrat/database/core.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
savepoint(name=None, rollback='on-error', connection=None)
A PostgreSQL-specific savepoint context manager. This is similar to the
transaction context manager but uses savepoints directly for simpler operation.
Notably, it supports nested savepoints, a feature that is difficult in SQLAlchemy's transaction
model.
This function is not yet drop-in compatible with the transaction context manager, but that
is a future goal.
Source code in database/macrostrat/database/core.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
session_scope(commit=True)
Provide a transactional scope around a series of operations.
Source code in database/macrostrat/database/core.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
transaction(*, rollback='on-error', connection=None, raise_errors=True)
Create a database session that can be rolled back after use.
This is similar to the session_scope method but includes
more fine-grained control over transactions. The two methods may be integrated
in the future.
This is based on the Sparrow's implementation: https://github.com/EarthCubeGeochron/Sparrow/blob/main/backend/conftest.py
It can be effectively used in a Pytest fixture like so: ``` @fixture(scope="class") def db(base_db): with base_db.transaction(rollback=True): yield base_db
Source code in database/macrostrat/database/core.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |