From fdc2ae1a9114aac512eb0ce638d6bd8759f761c2 Mon Sep 17 00:00:00 2001 From: Ryan Belgrave Date: Tue, 31 Oct 2017 21:01:20 -0500 Subject: [PATCH 1/2] use clock_timestamp to prevent items being entered with the same time during a transaction --- .../versions/1fdbfd6b0eea_create_instance.py | 6 ++-- .../versions/3ce1572cbc6b_create_tasks.py | 6 ++-- .../458762cd0419_create_authn_user.py | 6 ++-- .../versions/52923fe51ede_create_image.py | 9 ++++-- .../versions/9d6460001e00_create_network.py | 6 ++-- .../b129792f908b_create_network_port.py | 6 ++-- .../ba0652dfa1be_create_public_keys.py | 12 +++++--- .../versions/dadf4ada480a_create_authz.py | 28 +++++++++++++------ .../versions/f422a466b0a8_create_project.py | 6 ++-- ingredients_db/models/authn.py | 17 ++++++----- ingredients_db/models/authz.py | 12 ++++---- ingredients_db/models/images.py | 9 +++--- ingredients_db/models/instance.py | 12 ++++---- ingredients_db/models/network.py | 7 +++-- ingredients_db/models/network_port.py | 7 +++-- ingredients_db/models/project.py | 7 +++-- ingredients_db/models/public_key.py | 7 +++-- ingredients_db/models/task.py | 7 +++-- ingredients_db/models/user.py | 8 +++--- 19 files changed, 110 insertions(+), 68 deletions(-) diff --git a/ingredients_db/alembic/versions/1fdbfd6b0eea_create_instance.py b/ingredients_db/alembic/versions/1fdbfd6b0eea_create_instance.py index 4de2dee..22752e0 100644 --- a/ingredients_db/alembic/versions/1fdbfd6b0eea_create_instance.py +++ b/ingredients_db/alembic/versions/1fdbfd6b0eea_create_instance.py @@ -31,8 +31,10 @@ def upgrade(): sa.Column('project_id', sau.UUIDType, sa.ForeignKey('projects.id', ondelete='RESTRICT'), nullable=False), sa.Column('current_task_id', sau.UUIDType, sa.ForeignKey('tasks.id')), sa.Column('image_id', sau.UUIDType, sa.ForeignKey('images.id', ondelete='SET NULL')), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False), ) diff --git a/ingredients_db/alembic/versions/3ce1572cbc6b_create_tasks.py b/ingredients_db/alembic/versions/3ce1572cbc6b_create_tasks.py index 2331889..5bebf9b 100644 --- a/ingredients_db/alembic/versions/3ce1572cbc6b_create_tasks.py +++ b/ingredients_db/alembic/versions/3ce1572cbc6b_create_tasks.py @@ -26,8 +26,10 @@ def upgrade(): sa.Column('state', sa.Enum(TaskState), default=TaskState.PENDING, nullable=False), sa.Column('error_message', sa.Text), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False), sa.Column('stopped_at', sau.ArrowType(timezone=True)), ) diff --git a/ingredients_db/alembic/versions/458762cd0419_create_authn_user.py b/ingredients_db/alembic/versions/458762cd0419_create_authn_user.py index f718df7..6dfe591 100644 --- a/ingredients_db/alembic/versions/458762cd0419_create_authn_user.py +++ b/ingredients_db/alembic/versions/458762cd0419_create_authn_user.py @@ -22,8 +22,10 @@ def upgrade(): sa.Column('id', sau.UUIDType, server_default=sa.text("uuid_generate_v4()"), primary_key=True), sa.Column('username', sa.String, nullable=False), sa.Column('driver', sa.String, nullable=False), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False) ) op.create_unique_constraint('uq_username_driver', 'authn_users', ['username', 'driver']) diff --git a/ingredients_db/alembic/versions/52923fe51ede_create_image.py b/ingredients_db/alembic/versions/52923fe51ede_create_image.py index db10dbf..aa0b1c7 100644 --- a/ingredients_db/alembic/versions/52923fe51ede_create_image.py +++ b/ingredients_db/alembic/versions/52923fe51ede_create_image.py @@ -33,8 +33,10 @@ def upgrade(): sa.Column('project_id', sau.UUIDType, sa.ForeignKey('projects.id', ondelete='RESTRICT'), nullable=False), sa.Column('current_task_id', sau.UUIDType, sa.ForeignKey('tasks.id')), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False), ) @@ -45,7 +47,8 @@ def upgrade(): sa.Column('project_id', sau.UUIDType, sa.ForeignKey('projects.id', ondelete='CASCADE'), nullable=False, primary_key=True), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), ) diff --git a/ingredients_db/alembic/versions/9d6460001e00_create_network.py b/ingredients_db/alembic/versions/9d6460001e00_create_network.py index 9a4a9b0..ccf4bb9 100644 --- a/ingredients_db/alembic/versions/9d6460001e00_create_network.py +++ b/ingredients_db/alembic/versions/9d6460001e00_create_network.py @@ -33,8 +33,10 @@ def upgrade(): sa.Column('cidr', IPv4Network, nullable=False), sa.Column('pool_start', sau.IPAddressType, nullable=False), sa.Column('pool_end', sau.IPAddressType, nullable=False), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False), ) diff --git a/ingredients_db/alembic/versions/b129792f908b_create_network_port.py b/ingredients_db/alembic/versions/b129792f908b_create_network_port.py index 3443b79..6761050 100644 --- a/ingredients_db/alembic/versions/b129792f908b_create_network_port.py +++ b/ingredients_db/alembic/versions/b129792f908b_create_network_port.py @@ -24,8 +24,10 @@ def upgrade(): sa.Column('network_id', sau.UUIDType, sa.ForeignKey('networks.id', ondelete='RESTRICT'), nullable=False), sa.Column('ip_address', sau.IPAddressType), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False) ) diff --git a/ingredients_db/alembic/versions/ba0652dfa1be_create_public_keys.py b/ingredients_db/alembic/versions/ba0652dfa1be_create_public_keys.py index 131859a..d6b565c 100644 --- a/ingredients_db/alembic/versions/ba0652dfa1be_create_public_keys.py +++ b/ingredients_db/alembic/versions/ba0652dfa1be_create_public_keys.py @@ -25,8 +25,10 @@ def upgrade(): sa.Column('project_id', sau.UUIDType, sa.ForeignKey('projects.id', ondelete='CASCADE'), nullable=False), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False) ) @@ -37,8 +39,10 @@ def upgrade(): sa.Column('public_key_id', sau.UUIDType, sa.ForeignKey('public_keys.id', ondelete='CASCADE')), sa.Column('instance_id', sau.UUIDType, sa.ForeignKey('instances.id', ondelete='CASCADE')), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False) ) diff --git a/ingredients_db/alembic/versions/dadf4ada480a_create_authz.py b/ingredients_db/alembic/versions/dadf4ada480a_create_authz.py index 25a40f8..6375b60 100644 --- a/ingredients_db/alembic/versions/dadf4ada480a_create_authz.py +++ b/ingredients_db/alembic/versions/dadf4ada480a_create_authz.py @@ -23,8 +23,10 @@ def upgrade(): sa.Column('name', sa.String, nullable=False, unique=True), sa.Column('rule', sa.String, nullable=False), sa.Column('description', sa.String), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False) ) @@ -33,8 +35,10 @@ def upgrade(): sa.Column('id', sau.UUIDType, server_default=sa.text("uuid_generate_v4()"), primary_key=True), sa.Column('name', sa.String, nullable=False, unique=True), sa.Column('description', sa.String), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False) ) @@ -46,8 +50,10 @@ def upgrade(): sa.Column('user_id', sau.UUIDType, sa.ForeignKey('authn_users.id', ondelete='CASCADE'), nullable=False), sa.Column('project_id', sau.UUIDType, sa.ForeignKey('projects.id', ondelete='CASCADE')), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False), sa.Column('expires_at', sau.ArrowType(timezone=True), nullable=False) ) @@ -59,8 +65,10 @@ def upgrade(): sa.Column('token_id', sau.UUIDType, sa.ForeignKey('authn_tokens.id', ondelete='CASCADE'), nullable=False), sa.Column('role_id', sau.UUIDType, sa.ForeignKey('authz_roles.id', ondelete='CASCADE'), nullable=False), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False), ) @@ -132,7 +140,9 @@ def upgrade(): {"name": "instances:action:image:public", "rule": "rule:is_admin", "description": "Ability to create a public image from an instance"}, {"name": "instances:action:reset_state", "rule": "rule:admin_or_member", - "description": "Ability to reset the state of an instance"}, + "description": "Ability to reset the state of an instance to error"}, + {"name": "instances:action:reset_state:active", "rule": "rule:is_admin", + "description": "Ability to reset the state of an instance to active"}, # Networks {"name": "networks:create", "rule": "rule:is_admin", "description": "Ability to create a network"}, diff --git a/ingredients_db/alembic/versions/f422a466b0a8_create_project.py b/ingredients_db/alembic/versions/f422a466b0a8_create_project.py index 2c38e22..c3c7d0c 100644 --- a/ingredients_db/alembic/versions/f422a466b0a8_create_project.py +++ b/ingredients_db/alembic/versions/f422a466b0a8_create_project.py @@ -25,8 +25,10 @@ def upgrade(): sa.Column('name', sa.String, unique=True, nullable=False), sa.Column('state', sa.Enum(ProjectState), default=ProjectState.CREATED, nullable=False), - sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), nullable=False, index=True), - sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now(), + sa.Column('created_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + nullable=False, index=True), + sa.Column('updated_at', sau.ArrowType(timezone=True), server_default=sa.text('clock_timestamp()'), + onupdate=sa.text('clock_timestamp()'), nullable=False) ) diff --git a/ingredients_db/models/authn.py b/ingredients_db/models/authn.py index c08f34a..f23adae 100644 --- a/ingredients_db/models/authn.py +++ b/ingredients_db/models/authn.py @@ -1,5 +1,5 @@ import arrow -from sqlalchemy import func, Column, String, text, ForeignKey, UniqueConstraint +from sqlalchemy import Column, String, text, ForeignKey, UniqueConstraint from sqlalchemy_utils import generic_repr, ArrowType, UUIDType from ingredients_db.database import Base @@ -16,8 +16,9 @@ class AuthNUser(Base): username = Column(String, nullable=False) driver = Column(String, nullable=False) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) @generic_repr @@ -30,8 +31,9 @@ class AuthNToken(Base): user_id = Column(UUIDType, ForeignKey('authn_users.id', ondelete='CASCADE'), nullable=False) project_id = Column(UUIDType, ForeignKey('projects.id', ondelete='CASCADE')) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) expires_at = Column(ArrowType(timezone=True), default=arrow.now().shift(days=+1), nullable=False) @@ -42,5 +44,6 @@ class AuthNTokenRole(Base): token_id = Column(UUIDType, ForeignKey('authn_tokens.id', ondelete='CASCADE'), nullable=False) role_id = Column(UUIDType, ForeignKey('authz_roles.id', ondelete='CASCADE'), nullable=False) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) diff --git a/ingredients_db/models/authz.py b/ingredients_db/models/authz.py index 16399d3..aa4e01b 100644 --- a/ingredients_db/models/authz.py +++ b/ingredients_db/models/authz.py @@ -1,4 +1,4 @@ -from sqlalchemy import text, Column, func, String +from sqlalchemy import text, Column, String from sqlalchemy_utils import UUIDType, ArrowType from ingredients_db.database import Base @@ -13,8 +13,9 @@ class AuthZPolicy(Base): rule = Column(String, nullable=False) description = Column(String) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) # TODO: should we have locked roles to prevent accidental deletion? @@ -25,5 +26,6 @@ class AuthZRole(Base): name = Column(String, nullable=False, unique=True) description = Column(String) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) diff --git a/ingredients_db/models/images.py b/ingredients_db/models/images.py index bd385e2..0f691a0 100644 --- a/ingredients_db/models/images.py +++ b/ingredients_db/models/images.py @@ -1,6 +1,6 @@ import enum -from sqlalchemy import Column, func, String, ForeignKey, Enum, Boolean, text +from sqlalchemy import Column, String, ForeignKey, Enum, Boolean, text from sqlalchemy.orm import relationship from sqlalchemy_utils import UUIDType, ArrowType, generic_repr @@ -30,7 +30,7 @@ class ImageMembers(Base): image_id = Column(UUIDType, ForeignKey('images.id', ondelete='CASCADE'), nullable=False, primary_key=True) project_id = Column(UUIDType, ForeignKey('projects.id', ondelete='CASCADE'), nullable=False, primary_key=True) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) # TODO: Image families @@ -47,7 +47,8 @@ class Image(Base, TaskMixin, ProjectMixin): state = Column(Enum(ImageState), default=ImageState.CREATING, nullable=False) visibility = Column(Enum(ImageVisibility), default=ImageVisibility.PRIVATE, nullable=False) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) members = relationship(Project, secondary='image_members') diff --git a/ingredients_db/models/instance.py b/ingredients_db/models/instance.py index d6dc458..b4e95af 100644 --- a/ingredients_db/models/instance.py +++ b/ingredients_db/models/instance.py @@ -1,6 +1,6 @@ import enum -from sqlalchemy import Column, String, ForeignKey, Enum, text, func +from sqlalchemy import Column, String, ForeignKey, Enum, text from sqlalchemy.dialects.postgresql import HSTORE from sqlalchemy.orm import relationship from sqlalchemy_utils import UUIDType, generic_repr, ArrowType @@ -38,8 +38,9 @@ class Instance(Base, TaskMixin, NetworkableMixin, ProjectMixin): public_keys = relationship(PublicKey, secondary='instance_public_keys') - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) class InstancePublicKey(Base): @@ -50,5 +51,6 @@ class InstancePublicKey(Base): public_key_id = Column(UUIDType, ForeignKey('public_keys.id', ondelete='CASCADE')) instance_id = Column(UUIDType, ForeignKey('instances.id', ondelete='CASCADE')) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) diff --git a/ingredients_db/models/network.py b/ingredients_db/models/network.py index 5b8d584..0519ed5 100644 --- a/ingredients_db/models/network.py +++ b/ingredients_db/models/network.py @@ -2,7 +2,7 @@ import ipaddress from typing import Optional -from sqlalchemy import Column, String, Enum, text, func +from sqlalchemy import Column, String, Enum, text from sqlalchemy_utils import UUIDType, generic_repr, IPAddressType, ArrowType from ingredients_db.database import Base @@ -34,8 +34,9 @@ class Network(Base, TaskMixin): pool_start = Column(IPAddressType, nullable=False) pool_end = Column(IPAddressType, nullable=False) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) def next_free_address(self, session) -> Optional[ipaddress.IPv4Address]: ip_network = ipaddress.ip_network(self.cidr) diff --git a/ingredients_db/models/network_port.py b/ingredients_db/models/network_port.py index cb6f09d..09f227e 100644 --- a/ingredients_db/models/network_port.py +++ b/ingredients_db/models/network_port.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, text, func, ForeignKey +from sqlalchemy import Column, text, ForeignKey from sqlalchemy.ext.declarative import declared_attr from sqlalchemy_utils import UUIDType, ArrowType, generic_repr, IPAddressType @@ -14,8 +14,9 @@ class NetworkPort(Base): network_id = Column(UUIDType, ForeignKey('networks.id', ondelete='RESTRICT'), nullable=False) ip_address = Column(IPAddressType) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) class NetworkableMixin(object): diff --git a/ingredients_db/models/project.py b/ingredients_db/models/project.py index aca9079..f79466d 100644 --- a/ingredients_db/models/project.py +++ b/ingredients_db/models/project.py @@ -1,6 +1,6 @@ import enum -from sqlalchemy import Column, text, func, String, Enum, ForeignKey +from sqlalchemy import Column, text, String, Enum, ForeignKey from sqlalchemy.ext.declarative import declared_attr from sqlalchemy_utils import UUIDType, ArrowType, generic_repr @@ -26,8 +26,9 @@ class Project(Base): name = Column(String, unique=True, nullable=False) state = Column(Enum(ProjectState), default=ProjectState.CREATED, nullable=False) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) class ProjectMixin(object): diff --git a/ingredients_db/models/public_key.py b/ingredients_db/models/public_key.py index f9b28f1..8e9b6b6 100644 --- a/ingredients_db/models/public_key.py +++ b/ingredients_db/models/public_key.py @@ -1,4 +1,4 @@ -from sqlalchemy import text, Column, String, Text, func, ForeignKey +from sqlalchemy import text, Column, String, Text, ForeignKey from sqlalchemy_utils import UUIDType, generic_repr, ArrowType from ingredients_db.database import Base @@ -14,5 +14,6 @@ class PublicKey(Base): project_id = Column(UUIDType, ForeignKey('projects.id', ondelete='CASCADE'), nullable=False) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) diff --git a/ingredients_db/models/task.py b/ingredients_db/models/task.py index a607e6e..8c41d62 100644 --- a/ingredients_db/models/task.py +++ b/ingredients_db/models/task.py @@ -1,6 +1,6 @@ import enum -from sqlalchemy import text, Column, String, Enum, Text, func, ForeignKey +from sqlalchemy import text, Column, String, Enum, Text, ForeignKey from sqlalchemy.ext.declarative import declared_attr from sqlalchemy_utils import UUIDType, ArrowType, generic_repr @@ -23,8 +23,9 @@ class Task(Base): state = Column(Enum(TaskState), default=TaskState.PENDING, nullable=False) error_message = Column(Text) - created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False) - updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) + created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) + updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), + onupdate=text('clock_timestamp()'), nullable=False) stopped_at = Column(ArrowType(timezone=True)) diff --git a/ingredients_db/models/user.py b/ingredients_db/models/user.py index f2b77d7..3034933 100644 --- a/ingredients_db/models/user.py +++ b/ingredients_db/models/user.py @@ -12,8 +12,8 @@ # id = Column(UUIDType, server_default=text("uuid_generate_v4()"), primary_key=True) # username = Column(String, unique=True, nullable=False) # -# created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False) -# updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) +# created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) +# updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), onupdate=text('clock_timestamp()'), nullable=False) # # # @generic_repr @@ -26,6 +26,6 @@ # user_id = Column(UUIDType, ForeignKey('users.id', ondelete='CASCADE'), nullable=False) # project_id = Column(UUIDType, ForeignKey('projects.id', ondelete='CASCADE')) # -# created_at = Column(ArrowType(timezone=True), server_default=func.now(), nullable=False, index=True) -# updated_at = Column(ArrowType(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False) +# created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) +# updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), onupdate=text('clock_timestamp()'), nullable=False) # expires_at = Column(ArrowType(timezone=True), default=arrow.now().shift(days=+1), nullable=False) From 83838c7604a0f758e26f013396bd6246ad8e623d Mon Sep 17 00:00:00 2001 From: Ryan Belgrave Date: Tue, 31 Oct 2017 21:06:40 -0500 Subject: [PATCH 2/2] remove users model since it is not used --- ingredients_db/models/user.py | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 ingredients_db/models/user.py diff --git a/ingredients_db/models/user.py b/ingredients_db/models/user.py deleted file mode 100644 index 3034933..0000000 --- a/ingredients_db/models/user.py +++ /dev/null @@ -1,31 +0,0 @@ -# import arrow -# from sqlalchemy import Column, text, func, String, ForeignKey -# from sqlalchemy_utils import UUIDType, ArrowType, generic_repr -# -# from ingredients_db.database import Base - - -# @generic_repr -# class User(Base): -# __tablename__ = 'users' -# -# id = Column(UUIDType, server_default=text("uuid_generate_v4()"), primary_key=True) -# username = Column(String, unique=True, nullable=False) -# -# created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False) -# updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), onupdate=text('clock_timestamp()'), nullable=False) -# -# -# @generic_repr -# class UserToken(Base): -# __tablename__ = 'user_tokens' -# -# id = Column(UUIDType, server_default=text("uuid_generate_v4()"), primary_key=True) -# access_token = Column(String, nullable=False, index=True) -# -# user_id = Column(UUIDType, ForeignKey('users.id', ondelete='CASCADE'), nullable=False) -# project_id = Column(UUIDType, ForeignKey('projects.id', ondelete='CASCADE')) -# -# created_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), nullable=False, index=True) -# updated_at = Column(ArrowType(timezone=True), server_default=text('clock_timestamp()'), onupdate=text('clock_timestamp()'), nullable=False) -# expires_at = Column(ArrowType(timezone=True), default=arrow.now().shift(days=+1), nullable=False)