Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,42 @@ jobs:
poetry config pypi-token.pypi $PYPI_PASSWORD
poetry publish

pre-commit:
working_directory: ~/circleci
docker:
- image: cimg/python:3.10
steps:
- setup_remote_docker
- checkout
- restore_cache:
keys:
- v1-pre-commit-pip
- run:
name: Install pre-commit
command: |
python -m pip install -U pip pre-commit
- save_cache:
key: v1-pre-commit-pip
paths:
- .venv
- ~/.cache/pip
- restore_cache:
keys:
- v1-pre-commit-env-{{ checksum ".pre-commit-config.yaml" }}
- v1-pre-commit-env-
- run:
name: Run pre-commit
command: |
python -m pre_commit run --show-diff-on-failure --color=always --all-files
- save_cache:
key: v1-pre-commit-env-{{ checksum ".pre-commit-config.yaml" }}
paths: ~/.cache/pre-commit

workflows:
version: 2
lint:
jobs:
- pre-commit
build-then-publish:
jobs:
- build-pg14
Expand Down
47 changes: 47 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-shebang-scripts-are-executable
- id: check-symlinks
- id: check-toml
- id: check-vcs-permalinks
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: destroyed-symlinks
- id: detect-aws-credentials
args: [--allow-missing-credentials]
- id: detect-private-key
# - id: double-quote-string-fixer
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: fix-encoding-pragma
args: [--remove]
# - id: file-contents-sorter
# - id: forbid-new-submodules
- id: mixed-line-ending
args: ["--fix", "no"]
# - id: name-tests-test
# - id: no-commit-to-branch
# - id: pretty-format-json
# - id: requirements-txt-fixer
# - id: sort-simple-yaml
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
11 changes: 4 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "schemainspect"
version = "3.1"
authors = [ "Robert Lechte <[email protected]>",]
authors = ["Robert Lechte <[email protected]>"]
license = "Unlicense"
readme = "README.md"
description = "Schema inspection for PostgreSQL (and possibly others)"
Expand All @@ -11,7 +11,6 @@ homepage = "https://github.com/djrobstep/schemainspect"

[tool.poetry.dependencies]
python = ">=3.7,<4"
six = "*"
sqlalchemy = "*"

[tool.poetry.dev-dependencies]
Expand All @@ -21,15 +20,13 @@ pytest-cov = "*"
pytest-clarity = "*"
psycopg2-binary = "*"
flake8 = "*"
isort = "*"
isort = "5.10.1"
migra = "*"
black = "*"
black = "22.3.0"
toml = "*"

[tool.poetry.scripts]
schemainspect = 'schemainspect:do_command'

[tool.isort]
multi_line_output = 3
include_trailing_comma = true
line_length = 88
profile = "black"
29 changes: 17 additions & 12 deletions schemainspect/graphlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
__all__ = ["TopologicalSorter", "CycleError"]

from typing import Dict, List, Optional, Set, TypeAlias

_NODE_OUT = -1
_NODE_DONE = -2


class _NodeInfo:
__slots__ = "node", "npredecessors", "successors"

def __init__(self, node):
def __init__(self, node: str):
# The node this class is augmenting.
self.node = node

Expand All @@ -18,7 +20,7 @@ def __init__(self, node):

# List of successor nodes. The list can contain duplicated elements as
# long as they're all reflected in the successor's npredecessors attribute).
self.successors = []
self.successors: List[str] = []


class CycleError(ValueError):
Expand All @@ -35,27 +37,30 @@ class CycleError(ValueError):
pass


Graph: TypeAlias = Dict[str, List[str]]


class TopologicalSorter:
"""Provides functionality to topologically sort a graph of hashable nodes"""

def __init__(self, graph=None):
self._node2info = {}
self._ready_nodes = None
def __init__(self, graph: Optional[Graph] = None):
self._node2info: Dict[str, _NodeInfo] = {}
self._ready_nodes: Optional[List[str]] = None
self._npassedout = 0
self._nfinished = 0

if graph is not None:
for node, predecessors in graph.items():
self.add(node, *predecessors)

def _get_nodeinfo(self, node):
def _get_nodeinfo(self, node: str):
result = self._node2info.get(node)

if result is None:
self._node2info[node] = result = _NodeInfo(node)
return result

def add(self, node, *predecessors):
def add(self, node: str, *predecessors: str):
"""Add a new node and its predecessors to the graph.

Both the *node* and all elements in *predecessors* must be hashable.
Expand Down Expand Up @@ -147,7 +152,7 @@ def is_active(self):
def __bool__(self):
return self.is_active()

def done(self, *nodes):
def done(self, *nodes: str):
"""Marks a set of nodes returned by "get_ready" as processed.

This method unblocks any successor of each node in *nodes* for being returned
Expand Down Expand Up @@ -204,10 +209,10 @@ def done(self, *nodes):

