-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from thread/label-updated
Label updated times
- Loading branch information
Showing
8 changed files
with
313 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
41 changes: 41 additions & 0 deletions
41
routemaster/migrations/versions/3eb4f3b419c6_create_trigger_to_sync_updated_field.py
This file contains 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,41 @@ | ||
""" | ||
create trigger to sync updated field | ||
Revision ID: 3eb4f3b419c6 | ||
Revises: 814a6b555eb9 | ||
""" | ||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '3eb4f3b419c6' | ||
down_revision = '814a6b555eb9' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.execute( | ||
''' | ||
CREATE OR REPLACE FUNCTION sync_label_updated_column_fn() | ||
RETURNS TRIGGER AS | ||
$$ | ||
BEGIN | ||
NEW.updated = now() AT TIME ZONE 'UTC'; | ||
RETURN NEW; | ||
END; | ||
$$ | ||
LANGUAGE PLPGSQL; | ||
CREATE TRIGGER sync_label_updated_column | ||
BEFORE UPDATE ON labels | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE sync_label_updated_column_fn(); | ||
''', | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.execute('DROP TRIGGER sync_label_updated_column') | ||
op.execute('DROP FUNCTION sync_label_updated_column_fn') |
150 changes: 150 additions & 0 deletions
150
routemaster/migrations/versions/814a6b555eb9_make_fields_non_nullable_by_default.py
This file contains 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,150 @@ | ||
""" | ||
make fields non-nullable by default | ||
Revision ID: 814a6b555eb9 | ||
Revises: e7d5ad06c0d1 | ||
""" | ||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '814a6b555eb9' | ||
down_revision = 'e7d5ad06c0d1' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.alter_column( | ||
'history', | ||
'label_name', | ||
existing_type=sa.VARCHAR(), | ||
nullable=False, | ||
) | ||
op.alter_column( | ||
'history', | ||
'label_state_machine', | ||
existing_type=sa.VARCHAR(), | ||
nullable=False, | ||
) | ||
|
||
def set_not_null(table, column, existing_type, server_default): | ||
op.alter_column( | ||
table, | ||
column, | ||
existing_type=existing_type, | ||
server_default=server_default, | ||
) | ||
op.execute( | ||
f"UPDATE {table} SET {column} = {server_default} " | ||
f"WHERE {column} IS NULL", | ||
) | ||
op.alter_column( | ||
table, | ||
column, | ||
existing_type=existing_type, | ||
nullable=False, | ||
) | ||
|
||
set_not_null( | ||
'history', | ||
'created', | ||
existing_type=postgresql.TIMESTAMP(), | ||
server_default=sa.func.now(), | ||
) | ||
set_not_null( | ||
'history', | ||
'forced', | ||
existing_type=sa.BOOLEAN(), | ||
server_default=sa.false(), | ||
) | ||
set_not_null( | ||
'labels', | ||
'metadata', | ||
existing_type=postgresql.JSONB(astext_type=sa.Text()), | ||
server_default=sa.text("'{}'::json"), # noqa | ||
) | ||
set_not_null( | ||
'labels', | ||
'metadata_triggers_processed', | ||
existing_type=sa.BOOLEAN(), | ||
server_default=sa.true(), | ||
) | ||
set_not_null( | ||
'state_machines', | ||
'updated', | ||
existing_type=postgresql.TIMESTAMP(), | ||
server_default=sa.func.now(), | ||
) | ||
set_not_null( | ||
'states', | ||
'deprecated', | ||
existing_type=sa.BOOLEAN(), | ||
server_default=sa.false(), | ||
) | ||
set_not_null( | ||
'states', | ||
'updated', | ||
existing_type=postgresql.TIMESTAMP(), | ||
server_default=sa.func.now(), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.alter_column( | ||
'states', | ||
'updated', | ||
existing_type=postgresql.TIMESTAMP(), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'states', | ||
'deprecated', | ||
existing_type=sa.BOOLEAN(), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'state_machines', | ||
'updated', | ||
existing_type=postgresql.TIMESTAMP(), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'labels', | ||
'metadata_triggers_processed', | ||
existing_type=sa.BOOLEAN(), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'labels', | ||
'metadata', | ||
existing_type=postgresql.JSONB(astext_type=sa.Text()), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'history', | ||
'label_state_machine', | ||
existing_type=sa.VARCHAR(), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'history', | ||
'label_name', | ||
existing_type=sa.VARCHAR(), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'history', | ||
'forced', | ||
existing_type=sa.BOOLEAN(), | ||
nullable=True, | ||
) | ||
op.alter_column( | ||
'history', | ||
'created', | ||
existing_type=postgresql.TIMESTAMP(), | ||
nullable=True, | ||
) |
31 changes: 31 additions & 0 deletions
31
routemaster/migrations/versions/e7d5ad06c0d1_add_updated_field_to_label.py
This file contains 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,31 @@ | ||
""" | ||
add updated field to label | ||
Revision ID: e7d5ad06c0d1 | ||
Revises: e1fec9622785 | ||
""" | ||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'e7d5ad06c0d1' | ||
down_revision = 'e1fec9622785' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.add_column( | ||
'labels', | ||
sa.Column( | ||
'updated', | ||
sa.DateTime(), | ||
nullable=False, | ||
server_default=sa.func.now(), | ||
), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column('labels', 'updated') |
This file contains 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