Skip to content

Commit

Permalink
nullable false
Browse files Browse the repository at this point in the history
  • Loading branch information
achandrabalan committed Nov 23, 2023
1 parent fe591c7 commit 0cfe130
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
4 changes: 3 additions & 1 deletion backend/app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class User(db.Model):
)
email = db.Column(db.String, nullable=False)
last_modified = db.deferred(
db.Column(db.DateTime, default=func.now(), onupdate=func.now())
db.Column(
db.DateTime, server_default=func.now(), onupdate=func.now(), nullable=False
)
)

__table_args__ = (
Expand Down
3 changes: 1 addition & 2 deletions backend/app/resources/auth_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def __init__(
email,
role,
user_status,
last_modified
):
Token.__init__(self, access_token, refresh_token)
UserDTO.__init__(self, id, first_name, last_name, email, role, user_status, last_modified)
UserDTO.__init__(self, id, first_name, last_name, email, role, user_status)
4 changes: 3 additions & 1 deletion backend/app/resources/user_dto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class UserDTO:
def __init__(self, id, first_name, last_name, email, role, user_status, last_modified):
def __init__(
self, id, first_name, last_name, email, role, user_status, last_modified
):
self.id = id
self.first_name = first_name
self.last_name = last_name
Expand Down
3 changes: 2 additions & 1 deletion backend/app/rest/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def create_user():
status_code = None
if str(e) == "User already exists":
status_code = 409

if e:
print(str(e))
return jsonify({"error": (error_message if error_message else str(e))}), (
status_code if status_code else 500
)
Expand Down
11 changes: 3 additions & 8 deletions backend/app/services/implementations/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,15 @@ def get_auth_id_by_user_id(self, user_id):
def get_users(self, return_all, page_number, results_per_page):
try:
if return_all:
users = (
User.query
.order_by(User.last_modified.desc())
.all()
)
users = User.query.order_by(User.last_modified.desc()).all()
else:
users = (
User.query
.order_by(User.last_modified.desc())
User.query.order_by(User.last_modified.desc())
.limit(results_per_page)
.offset((page_number - 1) * results_per_page)
.all()
)

json_list = list(
map(
lambda user: UserService.__user_to_dict_and_remove_auth_id(user),
Expand Down
20 changes: 14 additions & 6 deletions backend/migrations/versions/698e5724baae_last_modified_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@


# revision identifiers, used by Alembic.
revision = '698e5724baae'
down_revision = '0ea2257f1dc6'
revision = "698e5724baae"
down_revision = "0ea2257f1dc6"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.add_column(sa.Column('last_modified', sa.DateTime(), nullable=True))
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.add_column(
sa.Column(
"last_modified",
sa.DateTime(),
server_default=sa.text("CURRENT_TIMESTAMP"),
onupdate=sa.text("CURRENT_TIMESTAMP"),
nullable=False,
)
)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.drop_column('last_modified')
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.drop_column("last_modified")

# ### end Alembic commands ###

0 comments on commit 0cfe130

Please sign in to comment.