Compare commits

...

10 commits

Author SHA1 Message Date
e2850da787
creation projection workinggg 2025-06-15 00:56:00 +02:00
35f551949e
add table 2025-06-15 00:14:12 +02:00
6c383e5dd7
typo 2025-06-15 00:14:03 +02:00
0e387f3c06
moar events 2025-06-14 23:56:13 +02:00
f11c18f012
add events 2025-06-14 23:42:25 +02:00
7a5312c607
create table migration 2025-06-14 22:55:29 +02:00
49d6b510a5
python gitignore 2025-06-14 22:55:16 +02:00
7bd7e1f74f
black formatter 2025-06-14 22:43:32 +02:00
6d4d50a156
alembic 2025-06-14 22:25:40 +02:00
7ae943a5b5
db 2025-06-14 22:14:07 +02:00
13 changed files with 806 additions and 0 deletions

5
.env.dist Normal file
View file

@ -0,0 +1,5 @@
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=rollup
POSTGRES_USER=rollup
POSTGRES_PASSWORD=rollup

217
.gitignore vendored Normal file
View file

@ -0,0 +1,217 @@
.env
.venv/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[codz]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
#poetry.toml
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.envrc
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Abstra
# Abstra is an AI-powered process automation framework.
# Ignore directories containing user credentials, local state, and settings.
# Learn more at https://abstra.io/docs
.abstra/
# Visual Studio Code
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
# and can be added to the global gitignore or merged into this file. However, if you prefer,
# you could uncomment the following to ignore the entire vscode folder
# .vscode/
# Ruff stuff:
.ruff_cache/
# PyPI configuration file
.pypirc
# Cursor
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
# refer to https://docs.cursor.com/context/ignore-files
.cursorignore
.cursorindexingignore
# Marimo
marimo/_static/
marimo/_lsp/
__marimo__/

6
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,6 @@
{"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.experiments.enabled": false
}

141
alembic.ini Normal file
View file

@ -0,0 +1,141 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts.
# this is typically a path given in POSIX (e.g. forward slashes)
# format, relative to the token %(here)s which refers to the location of this
# ini file
script_location = %(here)s/alembic
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory. for multiple paths, the path separator
# is defined by "path_separator" below.
prepend_sys_path = .
# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
# string value is passed to ZoneInfo()
# 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 <script_location>/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "path_separator"
# below.
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
# path_separator; This indicates what character is used to split lists of file
# paths, including version_locations and prepend_sys_path within configparser
# files such as alembic.ini.
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
# to provide os-dependent path splitting.
#
# Note that in order to support legacy alembic.ini files, this default does NOT
# take place if path_separator is not present in alembic.ini. If this
# option is omitted entirely, fallback logic is as follows:
#
# 1. Parsing of the version_locations option falls back to using the legacy
# "version_path_separator" key, which if absent then falls back to the legacy
# behavior of splitting on spaces and/or commas.
# 2. Parsing of the prepend_sys_path option falls back to the legacy
# behavior of splitting on spaces, commas, or colons.
#
# Valid values for path_separator are:
#
# path_separator = :
# path_separator = ;
# path_separator = space
# path_separator = newline
#
# Use os.pathsep. Default configuration used for new projects.
path_separator = os
# set to 'true' to search source files recursively
# in each "version_locations" directory
# new in Alembic version 1.10
# recursive_version_locations = false
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
# database URL. This is consumed by the user-maintained env.py script only.
# other means of configuring database URLs may be customized within the env.py
# file.
sqlalchemy.url = driver://user:pass@localhost/dbname
[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
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
# hooks = ruff
# ruff.type = exec
# ruff.executable = %(here)s/.venv/bin/ruff
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
# Logging configuration. This is also consumed by the user-maintained
# env.py script only.
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARNING
handlers = console
qualname =
[logger_sqlalchemy]
level = WARNING
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

1
alembic/README Normal file
View file

@ -0,0 +1 @@
Generic single-database configuration.

85
alembic/env.py Normal file
View file

@ -0,0 +1,85 @@
import os
from logging.config import fileConfig
from dotenv import load_dotenv
from sqlalchemy import engine_from_config, pool
from alembic import context
load_dotenv()
db_url = f"postgresql://{os.getenv('POSTGRES_USER')}:{os.getenv('POSTGRES_PASSWORD')}@{os.getenv('POSTGRES_HOST')}:{os.getenv('POSTGRES_PORT')}/{os.getenv('POSTGRES_DB')}"
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option("sqlalchemy.url", db_url)
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""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.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

28
alembic/script.py.mako Normal file
View file

@ -0,0 +1,28 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}

View file

