Skip to content

Commit

Permalink
360 sim fix validators create and update methods (#361)
Browse files Browse the repository at this point in the history
* chore: migrated the database validators stake field

* docs: improved migrations documentation

* refactor: improved the frontend validators form

* force digits input keys on number fields

* fix forceInteger prop

* switch back to true

* docs: updated steps for running db migrations

Co-authored-by: Agustín Díaz <[email protected]>

* docs: fixed items list number

---------

Co-authored-by: Den <[email protected]>
Co-authored-by: Agustín Díaz <[email protected]>
  • Loading branch information
3 people authored Aug 7, 2024
1 parent b354fe3 commit a8795af
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 16 deletions.
24 changes: 14 additions & 10 deletions backend/database_handler/migration/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
## Pre-requisites

1. set your virtual environment
1. `pip install -r requirements.txt`
### Window One
1. Launch the simulator

## User guide

1. Change the `model`
1. Set your current working directory to `backend/database_handler`
1. Run `alembic revision --autogenerate -m "migration name here"` to generate the migration file
1. Modify the migration file if needed
1. Apply the migration: we have different options here
- [preferred] If using `docker-compose`, run `docker-compose up database-migration --build`
### Window Two
1. Set your virtual environment
2. Pip install `pip install -r backend/database_handler/requirements.txt`
3. Update the "models" at `models.py` with your preferences
4. Set your current working directory to `backend/database_handler`
5. Run `alembic revision --autogenerate -m "migration name here"` to generate the migration file
6. Modify the migration file if needed
7. Apply the migration: we have different options here
- [preferred] If using `docker-compose`, run `docker compose up database-migration --build`
- Run `alembic upgrade head` to "manually" apply the migration

## Revert migrations
To revert the latest migration, run `alembic downgrade -1`

## Docs

- [Alembic](https://alembic.sqlalchemy.org/en/latest/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""changed validators stage property to int
Revision ID: d9ddc7436122
Revises: 02aa0c34a463
Create Date: 2024-08-06 14:26:15.737405
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "d9ddc7436122"
down_revision: Union[str, None] = "02aa0c34a463"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
connection = op.get_bind()

column_name = "stake"
id_column = "id"
table_name = "validators"

result = connection.execute(
sa.text(f"SELECT {id_column}, {column_name} FROM {table_name}")
)

for [id, value] in result:
int_value = int(value)
new_value = int_value if int_value > 0 else 1
print(f"Updating {table_name} {id} with `{new_value}`")
# Update the row
connection.execute(
sa.text(
f"UPDATE {table_name} SET {column_name} = :value WHERE {id_column} = :id"
),
{"value": new_value, id_column: id},
)

op.alter_column(
"validators",
"stake",
existing_type=sa.NUMERIC(),
type_=sa.Integer(),
existing_nullable=False,
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"validators",
"stake",
existing_type=sa.Integer(),
type_=sa.NUMERIC(),
existing_nullable=False,
)
# ### end Alembic commands ###
7 changes: 5 additions & 2 deletions backend/database_handler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ class TransactionsAudit(Base):

class Validators(Base):
__tablename__ = "validators"
__table_args__ = (PrimaryKeyConstraint("id", name="validators_pkey"),)
__table_args__ = (
PrimaryKeyConstraint("id", name="validators_pkey"),
CheckConstraint("stake >= 0", name="stake_unsigned_int"),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
stake: Mapped[decimal.Decimal] = mapped_column(Numeric)
stake: Mapped[int] = mapped_column(Integer)
config: Mapped[dict] = mapped_column(JSONB)
address: Mapped[Optional[str]] = mapped_column(String(255))
provider: Mapped[Optional[str]] = mapped_column(String(255))
Expand Down
4 changes: 2 additions & 2 deletions frontend/package-lock.json

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

3 changes: 2 additions & 1 deletion frontend/src/components/Simulator/ValidatorModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ const tryInitValues = () => {
:step="1"
:invalid="newValidatorData.stake < 1"
v-model="newValidatorData.stake"
:forceInteger="true"
required
testId="input-stake"
/>
<FieldError v-if="newValidatorData.stake < 1"
>Please enter a number greater than 0.</FieldError
>Please enter an integer greater than 0.</FieldError
>
</div>

Expand Down
23 changes: 22 additions & 1 deletion frontend/src/components/global/NumberField.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
<script setup lang="ts">
defineProps<{
const props = defineProps<{
name: string;
min?: number;
step?: number;
testId?: string;
invalid?: boolean;
forceInteger?: boolean;
}>();
const model = defineModel();
const handleChange = (e: Event) => {
if (props.forceInteger) {
const target = e.target as HTMLInputElement;
model.value = parseInt(target.value, 10);
}
};
const handleKeydown = (event: KeyboardEvent) => {
if (
props.forceInteger &&
!(event.ctrlKey || event.metaKey) &&
event.key.length === 1 &&
isNaN(Number(event.key))
) {
event.preventDefault();
}
};
</script>

<template>
Expand All @@ -24,5 +43,7 @@ const model = defineModel();
!invalid &&
'ring-gray-400/60 hover:ring-gray-400 dark:ring-zinc-500/60 dark:hover:ring-zinc-500',
]"
@input="handleChange"
@keydown="handleKeydown"
/>
</template>

0 comments on commit a8795af

Please sign in to comment.