Skip to content
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

rename participant field (fixes #136) #142

Merged
merged 9 commits into from
Nov 9, 2019

Conversation

kristhina
Copy link
Member

I hope that it is correct. I don't know how to test it.

Copy link
Member

@magul magul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few minor issues spotted, but deal-breaker here is how migration was created.

@@ -29,18 +29,18 @@ def test_add_new_participant_to_db(_db, new_participant):
assert len(db.session.query(Participant).all()) == 0

new_participant = Participant(
name=new_participant['name'],
lastname=new_participant['lastname'],
first_name=new_participant['first_name'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that as new_participant = Participant(**new_participant)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed :)

@@ -19,14 +19,14 @@ def test_create_new_user(new_user):
def test_create_new_participant(new_participant):
"""Test participant model."""
new_participant = Participant(
name=new_participant['name'],
lastname=new_participant['lastname'],
first_name=new_participant['first_name'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto: new_participant = Participant(**new_participant)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed :)

@@ -26,7 +26,7 @@ def upgrade():
op.create_table('participant',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=20), nullable=True),
sa.Column('lastname', sa.String(length=20), nullable=True),
sa.Column('last_name', sa.String(length=20), nullable=True),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Historical cannot be changed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to lastname

email = factory.LazyAttribute(
lambda obj: "{}@cfp.com".format(obj.lastname)
lambda obj: "{}@cfp.com".format(obj.last_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't own cfp.com domain, it's not connected with us anyhow. We should change domain here to codeforpoznan.pl od stick to reserved, testing domains (like example.com, or codeforpoznan.test).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.test is the best option

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to "codeforpoznan.test"


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('participant', sa.Column('first_name', sa.String(length=50), nullable=True))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use alter_column here, not drop_column and add_column as altering will preserve data.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been done automatically by alembic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this one with alembic. 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magul, we are waiting for your re-review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, that Alembic could create such migration, but that's just a tool and in that case, it does it wrong. We're not on production right now, but as a learning exercise, we should prepare that migration in such manner, that it could be safely run on a production environment.

Right now that migration will drop existing columns an create new ones. Data previously stored in removed columns are permanently lost. I belive we should use here https://alembic.sqlalchemy.org/en/latest/ops.html?highlight=unique#alembic.operations.Operations.alter_column

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magul
I made all the requested changes in migration files. I checked the command flask db upgrade and flask db migrate - everything worked. When I opened the database in dbeaver it had column names: first_name and last_name. I hope that now everything works ok, but it would be nice if you could check :)

@kristhina kristhina requested a review from magul August 21, 2019 17:02

def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('participant', sa.Column('first_name', sa.String(length=50), nullable=True))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, that Alembic could create such migration, but that's just a tool and in that case, it does it wrong. We're not on production right now, but as a learning exercise, we should prepare that migration in such manner, that it could be safely run on a production environment.

Right now that migration will drop existing columns an create new ones. Data previously stored in removed columns are permanently lost. I belive we should use here https://alembic.sqlalchemy.org/en/latest/ops.html?highlight=unique#alembic.operations.Operations.alter_column

existing_type=sa.VARCHAR(length=20),
type_=sa.Date(),
existing_nullable=True)
op.execute("""ALTER TABLE hacknight ALTER COLUMN date TYPE DATE USING date::date""")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what happened here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magul there was some problem with the upgrade of database with date type. It required: "USING date::date" and in SQLAlchemy it was impossible to add this "USING" part. With @stanislawK we were looking for different solutions and we found that we should use pure SQL to solve this problem.

@magul magul merged commit 8d4cead into CodeForPoznan:master Nov 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants