-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: auto-downgrading revision #4678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
korolenkowork
wants to merge
123
commits into
keephq:main
Choose a base branch
from
korolenkowork:feature/auto-downgrade-revision
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
123 commits
Select commit
Hold shift + click to select a range
408e7fc
first version
korolenkowork dce47b0
fix
korolenkowork 6127d86
Main logic completed
korolenkowork 8cb9b81
minor
korolenkowork 9cc98a8
Update configuration.mdx
korolenkowork 2758d9f
lint fix
korolenkowork e1ee04b
Add test
korolenkowork 51787ba
fix path
korolenkowork db174f9
fix
korolenkowork 9b2f67f
migrations-e2e-prepare-for-coding
korolenkowork 3fb2996
migrations-e2e
korolenkowork 08db877
Done!
korolenkowork 4f635f3
fix pull old version e2e
korolenkowork f140de4
fix
korolenkowork 995fd83
Update run-migrations-e2e-tests.yml
korolenkowork 1d1e462
Update run-migrations-e2e-tests.yml
korolenkowork 5dd3bfa
fix
korolenkowork cb46278
Update test-pr-e2e.yml
korolenkowork bf1b653
Update run-migrations-e2e-tests.yml
korolenkowork f469bab
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork 3e3975c
Check revision between runs
korolenkowork 4671d62
Merge branch 'feature/auto-downgrade-revision' of https://github.com/β¦
korolenkowork c55103a
fix execute container name
korolenkowork 83b748d
test
korolenkowork f3ee8e3
test
korolenkowork 9036754
test
korolenkowork e185781
test
korolenkowork 33fbe21
test
korolenkowork 3fcdfcb
Update run-migrations-e2e-tests.yml
korolenkowork b2beede
Update run-migrations-e2e-tests.yml
korolenkowork e4dd78a
Update run-migrations-e2e-tests.yml
korolenkowork 8fd2137
revert
korolenkowork 9545c5a
Update run-migrations-e2e-tests.yml
korolenkowork 87d28d3
Update run-migrations-e2e-tests.yml
korolenkowork a2a1260
fix e2e env
korolenkowork cf0d105
Update run-migrations-e2e-tests.yml
korolenkowork 1d34408
Update run-migrations-e2e-tests.yml
korolenkowork ff4b2e9
I hate pipelines
korolenkowork 303e63c
Done!
korolenkowork 394fd03
Add dummy migrations to the second step
korolenkowork a3a2735
Update run-migrations-e2e-tests.yml
korolenkowork fd45db1
Update run-migrations-e2e-tests.yml
korolenkowork 4c96bcc
Fix docker cp path
korolenkowork b3a1190
Update run-migrations-e2e-tests.yml
korolenkowork 197c98a
Update run-migrations-e2e-tests.yml
korolenkowork a38facc
Merge branch 'main' into feature/auto-downgrade-revision
shahargl 8702a2b
Update run-migrations-e2e-tests.yml
korolenkowork 0e208ef
Merge branch 'feature/auto-downgrade-revision' of https://github.com/β¦
korolenkowork 32f2cbe
fix workflow
korolenkowork 9d7ea1f
Remove test alembic config
korolenkowork b05a727
fix
korolenkowork 099d9ea
fix
korolenkowork 755f143
fix
korolenkowork 8af5a68
fix
korolenkowork f83c3f0
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork c29ef1b
fix?
korolenkowork 753f899
fix?
korolenkowork 1cdc291
change SECRET_MANAGER_DIRECTORY
korolenkowork 9d9791e
Delete docker-compose-e2e-postgres.yml~
korolenkowork beb812e
Revert "change SECRET_MANAGER_DIRECTORY"
korolenkowork 5136a03
fix
korolenkowork 2b3693a
Revert "fix"
korolenkowork 775322f
fix
korolenkowork ef08091
fix
korolenkowork f1072e8
fix?
korolenkowork fa83f0f
revert
korolenkowork c24254c
fix
korolenkowork f40ed3c
test
korolenkowork 6b9d5a0
test
korolenkowork 0be6341
try
korolenkowork 5224b8c
test
korolenkowork 0870a1b
fix
korolenkowork a8d1acc
Revert "test"
korolenkowork e1f4a30
Update docker-compose-e2e-postgres.yml
korolenkowork 8ced224
fix
korolenkowork 42a362a
Update docker-compose-e2e-postgres.yml
korolenkowork 1211b91
Mock final step migrations
korolenkowork c1e648f
Update poetry.lock
korolenkowork 99be2fa
Update run-migrations-e2e-tests.yml
korolenkowork 5e1e409
fix
korolenkowork 7a405b8
Update run-migrations-e2e-tests.yml
korolenkowork fea2ead
Update run-migrations-e2e-tests.yml
korolenkowork ea6746e
Update run-migrations-e2e-tests.yml
korolenkowork 4fdb76e
try
korolenkowork d458eac
test
korolenkowork 4c79efe
Update docker-compose-e2e-postgres.yml
korolenkowork 1d2716c
minor changes in copy migrations logic
korolenkowork 4adea39
Merge branch 'feature/auto-downgrade-revision' of https://github.com/β¦
korolenkowork e645a5e
fix
korolenkowork a1ffa55
test
korolenkowork f4d8ea8
fix
korolenkowork 84ce798
Update docker-compose-e2e-postgres.yml
korolenkowork 36051a0
Update docker-compose-e2e-postgres.yml
korolenkowork d41c554
Update docker-compose-e2e-postgres.yml
korolenkowork 68416f8
Update docker-compose-e2e-postgres.yml
korolenkowork 4a6039e
stop instead of down
korolenkowork 9eeaddd
Update docker-compose-e2e-postgres.yml
korolenkowork 24200a7
final
korolenkowork e7ea523
final
korolenkowork 70325a0
fix
korolenkowork 1725ae2
fix test script path
korolenkowork 166abba
fix
korolenkowork 70808cc
fix
korolenkowork 89868ba
Ahhhh
korolenkowork 4faabfb
Merge branch 'main' into feature/auto-downgrade-revision
shahargl e1e1dad
Post review fixes
korolenkowork 40a8340
Merge branch 'feature/auto-downgrade-revision' of https://github.com/β¦
korolenkowork ad148d8
fix
korolenkowork 7afb934
Update docker-compose-e2e-postgres.yml
korolenkowork 3f1f88a
Merge branch 'main' into feature/auto-downgrade-revision
shahargl 49df674
fix
korolenkowork 077ba69
Merge branch 'feature/auto-downgrade-revision' of https://github.com/β¦
korolenkowork ed91ff4
Add migration path to dockerfiles
korolenkowork f1328ef
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork bc5ebce
Merge branch 'main' into feature/auto-downgrade-revision
shahargl 02ca8bf
Merge branch 'main' into feature/auto-downgrade-revision
shahargl db76ae7
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork c05e35e
Make separate docker compose for migrations e2e
korolenkowork aea9b81
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork 356ce18
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork 13fe761
Update poetry.lock
korolenkowork 96d9479
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork 71e81be
Merge branch 'main' into feature/auto-downgrade-revision
korolenkowork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
[alembic] | ||
korolenkowork marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Re-defined in the keep/api/core/db_on_start.py to make it stable while keep is installed as a package | ||
script_location = keep/api/models/db/migrations | ||
file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(hour).2d-%%(minute).2d_%%(rev)s | ||
prepend_sys_path = . | ||
output_encoding = utf-8 | ||
|
||
|
||
[post_write_hooks] | ||
hooks = black,isort | ||
|
||
black.type = console_scripts | ||
black.entrypoint = black | ||
|
||
isort.type = console_scripts | ||
isort.entrypoint = isort | ||
|
||
# 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 [PID %(process)d] [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import asyncio | ||
korolenkowork marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import os | ||
from logging.config import fileConfig | ||
|
||
from alembic import context | ||
from alembic.script import ScriptDirectory | ||
from sqlalchemy import create_engine, StaticPool | ||
from sqlalchemy.future import Connection | ||
from sqlmodel import SQLModel | ||
|
||
import keep.api.logging | ||
from keep.api.core.db_utils import create_db_engine | ||
from keep.api.models.db.action import * | ||
from keep.api.models.db.ai_suggestion import * | ||
from keep.api.models.db.alert import * | ||
from keep.api.models.db.dashboard import * | ||
from keep.api.models.db.extraction import * | ||
from keep.api.models.db.facet import * | ||
from keep.api.models.db.maintenance_window import * | ||
from keep.api.models.db.mapping import * | ||
from keep.api.models.db.preset import * | ||
from keep.api.models.db.provider import * | ||
from keep.api.models.db.rule import * | ||
from keep.api.models.db.statistics import * | ||
from keep.api.models.db.tenant import * | ||
from keep.api.models.db.topology import * | ||
from keep.api.models.db.user import * | ||
from keep.api.models.db.workflow import * | ||
|
||
target_metadata = SQLModel.metadata | ||
|
||
# this is the Alembic Config object, which provides | ||
# access to the values within the .ini file in use. | ||
config = context.config | ||
|
||
# Interpret the config file for Python logging. | ||
# This line sets up loggers basically. | ||
if config.config_file_name is not None: | ||
# backup the current config | ||
logging_config = config.get_section("loggers") | ||
fileConfig(config.config_file_name) | ||
|
||
|
||
async 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. | ||
|
||
""" | ||
connectable = create_engine( | ||
"sqlite:///:memory:", | ||
connect_args={"check_same_thread": False}, | ||
poolclass=StaticPool, | ||
) | ||
context.configure( | ||
url=str(connectable.url), | ||
target_metadata=target_metadata, | ||
literal_binds=True, | ||
dialect_opts={"paramstyle": "named"}, | ||
render_as_batch=True, | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
def do_run_migrations(connection: Connection) -> None: | ||
""" | ||
Run actual sync migrations. | ||
|
||
:param connection: connection to the database. | ||
""" | ||
context.configure( | ||
connection=connection, target_metadata=target_metadata, render_as_batch=True | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
async 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 = create_db_engine() | ||
try: | ||
do_run_migrations(connectable.connect()) | ||
except Exception as e: | ||
# print all migrations so we will know what failed | ||
list_migrations(connectable) | ||
raise e | ||
|
||
|
||
def list_migrations(connectable): | ||
""" | ||
List all migrations and their status for debugging. | ||
""" | ||
try: | ||
# Get the script directory from the alembic context | ||
script_directory = ScriptDirectory.from_config(config) | ||
current_rev = script_directory.get_current_head() | ||
# List all available migrations | ||
pid = os.getpid() | ||
print(f"[{pid}] Available migrations:") | ||
try: | ||
for script in script_directory.walk_revisions(): | ||
status = ( | ||
"PENDING" | ||
if current_rev and script.revision > current_rev | ||
else "APPLIED" | ||
) | ||
print(f" - {script.revision}: {script.doc} ({status})") | ||
except Exception as exc: | ||
logger.exception(f"Failed to list migrations: {exc}") | ||
except Exception as exc: | ||
logger.exception(f"Failed to process migration information: {exc}") | ||
|
||
|
||
loop = asyncio.get_event_loop() | ||
if context.is_offline_mode(): | ||
task = run_migrations_offline() | ||
else: | ||
task = run_migrations_online() | ||
|
||
loop.run_until_complete(task) | ||
# SHAHAR: set back the logs to the default after alembic is done | ||
keep.api.logging.setup_logging() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""${message} | ||
|
||
Revision ID: ${up_revision} | ||
Revises: ${down_revision | comma,n} | ||
Create Date: ${create_date} | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel | ||
import sqlalchemy_utils | ||
|
||
${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() -> None: | ||
${upgrades if upgrades else "pass"} | ||
|
||
|
||
def downgrade() -> None: | ||
${downgrades if downgrades else "pass"} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.