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

🔨 preparing for version 1.0.0 #93

Merged
merged 10 commits into from
Jan 31, 2023
Binary file modified docs/model/db-schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/model/db-schema.xml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/sphinx/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Sample-DB API

datasets
dataset_table
provenance
provenance
users
4 changes: 3 additions & 1 deletion docs/sphinx/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.

Usage
=====

Expand Down Expand Up @@ -72,7 +73,8 @@ You should get a similar output::
sampledb | collect_method | table | postgres
sampledb | datasets | table | postgres
sampledb | provenance | table | postgres
(3 rows)
sampledb | users | table | postgres
(4 rows)


Setting up PL/pgSQL Triggers and Loading default script data
Expand Down
25 changes: 25 additions & 0 deletions docs/sphinx/users.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
..
This file is part of SAMPLE-DB.
Copyright (C) 2022 INPE.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.


Users
-----

.. autoclass:: sample_db.models.users::Users
:members:
:special-members: __init__
:member-order: bysource
50 changes: 50 additions & 0 deletions sample_db/alembic/9f4a4b16344f_adding_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Adding user.

Revision ID: 9f4a4b16344f
Revises: 90f91c523f48
Create Date: 2022-11-11 14:59:20.900082

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '9f4a4b16344f'
down_revision = '90f91c523f48'
branch_labels = ()
depends_on = '561ebe6266ad' # LCCS-DB stable 0.8.1


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('email', sa.String(length=255), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('institution', sa.String(length=255), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('users_pkey')),
sa.UniqueConstraint('email', name=op.f('users_email_key')),
sa.UniqueConstraint('user_id', name=op.f('users_user_id_key')),
schema='sampledb'
)
op.create_index(op.f('idx_sampledb_users_email'), 'users', ['email'], unique=False, schema='sampledb')
op.create_index(op.f('idx_sampledb_users_institution'), 'users', ['institution'], unique=False, schema='sampledb')
op.create_index(op.f('idx_sampledb_users_name'), 'users', ['name'], unique=False, schema='sampledb')
op.create_index(op.f('idx_sampledb_users_user_id'), 'users', ['user_id'], unique=False, schema='sampledb')
op.create_foreign_key(op.f('datasets_user_id_users_fkey'), 'datasets', 'users', ['user_id'], ['user_id'], source_schema='sampledb', referent_schema='sampledb', ondelete='CASCADE')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(op.f('datasets_user_id_users_fkey'), 'datasets', schema='sampledb', type_='foreignkey')
op.drop_index(op.f('idx_sampledb_users_name'), table_name='users', schema='sampledb')
op.drop_index(op.f('idx_sampledb_users_institution'), table_name='users', schema='sampledb')
op.drop_index(op.f('idx_sampledb_users_email'), table_name='users', schema='sampledb')
op.drop_index(op.f('idx_sampledb_users_user_id'), table_name='users', schema='sampledb')
op.drop_table('users', schema='sampledb')
# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion sample_db/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""SampleDB Provenance Model."""
from sample_db.models.datasets import CollectMethod, Datasets, DatasetView
from sample_db.models.provenance import Provenance
from sample_db.models.users import Users

__all__ = ['Datasets', 'Provenance', 'CollectMethod', 'DatasetView']
__all__ = ['Datasets', 'Users', 'Provenance', 'CollectMethod', 'DatasetView']

7 changes: 6 additions & 1 deletion sample_db/models/dataset_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from ..config import Config
from .base import db, metadata
from .users import Users


class DatasetType(UserDefinedType):
Expand Down Expand Up @@ -81,7 +82,7 @@ def make_dataset_table(table_name: str, create: bool = False) -> Table:
s_name = f"{Config.SAMPLEDB_SCHEMA}.dataset_{table_name}_id_seq"

if create:
if not sqlalchemy.inspect(db.engine).has_table(table_name=f'dataset_{table_name}', schema=Config.SAMPLEDB_SCHEMA):
if not db.engine.dialect.has_table(table_name=f'dataset_{table_name}', connection=db.session, schema=Config.SAMPLEDB_SCHEMA):
db.engine.execute(f"CREATE TABLE {Config.SAMPLEDB_SCHEMA}.dataset_{table_name} OF dataset_type")
db.engine.execute(f"CREATE SEQUENCE {s_name}")

Expand Down Expand Up @@ -119,6 +120,10 @@ def make_dataset_table(table_name: str, create: bool = False) -> Table:
ForeignKeyConstraint(name=f"dataset_{table_name}_{klass.c.class_id.name}_fkey",
columns=[klass.c.class_id], refcolumns=[LucClass.id], onupdate="CASCADE",
ondelete="CASCADE")))
db.engine.execute(AddConstraint(
ForeignKeyConstraint(name=f"dataset_{table_name}_{klass.c.user_id.name}_fkey",
columns=[klass.c.user_id], refcolumns=[Users.user_id], onupdate="CASCADE",
ondelete="CASCADE")))
else:
raise RuntimeError(f'Table {table_name} already exists')
else:
Expand Down
20 changes: 11 additions & 9 deletions sample_db/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
from ..config import Config
from .base import db as _db
from .dataset_table import make_dataset_table
from .users import Users

Feature = Dict[str, str]


class CollectMethod(BaseModel):
"""Collect Method Model."""

Expand Down Expand Up @@ -76,7 +76,7 @@ class Datasets(BaseModel):
nullable=False)
collect_method_id = Column(Integer, ForeignKey(CollectMethod.id, ondelete='CASCADE', onupdate='CASCADE'),
nullable=True)
user_id = Column(Integer, nullable=False)
user_id = Column(Integer, ForeignKey(Users.user_id, ondelete='CASCADE'), nullable=False)

__table_args__ = (
Index(None, user_id),
Expand Down Expand Up @@ -147,13 +147,15 @@ def get_ds_table(cls, ds_name: str, ds_version: str) -> Union[Table, None]:
f'LOWER(ds.name) = LOWER(\'{ds_name}\') AND ' \
f'LOWER(ds.version) = LOWER(\'{ds_version}\')'

res = _db.session.execute(expr).fetchone()

if res:
return Table(res.table_name, _db.metadata, schema=Config.SAMPLEDB_SCHEMA, autoload=True,
autoload_with=_db.engine)

return None
try:
res = _db.session.execute(expr).fetchone()
if res:
return Table(res.table_name, _db.metadata, schema=Config.SAMPLEDB_SCHEMA, autoload=True,
autoload_with=_db.engine)

return None
finally:
_db.session.close()

@property
def ds_table(self) -> Union[Table, None]:
Expand Down
44 changes: 44 additions & 0 deletions sample_db/models/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# This file is part of SAMPLE-DB.
# Copyright (C) 2022 INPE.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
#

from bdc_db.sqltypes import JSONB
from lccs_db.models.base import BaseModel
from sqlalchemy import (Column, ForeignKey, Index, Integer,
PrimaryKeyConstraint, String)

from ..config import Config


class Users(BaseModel):
"""User Model."""

__tablename__ = 'users'

id = Column(Integer, primary_key=True)
email = Column(String(255), nullable=False, unique=True)
name = Column(String(255), nullable=False)
institution = Column(String(255), nullable=False)
user_id = Column(Integer, nullable=False, unique=True)

__table_args__ = (
Index(None, email),
Index(None, name),
Index(None, institution),
Index(None, user_id),
dict(schema=Config.SAMPLEDB_SCHEMA),
)
4 changes: 3 additions & 1 deletion sample_db/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
"""


__version__ = '0.9.1'

__version__ = '1.0.0'