fixed migration and insert_hash function
This commit is contained in:
parent
d8c7d55366
commit
db92c390d9
|
@ -5,12 +5,13 @@ Revises: 378b6b147b46
|
||||||
Create Date: 2020-12-18 16:26:15.453694
|
Create Date: 2020-12-18 16:26:15.453694
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import context
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
import sqlalchemy_utils
|
|
||||||
import citext
|
import citext
|
||||||
import teal
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
from alembic import context
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
|
@ -29,10 +30,10 @@ def get_inv():
|
||||||
def upgrade():
|
def upgrade():
|
||||||
# Report Hash table
|
# Report Hash table
|
||||||
op.create_table('report_hash',
|
op.create_table('report_hash',
|
||||||
sa.Column('id', sa.BigInteger(), nullable=False),
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
sa.Column('created', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'),
|
sa.Column('created', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'),
|
||||||
nullable=False, comment='When Devicehub created this.'),
|
nullable=False, comment='When Devicehub created this.'),
|
||||||
sa.Column('hash', citext.CIText(), nullable=False),
|
sa.Column('hash3', citext.CIText(), nullable=False),
|
||||||
sa.PrimaryKeyConstraint('id'),
|
sa.PrimaryKeyConstraint('id'),
|
||||||
schema=f'{get_inv()}'
|
schema=f'{get_inv()}'
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
"""Hash implementation and save in database
|
||||||
|
"""
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
from citext import CIText
|
||||||
|
from sqlalchemy import Column
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from ereuse_devicehub.db import db
|
||||||
|
|
||||||
|
|
||||||
|
class ReportHash(db.Model):
|
||||||
|
"""Save the hash than is create when one report is download.
|
||||||
|
"""
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
|
||||||
|
id.comment = """The identifier of the device for this database. Used only
|
||||||
|
internally for software; users should not use this.
|
||||||
|
"""
|
||||||
|
hash3 = db.Column(CIText(), nullable=False)
|
||||||
|
hash3.comment = """The normalized name of the hash."""
|
||||||
|
|
||||||
|
|
||||||
|
def insert_hash(bfile=b'hello'):
|
||||||
|
hash3 = hashlib.sha3_256(bfile).hexdigest()
|
||||||
|
db_hash = ReportHash(hash3=hash3)
|
||||||
|
db.session.add(db_hash)
|
||||||
|
db.session.commit()
|
||||||
|
db.session.flush()
|
Reference in New Issue