Skip to content

Commit

Permalink
[Done] Initial migration + migrate friction type 4 to 2 (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw authored Oct 4, 2021
1 parent 7fb5b89 commit 625c2c3
Show file tree
Hide file tree
Showing 12 changed files with 1,044 additions and 85 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Changelog of threedi-modelchecker

- Removed the summarize (--sum, --no-sum) option from the command-line interface.

- Complete run through of the checks.

- Added an initial migration (0200) that adds the tables only when necessary. In
this way, empty and existing sqlite files can be initialized.

- Added a migration (0201) that replaces friction_type=4 with 2.


0.14 (2021-07-29)
-----------------
Expand Down
12 changes: 11 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ Command-line interface

Use the modelchecker from the command line as follows::

threedi-modelchecker -l warning sqlite -s path/to/model.sqlite
threedi_modelchecker -s path/to/model.sqlite check -l warning

By default, WARNING and INFO checks are ignored.


Migrations
----------

Migrate the schematisation file to the latest version as follows::

threedi_modelchecker -s path/to/model.sqlite migrate

The file will be change in-place.


Development
-----------

Expand Down
91 changes: 91 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = threedi_modelchecker/migrations


version_table = schema_version
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .

# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =

# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions

# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = sqlite:///


[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
python_requires='>=3.6',
entry_points={
"console_scripts": [
"threedi-modelchecker = threedi_modelchecker.scripts:check_model"
"threedi_modelchecker = threedi_modelchecker.scripts:threedi_modelchecker"
]
},
)
2 changes: 1 addition & 1 deletion tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_upgrade_with_preexisting_version(in_memory_sqlite):
schema = ModelSchema(in_memory_sqlite)

with mock.patch.object(schema, "get_version", return_value=199):
schema.upgrade(backup=False)
schema.upgrade(backup=False, revision="0201")

assert in_memory_sqlite.get_engine().has_table("v2_connection_nodes")

Expand Down
15 changes: 15 additions & 0 deletions threedi_modelchecker/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
from alembic import context
from sqlalchemy import create_engine
from threedi_modelchecker.threedi_model.models import Base

import os
import threedi_modelchecker.threedi_model.models # NOQA needed for autogenerate


target_metadata = Base.metadata
config = context.config


def get_url():
db_url = os.environ.get("DB_URL")
if not db_url:
raise RuntimeError(
"Database URL must be specified using the environment variable DB_URL"
)
return db_url


def run_migrations_online():
"""Run migrations in 'online' mode.
Note: SQLite does not (completely) support transactions, so, backup the
SQLite before running migrations.
"""
connectable = config.attributes.get("connection")
if connectable is None:
connectable = create_engine(get_url())

with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
Expand Down
27 changes: 0 additions & 27 deletions threedi_modelchecker/migrations/versions/0001_initial.py

This file was deleted.

Loading

0 comments on commit 625c2c3

Please sign in to comment.