Skip to content

Commit 0519e8b

Browse files
authored
Merge pull request #52 from bit-bots/feature/alembic-db-migrations
Alembic DB migrations for NAO elbow joint
2 parents d4fe368 + 2036012 commit 0519e8b

File tree

11 files changed

+1032
-528
lines changed

11 files changed

+1032
-528
lines changed

.github/actions/setup-repo/action.yml

+17-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@ inputs:
44
python-version:
55
description: "Python version to use"
66
required: true
7-
default: "3.x"
87
runs:
98
using: "composite"
109
steps:
11-
- name: Install poetry
12-
run: pipx install poetry
13-
shell: bash
14-
1510
- name: Set up Python
1611
uses: actions/setup-python@v5
1712
with:
1813
python-version: ${{ inputs.python-version }}
19-
cache: "poetry"
14+
cache: "pip"
15+
16+
- name: Install poetry with pipx
17+
run: |
18+
python -m pip install --upgrade pipx
19+
python -m pipx ensurepath
20+
pipx install poetry
21+
22+
poetry config virtualenvs.create true --local
23+
poetry config virtualenvs.in-project true --local
24+
shell: bash
25+
26+
- name: Define a cache for poetry dependencies
27+
uses: actions/cache@v4
28+
with:
29+
path: .venv
30+
key: ${{ runner.os }}-${{ inputs.python-version }}-venv-${{ hashFiles('poetry.lock') }}
2031

2132
- name: Install dependencies
2233
run: poetry install

.github/workflows/test-dataset.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ jobs:
2929
with:
3030
python-version: ${{ matrix.python-version }}
3131

32-
- name: Create DB schema
33-
run: poetry run cli db create-schema
32+
- name: Setup DB with alembic migrations
33+
run: |
34+
cd ./ddlitlab2024/dataset
35+
poetry run alembic upgrade head
3436
3537
- name: Populate DB with dummy data
3638
run: |
37-
poetry run cli db create-schema
3839
poetry run cli db dummy-data -n 2 -s 100 -i 10
3940
4041
- name: Test rosbag import with invalid training data fails
@@ -52,7 +53,6 @@ jobs:
5253
exit 1
5354
fi
5455
55-
5656
- name: Test rosbag import with valid training data
5757
run: |
5858
curl -fsSLO https://data.bit-bots.de/ROSbags/robocup_2024/ID_donna_2024-07-20T15%3A49%3A20/ID_donna_2024-07-20T15%3A49%3A20_0.mcap

