-
Notifications
You must be signed in to change notification settings - Fork 76
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
Create proper enums for job statuses #412
base: master
Are you sure you want to change the base?
Changes from all commits
41313cd
f4bae19
d028b53
1498a44
98621c7
e0ce4f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""add_jobstatus | ||
Revision ID: 84796bdc2f77 | ||
Revises: dbb81bd4d47f | ||
Create Date: 2024-10-01 08:09:42.808911 | ||
""" | ||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "84796bdc2f77" | ||
down_revision = "dbb81bd4d47f" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.execute( | ||
"CREATE TYPE jobstatus AS ENUM ('done', 'new', 'cancelled', 'removed', 'processing');" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to know every possible type for this migration. Just change every value out of scope to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (But good point that we need to handle this correctly) |
||
) | ||
op.execute( | ||
"ALTER TABLE job ALTER COLUMN status TYPE jobstatus USING status::text::jobstatus;" | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.execute( | ||
"ALTER TABLE job ALTER COLUMN status TYPE VARCHAR USING status::text;" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,84 +5,110 @@ | |
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy import inspect | ||
|
||
import sqlmodel | ||
|
||
from config import app_config | ||
|
||
revision = "cbbba858deb0" | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
DATABASE_URL = app_config.database.url | ||
engine = sa.create_engine(DATABASE_URL) | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? We should not change older migrations in any case. (Please also don't reformat them, to keep unnecessary git changes to minimum) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I understand. I did this because the alembic upgrade head command in an existing environment causes errors that the table exists. So I check beforehand if the table exists. |
||
"agentgroup", | ||
sa.Column("plugins_spec", sa.JSON(), nullable=True), | ||
sa.Column("active_plugins", sa.ARRAY(sa.String()), nullable=True), | ||
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column( | ||
"ursadb_url", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_table( | ||
"configentry", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"plugin", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("key", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column("value", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_table( | ||
"job", | ||
sa.Column("taints", sa.ARRAY(sa.String()), nullable=True), | ||
sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column( | ||
"status", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("error", sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column( | ||
"rule_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"rule_author", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"raw_yara", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("submitted", sa.Integer(), nullable=False), | ||
sa.Column("finished", sa.Integer(), nullable=True), | ||
sa.Column("files_limit", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"reference", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("files_processed", sa.Integer(), nullable=False), | ||
sa.Column("files_matched", sa.Integer(), nullable=False), | ||
sa.Column("files_in_progress", sa.Integer(), nullable=False), | ||
sa.Column("total_files", sa.Integer(), nullable=False), | ||
sa.Column("files_errored", sa.Integer(), nullable=False), | ||
sa.Column("datasets_left", sa.Integer(), nullable=False), | ||
sa.Column("total_datasets", sa.Integer(), nullable=False), | ||
sa.Column("agents_left", sa.Integer(), nullable=False), | ||
sa.Column("internal_id", sa.Integer(), nullable=False), | ||
sa.PrimaryKeyConstraint("internal_id"), | ||
) | ||
op.create_table( | ||
"match", | ||
sa.Column("meta", sa.JSON(), nullable=True), | ||
sa.Column("matches", sa.ARRAY(sa.String()), nullable=True), | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("job_id", sa.Integer(), nullable=False), | ||
sa.Column("file", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["job_id"], | ||
["job.internal_id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
if not inspect(engine).has_table("agentgroup"): | ||
op.create_table( | ||
"agentgroup", | ||
sa.Column("plugins_spec", sa.JSON(), nullable=True), | ||
sa.Column("active_plugins", sa.ARRAY(sa.String()), nullable=True), | ||
sa.Column( | ||
"name", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"ursadb_url", | ||
sqlmodel.sql.sqltypes.AutoString(), | ||
nullable=False, | ||
), | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
if not inspect(engine).has_table("configentry"): | ||
op.create_table( | ||
"configentry", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"plugin", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"key", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"value", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
if not inspect(engine).has_table("job"): | ||
op.create_table( | ||
"job", | ||
sa.Column("taints", sa.ARRAY(sa.String()), nullable=True), | ||
sa.Column( | ||
"id", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"status", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"error", sqlmodel.sql.sqltypes.AutoString(), nullable=True | ||
), | ||
sa.Column( | ||
"rule_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"rule_author", | ||
sqlmodel.sql.sqltypes.AutoString(), | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"raw_yara", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("submitted", sa.Integer(), nullable=False), | ||
sa.Column("finished", sa.Integer(), nullable=True), | ||
sa.Column("files_limit", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"reference", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("files_processed", sa.Integer(), nullable=False), | ||
sa.Column("files_matched", sa.Integer(), nullable=False), | ||
sa.Column("files_in_progress", sa.Integer(), nullable=False), | ||
sa.Column("total_files", sa.Integer(), nullable=False), | ||
sa.Column("files_errored", sa.Integer(), nullable=False), | ||
sa.Column("datasets_left", sa.Integer(), nullable=False), | ||
sa.Column("total_datasets", sa.Integer(), nullable=False), | ||
sa.Column("agents_left", sa.Integer(), nullable=False), | ||
sa.Column("internal_id", sa.Integer(), nullable=False), | ||
sa.PrimaryKeyConstraint("internal_id"), | ||
) | ||
if not inspect(engine).has_table("match"): | ||
op.create_table( | ||
"match", | ||
sa.Column("meta", sa.JSON(), nullable=True), | ||
sa.Column("matches", sa.ARRAY(sa.String()), nullable=True), | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("job_id", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"file", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.ForeignKeyConstraint( | ||
["job_id"], | ||
["job.internal_id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,34 +5,41 @@ | |
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy import inspect | ||
|
||
|
||
from config import app_config | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "dbb81bd4d47f" | ||
down_revision = "cbbba858deb0" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
DATABASE_URL = app_config.database.url | ||
engine = sa.create_engine(DATABASE_URL) | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"jobagent", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly, please don't change and reformat this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("task_in_progress", sa.Integer(), nullable=False), | ||
sa.Column("job_id", sa.Integer(), nullable=False), | ||
sa.Column("agent_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["agent_id"], | ||
["agentgroup.id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["job_id"], | ||
["job.internal_id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
# ### end Alembic commands ### | ||
if not inspect(engine).has_table("jobagent"): | ||
op.create_table( | ||
"jobagent", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("task_in_progress", sa.Integer(), nullable=False), | ||
sa.Column("job_id", sa.Integer(), nullable=False), | ||
sa.Column("agent_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["agent_id"], | ||
["agentgroup.id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["job_id"], | ||
["job.internal_id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this. We want to reduce the number of additional commands to zero (#402), not add more of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't want to fix this in this PR and want to work separately in #402, it's OK to leave it as it is now (i.e. don't change the documentation in this PR. It works for fresh installations, just doesn't run migrations automatically).
If you want, I'll add some hints to #402 how to approach it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All because of alembic, which does not automatically recognize the modification from varchar to enum. I am able to create an enum type in postgres from the model level, but alembic does not replace it (it still remains VARCHAR) - that's why my manual modifications in migration files.
I found a library https://pypi.org/project/alembic-postgresql-enum/, quite new, I have to test it, but the question is whether to enter something like that at all. Or maybe look for solutions in sqlalchemy dialects for postgresql