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

adds access to archive database to sdssdb #11

Merged
merged 30 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c0d3fca
archive
Aug 10, 2019
de16086
archive_20190507
Aug 10, 2019
783eb6a
Update sas.py
joelbrownstein Aug 11, 2019
dbfb0c8
Updated define_relations() method in sas module
nbmurphy Sep 4, 2019
b92db30
linting archive sas.py file
havok2063 Sep 18, 2019
d1c9c80
updating relationships
havok2063 Sep 18, 2019
45ab952
updating relationships
havok2063 Sep 18, 2019
51d7dd9
updating relationships
havok2063 Sep 18, 2019
4a5f7f5
updating relationships to fix symlinks and symdirs
havok2063 Sep 18, 2019
aa0f038
cleaning up relationships
havok2063 Sep 18, 2019
a13c28d
updating print_fields
havok2063 Sep 18, 2019
632a3e0
add archive
Sep 18, 2019
4475031
updating backrefs
havok2063 Sep 19, 2019
71daf80
Merge branch 'archive' of https://github.com/sdss/sdssdb into archive
havok2063 Sep 19, 2019
7120537
adding archive db to docs
havok2063 Sep 19, 2019
3130548
updating changelog
havok2063 Sep 19, 2019
8e47785
adding files-dir relationship and name property to file
havok2063 Sep 19, 2019
807faf2
bugfix in relationship name
havok2063 Sep 19, 2019
64bfe1f
Added archive db ER diagram and made associated changes to intro.rst
nbmurphy Sep 20, 2019
3a6b978
fixing link to sas archive pdf schema
havok2063 Sep 23, 2019
2e6c259
merging master
havok2063 Sep 23, 2019
9dbc24b
fixing imports and linting issues
havok2063 Sep 23, 2019
a3dfcf0
fixing linting issues
havok2063 Sep 23, 2019
ba7c843
adding travis status badge
havok2063 Sep 23, 2019
d6ffc9a
removing requirements.txt from travis file
havok2063 Sep 23, 2019
f2cd3bc
adding a skipdb test for travis until db tests can be fixed
havok2063 Sep 23, 2019
4b3d9c6
removing tests for nonexistent code
havok2063 Sep 23, 2019
a6910b6
allowing py2.7 to fail
havok2063 Sep 23, 2019
6b66450
allowing py3.5 to fail since we use fstrings
havok2063 Sep 23, 2019
7c490a1
removing explicit localhost from local profile
havok2063 Sep 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog

This document records the main changes to the ``sdssdb`` code.

* Added ``archive`` database with ``sas`` schema.
* Added ``TIC v8``.
* :release:`0.2.2 <2019-07-24>`
* Fixed import of database connections when Peewee or SQLAlchemy are not available.
Expand Down
9 changes: 9 additions & 0 deletions docs/sphinx/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Currently, we support the following databases and schemas:
* **sdss5db**: the SDSS-V development database.
* *catalogdb*: schema for source catalogues used for target selection.
* *targetdb*: schema with the results of the target selection and positioner information.
* **archive**: the SDSS science archive database.
* *sas*: schema for SAS.

Note that the level of readiness is not necessarily identical in both Peewee and SQLAlchemy. This table summarises what schemas are available for each library. Green indicates fully supported, yellow partial support, and red means that there are currently not model classes available for that schema. You can download the graph visualisation of the schema, showing the tables, columns, and relations between tables.

Expand Down Expand Up @@ -111,6 +113,13 @@ Note that the level of readiness is not necessarily identical in both Peewee and
<td class="warning"></td>
<td align="center"><a class="glyphicon glyphicon-download-alt" href="_static/schema_graphs/auto/sdss5db.targetdb.pdf"></a></td>
</tr>
<tr>
<td class="active">archive</td>
<td class="active">sas</td>
<td class="danger"></td>
<td class="success"></td>
<td align="center"><a class="glyphicon glyphicon-download-alt" href="schema/archive/archive_sas.pdf"></a></td>
havok2063 marked this conversation as resolved.
Show resolved Hide resolved
</tr>
</tbody>
</table>

Expand Down
24 changes: 24 additions & 0 deletions python/sdssdb/sqlalchemy/archive/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# !usr/bin/env python
# -*- coding: utf-8 -*-
#
# Licensed under a 3-clause BSD license.
#
# @Author: Brian Cherinka
# @Date: 2018-09-23 16:06:18
# @Last modified by: José Sánchez-Gallego ([email protected])
# @Last Modified time: 2018-10-10 11:25:13

from __future__ import print_function, division, absolute_import

from sdssdb.connection import SQLADatabaseConnection
from sqlalchemy.ext.declarative import declarative_base, DeferredReflection
from sdssdb.sqlalchemy import BaseModel

# we need a shared common Base when joining across multiple schema
ArchiveBase = declarative_base(cls=(DeferredReflection, BaseModel,))

class ArchiveDatabaseConnection(SQLADatabaseConnection):
havok2063 marked this conversation as resolved.
Show resolved Hide resolved
dbname = 'archive_20190507'
base = ArchiveBase

database = ArchiveDatabaseConnection(autoconnect=True)
havok2063 marked this conversation as resolved.
Show resolved Hide resolved
142 changes: 142 additions & 0 deletions python/sdssdb/sqlalchemy/archive/sas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# !usr/bin/env python
# -*- coding: utf-8 -*-
#
# Licensed under a 3-clause BSD license.
#
# @Author: Joel Brownstein
# @Date: 2019-08-01 06:54:15
# @Last modified by: N Benjamin Murphy ([email protected])
# @Date: 2019-09-04 16:31:00