def _find_cycle(self):
n2i = self._node2info
stack = []
itstack = []
seen = set()
node2stacki = {}
stack: List[str] = []
itstack: List[str] = []
seen: Set[str] = set()
node2stacki: Dict[str, int] = {}

for node in n2i:
if node in seen:
Expand Down
4 changes: 1 addition & 3 deletions schemainspect/misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inspect
from reprlib import recursive_repr

import six
from pkg_resources import resource_stream as pkg_resource_stream


Expand All @@ -17,8 +16,7 @@ def connection_from_s_or_c(s_or_c): # pragma: no cover
return s_or_c


@six.python_2_unicode_compatible
class AutoRepr(object): # pragma: no cover
class AutoRepr: # pragma: no cover
@recursive_repr()
def __repr__(self):
done = set()
Expand Down
4 changes: 0 additions & 4 deletions schemainspect/pg/obj.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from collections import OrderedDict as od
from itertools import groupby

Expand Down Expand Up @@ -1238,9 +1237,6 @@ def dependency_order(
enums=True,
include_fk_deps=False,
):
if sys.version_info < (3, 0):
raise NotImplementedError

from schemainspect import TopologicalSorter

graph, things = {}, {}
Expand Down
6 changes: 3 additions & 3 deletions schemainspect/pg/sql/constraints.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ select
pg_attribute ta
join unnest(conkey) with ordinality c(cn, rn)

on
on
ta.attrelid = conrelid and ta.attnum = c.cn
)
else null end as fk_columns_local,
Expand All @@ -72,7 +72,7 @@ select
pg_attribute ta
join unnest(confkey) with ordinality c(cn, rn)

on
on
ta.attrelid = confrelid and ta.attnum = c.cn
)
else null end as fk_columns_foreign,
Expand Down Expand Up @@ -102,4 +102,4 @@ from
where true
-- SKIP_INTERNAL and nspname not in ('pg_internal', 'pg_catalog', 'information_schema', 'pg_toast', 'pg_temp_1', 'pg_toast_temp_1')
-- SKIP_INTERNAL and e.objid is null and er.objid is null and cr.objid is null
order by 1, 3, 2;
order by 1, 3, 2;
2 changes: 1 addition & 1 deletion schemainspect/pg/sql/deps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ combined as (
select * from combined
order by
schema, name, identity_arguments, kind_dependent_on,
schema_dependent_on, name_dependent_on, identity_arguments_dependent_on
schema_dependent_on, name_dependent_on, identity_arguments_dependent_on
4 changes: 2 additions & 2 deletions schemainspect/pg/sql/domains.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ with extension_oids as (
pg_depend d
WHERE
d.refclassid = 'pg_extension'::regclass and
d.classid = 'pg_type'::regclass
d.classid = 'pg_type'::regclass
)
SELECT n.nspname as "schema",
t.typname as "name",
Expand All @@ -26,4 +26,4 @@ WHERE t.typtype = 'd'
AND n.nspname <> 'information_schema'
AND pg_catalog.pg_type_is_visible(t.oid)
and t.oid not in (select * from extension_oids)
ORDER BY 1, 2;
ORDER BY 1, 2;
2 changes: 1 addition & 1 deletion schemainspect/pg/sql/relations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ with extension_oids as (
pg_depend d
WHERE
d.refclassid = 'pg_extension'::regclass and
d.classid = 'pg_class'::regclass
d.classid = 'pg_class'::regclass
), enums as (

SELECT
Expand Down
2 changes: 1 addition & 1 deletion schemainspect/pg/sql/schemas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ from
pg_catalog.pg_namespace
left outer join extension_oids e
on e.objid = oid
-- SKIP_INTERNAL where nspname not in ('pg_internal', 'pg_catalog', 'information_schema', 'pg_toast')
-- SKIP_INTERNAL where nspname not in ('pg_internal', 'pg_catalog', 'information_schema', 'pg_toast')
-- SKIP_INTERNAL and nspname not like 'pg_temp_%' and nspname not like 'pg_toast_temp_%'
-- SKIP_INTERNAL and e.objid is null
order by 1;
7 changes: 1 addition & 6 deletions schemainspect/tableformat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import sys

if sys.version_info >= (3, 0):
from itertools import zip_longest
else:
from itertools import izip_longest as zip_longest # noqa
from itertools import zip_longest


def transposed(in_data):
Expand Down
5 changes: 0 additions & 5 deletions tests/test_deps_fk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

from sqlbag import S

from schemainspect import get_inspector
Expand Down Expand Up @@ -83,9 +81,6 @@ def test_dep_order(db):

i = get_inspector(s)

# dependency_order doesn't work in py2
if sys.version_info < (3, 0):
return
create_order = i.dependency_order(
include_fk_deps=True,
)
Expand Down