Skip to content

Commit

Permalink
Introduce sqlalchemy
Browse files Browse the repository at this point in the history
* includes alembic migrations
* decided to rename the "ui" package to "web"
* prepare first migration and model (system)
* extend devenv to be helpful and easy to interact with (scripts)

Re ARAMAKI-9
  • Loading branch information
ctheune committed Feb 2, 2024
1 parent 77c27c9 commit 96f8686
Showing 22 changed files with 346 additions and 74 deletions.
4 changes: 4 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Database

* [ ] Names of new relations use uninflected (singular) form.
2 changes: 1 addition & 1 deletion development.ini
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ pyramid.default_locale_name = de
pyramid.includes =
pyramid_debugtoolbar

sqlalchemy.url = aramaki:///%(here)s/aramaki.sqlite
sqlalchemy.url = postgresql://localhost/aramaki?host=%(here)s/.devenv/state/postgres

retry.attempts = 3

38 changes: 31 additions & 7 deletions devenv.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{ pkgs, ... }:
{ pkgs, lib, config, ... }:

{
env.PYTHONUNBUFFERED = "true";
env.ALEMBIC_CONFIG = "development.ini";
env.PGDATABASE = "aramaki";

packages = [
pkgs.git
@@ -22,14 +24,36 @@
};
};

scripts.db-revision.exec = ''
alembic revision --autogenerate -m "''${1?need revision comment}"
'';

scripts.db-upgrade.exec = ''
alembic upgrade head
'';

scripts.aramaki-web.exec = ''
pserve development.ini
'';

enterShell = ''
echo
echo 🦾 Helper scripts you can run to make your development richer:
echo 🦾
${pkgs.gnused}/bin/sed -e 's| |••|g' -e 's|=| |' <<EOF | ${pkgs.util-linuxMinimal}/bin/column -t | ${pkgs.gnused}/bin/sed -e 's|^|🦾 |' -e 's|••| |g'
${lib.generators.toKeyValue {} (lib.mapAttrs (name: value: value.description) config.scripts)}
EOF
echo
'';

processes = {
aramaki-server-ui.exec = "pserve development.ini";
#aramaki-server-processing.exec = "aramaki-server processing";
#aramaki-server-federation.exec = "aramaki-server federation";
#aramaki-server-agent-manager.exec = "aramaki-server agent-manager";
# aramaki-agent.exec = "aramaki-agent";
# aramaki-server-web.exec = "pserve development.ini";
# aramaki-server-processing.exec = "aramaki-server processing";
# aramaki-server-federation.exec = "aramaki-server federation";
# aramaki-server-agent-manager.exec = "aramaki-server agent-manager";
# aramaki-agent.exec = "aramaki-agent";

tailwindcss.exec = "cd tailwind; (while true; do sleep 10; done) | tailwindcss -i aramaki.css -o ../src/aramaki/server/ui/static/aramaki.css --minify --watch";
tailwindcss.exec = "cd tailwind; (while true; do sleep 10; done) | tailwindcss -i aramaki.css -o ../src/aramaki/server/web/static/aramaki.css --minify --watch";
};

services.postgres = {
47 changes: 4 additions & 43 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -17,37 +17,37 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.11"

alembic = "^1.11.1"
arrow = "^1.3.0"
babel = "^2.12.1"
cachecontrol = "^0.13.1"
filelock = "^3.13.1"
plaster-pastedeploy = "^1.0.1"
psycopg2-binary = "^2.9.9"
pyramid = "^2.0.1"
pyramid-chameleon = "^0.3"
pyramid-debugtoolbar = "^4.10"
waitress = "^2.1.2"
plaster-pastedeploy = "^1.0.1"
alembic = "^1.11.1"
pyramid-retry = "^2.1.1"
pyramid-tm = "^2.5"
pytz = "^2023.3"
sqlalchemy = "^2.0.19"
transaction = "^3.1.0"
transaction = { version = "^3.1.0", extras = ["asyncio"] }
waitress = "^2.1.2"
websockets = "^12"
zope-sqlalchemy = "^3.0"
babel = "^2.12.1"
pytz = "^2023.3"

# peppercorn = "^0.6"
peppercorn = { git = "https://github.com/ctheune/peppercorn.git", branch = "htmx-workaround-1686-wrong-form-dataset-construction" }
rtmapi = "^0.7.2"
# implicit dependency that rtmapi did not specify!?!
httplib2 = "^0.22.0"
psycopg2-binary = "^2.9.9"
cachecontrol = "^0.13.1"
arrow = "^1.3.0"
filelock = "^3.13.1"
websockets = "^12"
peppercorn = {git = "https://github.com/ctheune/peppercorn.git", branch = "htmx-workaround-1686-wrong-form-dataset-construction" }


[tool.poetry.group.dev.dependencies]
webtest = "^3.0.0"
pytest = "^7.4.0"
pytest-cov = "^4.1.0"

[tool.poetry.plugins."paste.app_factory"]
main = "aramaki.server.ui.main:main"
main = "aramaki.server.web.main:main"

[tool.poetry.plugins."console_scripts"]
aramaki-initialize-db = "aramaki.scripts.initialize_db:main"
55 changes: 55 additions & 0 deletions src/aramaki/server/alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Pyramid bootstrap environment. """
from alembic import context
from pyramid.paster import get_appsettings, setup_logging
from sqlalchemy import engine_from_config

from aramaki.server.models.meta import Base

config = context.config

setup_logging(config.config_file_name)

settings = get_appsettings(config.config_file_name)
target_metadata = Base.metadata


def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
context.configure(url=settings["sqlalchemy.url"])
with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = engine_from_config(settings, prefix="sqlalchemy.")

connection = engine.connect()
context.configure(connection=connection, target_metadata=target_metadata)

try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
22 changes: 22 additions & 0 deletions src/aramaki/server/alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}

def upgrade():
${upgrades if upgrades else "pass"}

def downgrade():
${downgrades if downgrades else "pass"}
38 changes: 38 additions & 0 deletions src/aramaki/server/alembic/versions/20240202_d50e144e683a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""create system table
Revision ID: d50e144e683a
Revises:
Create Date: 2024-02-02 16:49:39.667043
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "d50e144e683a"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"system",
sa.Column(
"id",
sa.UUID(),
server_default=sa.text("gen_random_uuid()"),
nullable=False,
),
sa.Column("title", sa.Text(), nullable=True),
sa.Column("primary_instance", sa.UUID(), nullable=True),
sa.PrimaryKeyConstraint("id", name=op.f("pk_system")),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("system")
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions src/aramaki/server/alembic/versions/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Placeholder for alembic versions
Loading
Oops, something went wrong.

0 comments on commit 96f8686

Please sign in to comment.