@ -0,0 +1,106 @@
"""add projection table
Revision ID: 394542f24f6c
Revises: abfbb3d96037
Create Date: 2025-06-14 23:57:39.301255
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "394542f24f6c"
down_revision: Union[str, None] = "abfbb3d96037"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
op.create_table(
"users",
sa.Column(
"id",
sa.UUID(as_uuid=True),
nullable=False,
),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("deleted_at", sa.DateTime(timezone=True)),
sa.Column("last_event_id", sa.UUID(as_uuid=True), nullable=False, unique=True),
sa.Column(
"last_event_sequence",
sa.Integer(),
nullable=False,
autoincrement=True,
unique=True,
),
sa.Column("name", sa.String(), nullable=False),
sa.Column("age", sa.Integer(), nullable=False),
sa.Column("hair_color", sa.String(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.execute(
"""
create or replace function fn_project_user_created(event_id uuid, event_sequence integer, event_timestamp timestamptz, event jsonb) returns uuid
security definer
language plpgsql as $$
declare result uuid;
begin
insert into users(
id,
created_at,
updated_at,
last_event_id,
last_event_sequence,
name,
age,
hair_color
)
values(
cast(event->'event_payload'->>'id' as UUID),
event_timestamp,
event_timestamp,
event_id,
event_sequence,
event->'event_payload'->>'name',
cast(event->'event_payload'->>'age' as INTEGER),
event->'event_payload'->>'hair_color'
)
returning id into result;
return result;
end;
$$;
create or replace function fn_trigger_user_created() returns trigger
security definer
language plpgsql
as $$
begin
perform fn_project_user_created(
new.id,
new.sequence,
new.created_at,
new.event_payload
);
return new;
end;
$$;
create trigger event_insert_user_created
after insert on user_events
for each row
when ((new.event_payload->>'event_type') = 'user_created')
execute procedure fn_trigger_user_created();
"""
)
def downgrade() -> None:
"""Downgrade schema."""
op.drop_table("users")

View file

@ -0,0 +1,49 @@
"""create user event table
Revision ID: abfbb3d96037
Revises:
Create Date: 2025-06-14 22:22:48.407239
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
import sqlalchemy.dialects.postgresql as psql
# revision identifiers, used by Alembic.
revision: str = "abfbb3d96037"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
op.execute("CREATE EXTENSION IF NOT EXISTS pgcrypto;")
op.create_table(
"user_events",
sa.Column(
"id",
sa.UUID(as_uuid=True),
nullable=False,
server_default=sa.text("gen_random_uuid()"),
),
sa.Column("sequence", sa.Integer(), nullable=False, autoincrement=True),
sa.Column("event_payload", psql.JSONB, nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("NOW()"),
),
sa.PrimaryKeyConstraint("id", "sequence"),
)
def downgrade() -> None:
"""Downgrade schema."""
op.drop_table("user_events")

147
create_events.py Normal file
View file

@ -0,0 +1,147 @@
import os
from dotenv import load_dotenv
import psycopg2
import uuid
import json
load_dotenv()
conn_params = {
"host": os.getenv("POSTGRES_HOST"),
"port": os.getenv("POSTGRES_PORT"),
"dbname": os.getenv("POSTGRES_DB"),
"user": os.getenv("POSTGRES_USER"),
"password": os.getenv("POSTGRES_PASSWORD"),
}
class User:
def __init__(self, cursor, name, age, hair_color):
self.cursor = cursor
self.id = uuid.uuid4()
self.name = name
self.age = age
self.hair_color = hair_color
self._fire_created_user_event()
def update_user_details(self, name=None, age=None, hair_color=None):
if name:
self.name = name
if age:
self.age = age
if hair_color:
self.hair_color = hair_color
self._fire_updated_user_event()
def delete_user(self):
self._fire_deleted_user_event()
def _fire_created_user_event(self):
event = CreateUserEvent(self)
self._fire_event(event)
def _fire_updated_user_event(self):
event = UpdateUserEvent(self)
self._fire_event(event)
def _fire_deleted_user_event(self):
event = DeleteUserEvent(self)
self._fire_event(event)
def _fire_event(self, event):
query = f"INSERT INTO user_events (event_payload) VALUES ('{event.to_json()}')"
self.cursor.execute(query)
class CreateUserEvent:
def __init__(self, user):
self._user = user
def to_json(self):
return json.dumps(
{
"event_type": "user_created",
"event_payload": {
"id": self._user.id,
"name": self._user.name,
"age": self._user.age,
"hair_color": self._user.hair_color,
},
},
default=str,
)
class UpdateUserEvent:
def __init__(self, user):
self._user = user
def to_json(self):
return json.dumps(
{
"event_type": "user_updated",
"event_payload": {
"id": self._user.id,
"name": self._user.name,
"age": self._user.age,
"hair_color": self._user.hair_color,
},
},
default=str,
)
class DeleteUserEvent:
def __init__(self, user):
self._user = user
def to_json(self):
return json.dumps(
{
"event_type": "user_deleted",
"event_payload": {"id": self._user.id},
},
default=str,
)
def main():
try:
# Connect to PostgreSQL
conn = psycopg2.connect(**conn_params)
cur = conn.cursor()
my_user = User(cursor=cur, name="John", age=95, hair_color="Gray")
my_user.update_user_details(age=96)
my_user.delete_user()
my_other_user = User(cursor=cur, name="Jane", age=3, hair_color="Fire Red")
my_other_user.update_user_details(age=15, hair_color="Golden Blonde")
my_other_user.update_user_details(age=35, hair_color="Touches of gray")
my_immutable_user = User(
cursor=cur, name="The Dude", age=33, hair_color="Muffin Orange"
)
# Commit the transaction
conn.commit()
print("Done")
except Exception as e:
print("Error:", e)
finally:
if cur:
cur.close()
if conn:
conn.close()
if __name__ == "__main__":
main()

11
docker-compose.yml Normal file
View file

@ -0,0 +1,11 @@
version: '3.8'
services:
postgres:
image: postgres:17
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
ports:
- "${POSTGRES_PORT}:${POSTGRES_PORT}"

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
alembic
python-dotenv
psycopg2-binary

7
run_show.sh Normal file
View file

@ -0,0 +1,7 @@
docker compose down
docker compose up -d
sleep 2s
echo "Running migrations"
alembic upgrade head
echo "Creating events"
python3 create_events.py