Skip to content

Commit

Permalink
Merge pull request #2 from sandwichcloud/timestamps
Browse files Browse the repository at this point in the history
use clock_timestamp to prevent items being entered with the same time…
  • Loading branch information
rmb938 authored Nov 1, 2017
2 parents 1b543d9 + 83838c7 commit 2a7ae35
Show file tree
Hide file tree
Showing 19 changed files with 106 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),

)
Expand Down
6 changes: 4 additions & 2 deletions ingredients_db/alembic/versions/3ce1572cbc6b_create_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
9 changes: 6 additions & 3 deletions ingredients_db/alembic/versions/52923fe51ede_create_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

Expand All @@ -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),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),

)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand All @@ -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)
)

Expand Down
28 changes: 19 additions & 9 deletions ingredients_db/alembic/versions/dadf4ada480a_create_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand All @@ -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)
)

Expand All @@ -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)
)
Expand All @@ -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),
)

Expand Down Expand Up @@ -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"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
17 changes: 10 additions & 7 deletions ingredients_db/models/authn.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)


Expand All @@ -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)
12 changes: 7 additions & 5 deletions ingredients_db/models/authz.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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?
Expand All @@ -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)
9 changes: 5 additions & 4 deletions ingredients_db/models/images.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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')
12 changes: 7 additions & 5 deletions ingredients_db/models/instance.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Loading

0 comments on commit 2a7ae35

Please sign in to comment.