from __future__ import absolute_import, division, print_function

from sdssdb.sqlalchemy.archive import database, ArchiveBase, sas
havok2063 marked this conversation as resolved.
Show resolved Hide resolved
from sqlalchemy import Column, Float, and_, case, cast, select
from sqlalchemy.engine import reflection
from sqlalchemy.ext.declarative import AbstractConcreteBase, declared_attr
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
from sqlalchemy.types import Integer

SCHEMA = 'sas'


class Base(AbstractConcreteBase, ArchiveBase):
__abstract__ = True
_schema = SCHEMA
_relations = 'define_relations'

@declared_attr
def __table_args__(cls):
return {'schema': cls._schema}


class Root(Base):
__tablename__ = 'root'
print_fields = ['identifier']


class Tree(Base):
__tablename__ = 'tree'
print_fields = ['version']


class Env(Base):
__tablename__ = 'env'


class Directory(Base):
__tablename__ = 'directory'
print_fields = ['location']


class File(Base):
__tablename__ = 'file'
print_fields = ['name']

@property
def name(self):
return self.location.rsplit('/', 1)[-1]


class SymlinkFile(Base):
__tablename__ = 'symlink_file'
print_fields = ['location']


class SymlinkDirectory(Base):
__tablename__ = 'symlink_directory'
print_fields = ['location']


class ChecksumFile(Base):
__tablename__ = 'checksumfile'
print_fields = ['filename']


class Checksum(Base):
__tablename__ = 'checksum'


def define_relations():
"""Setup relationships after preparation."""

# model relationships
Root.directories = relationship(Directory, backref='root')
Tree.envs = relationship(Env, backref='tree')

# class Checksum
Checksum.tree = relationship(Tree, backref='checksums')
Checksum.checksumfile = relationship(ChecksumFile, backref='checksums')
Checksum.file = relationship(File, backref='checksums')
Checksum.directory = relationship(Directory, backref='checksums')

# class Checksumfile
ChecksumFile.tree = relationship(Tree, backref='checksumfiles')
ChecksumFile.env = relationship(Env, backref='checksumfile')
ChecksumFile.directory = relationship(Directory, backref='checksumfile')
ChecksumFile.file = relationship(File, backref='checksumfile')

# class Directory
Directory.tree = relationship(Tree, backref='directories')
Directory.env = relationship(Env, backref='directories')

# class Env
# need to specify remote_side when foreign key points to itself
Env.real_env = relationship(Env, remote_side='Env.id', backref='env')

# class File
File.root = relationship(Root, backref='files')
File.tree = relationship(Tree, backref='files')
File.env = relationship(Env, backref='files')
File.directory = relationship(Directory, backref='files')

# class Symlink_directory
SymlinkDirectory.env = relationship(Env, backref='symlink_directories')
SymlinkDirectory.root = relationship(Root, backref='symlink_directories')
# need to specify foreign_keys when multiple columns points to same key
# need to specify primaryjoin when there are multiple ways to join the tables
SymlinkDirectory.directory = relationship(
Directory, backref='symlink_directories', foreign_keys='SymlinkDirectory.directory_id')
SymlinkDirectory.real_directory = relationship(Directory, backref='linked_symlink_directories',
primaryjoin='and_(SymlinkDirectory.real_directory_id==Directory.id)')
havok2063 marked this conversation as resolved.
Show resolved Hide resolved

SymlinkDirectory.tree = relationship(Tree, backref='symlink_directories', foreign_keys='SymlinkDirectory.tree_id',
primaryjoin='and_(SymlinkDirectory.tree_id==Tree.id)')
SymlinkDirectory.real_tree = relationship(
Tree, backref='linked_symlink_directories', primaryjoin='and_(SymlinkDirectory.real_tree_id==Tree.id)')

# class Symlink_file
SymlinkFile.directory = relationship(Directory, backref='symlink_files')
SymlinkFile.env = relationship(Env, backref='symlink_files')
SymlinkFile.real_file = relationship(File, backref='symlink_files')
SymlinkFile.root = relationship(Root, backref='symlink_files', foreign_keys='SymlinkFile.root_id')

SymlinkFile.tree = relationship(Tree, backref='symlink_files', foreign_keys='SymlinkFile.tree_id',
primaryjoin='and_(SymlinkFile.tree_id==Tree.id)')
SymlinkFile.real_tree = relationship(
Tree, backref='linked_symlink_files', primaryjoin='and_(SymlinkFile.real_tree_id==Tree.id)')


# prepare the base
database.add_base(Base)
14 changes: 7 additions & 7 deletions python/sdssdb/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

from pytest import mark

from sdssdb.main import math
# from sdssdb.main import math


class TestMath(object):
"""Tests for the ``math`` function in main.py."""
# class TestMath(object):
# """Tests for the ``math`` function in main.py."""

@mark.parametrize(('arg1', 'arg2', 'operator', 'result'),
[(1, 2, '+', 3), (2, 2, '-', 0), (3, 5, '*', 15), (10, 2, '/', 5)])
def test_math(self, arg1, arg2, operator, result):
# @mark.parametrize(('arg1', 'arg2', 'operator', 'result'),
# [(1, 2, '+', 3), (2, 2, '-', 0), (3, 5, '*', 15), (10, 2, '/', 5)])
# def test_math(self, arg1, arg2, operator, result):

assert math(arg1, arg2, arith_operator=operator) == result
# assert math(arg1, arg2, arith_operator=operator) == result
Binary file added schema/archive/archive_sas.pdf
Binary file not shown.