ddlitlab2024/dataset/alembic.ini

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
# Use forward slashes (/) also on windows to provide an os agnostic path
6+
script_location = migrations
7+
8+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
9+
# Uncomment the line below if you want the files to be prepended with date and time
10+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
11+
# for all available tokens
12+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
13+
14+
# sys.path path, will be prepended to sys.path if present.
15+
# defaults to the current working directory.
16+
prepend_sys_path = .
17+
18+
# timezone to use when rendering the date within the migration file
19+
# as well as the filename.
20+
# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.
21+
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
22+
# string value is passed to ZoneInfo()
23+
# leave blank for localtime
24+
# timezone =
25+
26+
# max length of characters to apply to the "slug" field
27+
# truncate_slug_length = 40
28+
29+
# set to 'true' to run the environment during
30+
# the 'revision' command, regardless of autogenerate
31+
# revision_environment = false
32+
33+
# set to 'true' to allow .pyc and .pyo files without
34+
# a source .py file to be detected as revisions in the
35+
# versions/ directory
36+
# sourceless = false
37+
38+
# version location specification; This defaults
39+
# to migrations/versions. When using multiple version
40+
# directories, initial revisions must be specified with --version-path.
41+
# The path separator used here should be the separator specified by "version_path_separator" below.
42+
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
43+
44+
# version path separator; As mentioned above, this is the character used to split
45+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47+
# Valid values for version_path_separator are:
48+
#
49+
# version_path_separator = :
50+
# version_path_separator = ;
51+
# version_path_separator = space
52+
# version_path_separator = newline
53+
#
54+
# Use os.pathsep. Default configuration used for new projects.
55+
version_path_separator = os
56+
57+
# set to 'true' to search source files recursively
58+
# in each "version_locations" directory
59+
# new in Alembic version 1.10
60+
# recursive_version_locations = false
61+
62+
# the output encoding used when revision files
63+
# are written from script.py.mako
64+
# output_encoding = utf-8
65+
66+
# sqlalchemy.url = driver://user:pass@localhost/dbname
67+
68+
69+
[post_write_hooks]
70+
# post_write_hooks defines scripts or Python functions that are run
71+
# on newly generated revision scripts. See the documentation for further
72+
# detail and examples
73+
74+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
75+
# hooks = black
76+
# black.type = console_scripts
77+
# black.entrypoint = black
78+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
79+
80+
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
81+
# hooks = ruff
82+
# ruff.type = exec
83+
# ruff.executable = %(here)s/.venv/bin/ruff
84+
# ruff.options = --fix REVISION_SCRIPT_FILENAME
85+
86+
# Logging configuration
87+
[loggers]
88+
keys = root,sqlalchemy,alembic
89+
90+
[handlers]
91+
keys = console
92+
93+
[formatters]
94+
keys = generic
95+
96+
[logger_root]
97+
level = WARNING
98+
handlers = console
99+
qualname =
100+
101+
[logger_sqlalchemy]
102+
level = WARNING
103+
handlers =
104+
qualname = sqlalchemy.engine
105+
106+
[logger_alembic]
107+
level = INFO
108+
handlers =
109+
qualname = alembic
110+
111+
[handler_console]
112+
class = StreamHandler
113+
args = (sys.stderr,)
114+
level = NOTSET
115+
formatter = generic
116+
117+
[formatter_generic]
118+
format = %(levelname)-5.5s [%(name)s] %(message)s
119+
datefmt = %H:%M:%S
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from logging.config import fileConfig
2+
3+
from alembic import context
4+
from sqlalchemy import engine_from_config, pool
5+
6+
from ddlitlab2024 import DB_PATH
7+
from ddlitlab2024.dataset import models
8+
9+
# this is the Alembic Config object, which provides
10+
# access to the values within the .ini file in use.
11+
config = context.config
12+
13+
# Interpret the config file for Python logging.
14+
# This line sets up loggers basically.
15+
if config.config_file_name is not None:
16+
fileConfig(config.config_file_name)
17+
18+
# add your model's MetaData object here
19+
# for 'autogenerate' support
20+
# from myapp import mymodel
21+
# target_metadata = mymodel.Base.metadata
22+
target_metadata = models.Base.metadata
23+
24+
# other values from the config, defined by the needs of env.py,
25+
# can be acquired:
26+
# my_important_option = config.get_main_option("my_important_option")
27+
# ... etc.
28+
config.set_main_option("sqlalchemy.url", f"sqlite:///{DB_PATH}")
29+
30+
31+
def run_migrations_offline() -> None:
32+
"""Run migrations in 'offline' mode.
33+
34+
This configures the context with just a URL
35+
and not an Engine, though an Engine is acceptable
36+
here as well. By skipping the Engine creation
37+
we don't even need a DBAPI to be available.
38+
39+
Calls to context.execute() here emit the given string to the
40+
script output.
41+
42+
"""
43+
url = config.get_main_option("sqlalchemy.url")
44+
context.configure(
45+
url=url,
46+
target_metadata=target_metadata,
47+
literal_binds=True,
48+
dialect_opts={"paramstyle": "named"},
49+
)
50+
51+
with context.begin_transaction():
52+
context.run_migrations()
53+
54+
55+
def run_migrations_online() -> None:
56+
"""Run migrations in 'online' mode.
57+
58+
In this scenario we need to create an Engine
59+
and associate a connection with the context.
60+
61+
"""
62+
connectable = engine_from_config(
63+
config.get_section(config.config_ini_section, {}),
64+
prefix="sqlalchemy.",
65+
poolclass=pool.NullPool,
66+
)
67+
68+
with connectable.connect() as connection:
69+
context.configure(connection=connection, target_metadata=target_metadata)
70+
71+
with context.begin_transaction():
72+
context.run_migrations()
73+
74+
75+
if context.is_offline_mode():
76+
run_migrations_offline()
77+
else:
78+
run_migrations_online()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
${imports if imports else ""}
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = ${repr(up_revision)}
16+
down_revision: Union[str, None] = ${repr(down_revision)}
17+
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
18+
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
19+
20+
21+
def upgrade() -> None:
22+
${upgrades if upgrades else "pass"}
23+
24+
25+
def downgrade() -> None:
26+
${downgrades if downgrades else "pass"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Add NAO elbow joint
2+
3+
Revision ID: 14ae0e795470
4+
Revises: 3f1574e89695
5+
Create Date: 2025-01-23 17:01:45.686722
6+
7+
"""
8+
9+
from collections.abc import Sequence
10+
from typing import Union
11+
12+
import sqlalchemy as sa
13+
from alembic import op
14+
15+
# revision identifiers, used by Alembic.
16+
revision: str = "14ae0e795470"
17+
down_revision: Union[str, None] = "3f1574e89695"
18+
branch_labels: Union[str, Sequence[str], None] = None
19+
depends_on: Union[str, Sequence[str], None] = None
20+
21+
22+
def upgrade() -> None:
23+
# ### commands auto generated by Alembic - please adjust! ###
24+
with op.batch_alter_table("JointCommands") as batch_op:
25+
batch_op.add_column(sa.Column("RElbowYaw", sa.Float(), nullable=False))
26+
batch_op.create_check_constraint(
27+
op.f("ck_JointCommands_RElbowYaw_value"), "RElbowYaw >= 0 AND RElbowYaw < 2 * pi()"
28+
)
29+
30+
batch_op.add_column(sa.Column("LElbowYaw", sa.Float(), nullable=False))
31+
batch_op.create_check_constraint(
32+
op.f("ck_JointCommands_LElbowYaw_value"), "LElbowYaw >= 0 AND LElbowYaw < 2 * pi()"
33+
)
34+
35+
with op.batch_alter_table("JointStates") as batch_op:
36+
batch_op.add_column(sa.Column("RElbowYaw", sa.Float(), nullable=False))
37+
batch_op.create_check_constraint(
38+
op.f("ck_JointStates_LElbowYaw_value"), "LElbowYaw >= 0 AND LElbowYaw < 2 * pi()"
39+
)
40+
41+
batch_op.add_column(sa.Column("LElbowYaw", sa.Float(), nullable=False))
42+
batch_op.create_check_constraint(
43+
op.f("ck_JointStates_RElbowYaw_value"), "RElbowYaw >= 0 AND RElbowYaw < 2 * pi()"
44+
)
45+
# ### end Alembic commands ###
46+
47+
48+
def downgrade() -> None:
49+
# ### commands auto generated by Alembic - please adjust! ###
50+
with op.batch_alter_table("JointCommands") as batch_op:
51+
batch_op.drop_constraint("ck_JointCommands_RElbowYaw_value")
52+
batch_op.drop_column("RElbowYaw")
53+
54+
batch_op.drop_constraint("ck_JointCommands_LElbowYaw_value")
55+
batch_op.drop_column("LElbowYaw")
56+
57+
with op.batch_alter_table("JointStates") as batch_op:
58+
batch_op.drop_constraint("ck_JointStates_RElbowYaw_value")
59+
batch_op.drop_column("RElbowYaw")
60+
61+
batch_op.drop_constraint("ck_JointStates_LElbowYaw_value")
62+
batch_op.drop_column("LElbowYaw")
63+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)