Merge pull request #54 from djrobstep/funcfix

change to new schemainspect function output
This commit is contained in:
djrobstep 2018-10-29 18:35:19 +11:00 committed by GitHub
commit e29573ec15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 191 additions and 133 deletions

86
.circleci/config.yml Normal file
View File

@ -0,0 +1,86 @@
version: 2
jobs:
build:
working_directory: ~/circleci
docker:
- image: circleci/python:3.7.0
- image: circleci/postgres:10
environment:
POSTGRES_USER: circleci
POSTGRES_DB: circleci
steps:
- checkout
- restore_cache:
key: deps1-{{ .Branch }}-{{ checksum "pyproject.toml" }}
- run:
name: Wait for db
command: dockerize -wait tcp://localhost:5432 -timeout 1m
- run: sudo apt-get install -y postgresql-client
- run:
name: create postgres user
command: psql postgresql://@localhost/circleci -c 'create role postgres'
- run:
name: Install poetry
command: |
sudo pip3 install poetry
poetry config settings.virtualenvs.create false
- run:
command: |
python3 -m venv ~/.venv
. ~/.venv/bin/activate
poetry install
- save_cache:
key: deps1-{{ .Branch }}-{{ checksum "pyproject.toml" }}
paths:
- "~/.venv"
- run:
name: Check formatting
command: |
. ~/.venv/bin/activate
make lint
- run:
command: |
. ~/.venv/bin/activate
make test
- store_artifacts:
path: test-reports/
destination: tr1
publish:
working_directory: ~/circleci
docker:
- image: circleci/python:3.7.0
steps:
- setup_remote_docker
- checkout
- restore_cache:
key: deps1-{{ .Branch }}-{{ checksum "pyproject.toml" }}
- run:
name: Install poetry, deps
command: |
sudo pip3 install poetry
poetry config settings.virtualenvs.create false
python3 -m venv ~/.venv
. ~/.venv/bin/activate
poetry install
- run:
name: Bump version, build, install
command: |
. ~/.venv/bin/activate
python deploy/vbump.py
poetry build
poetry publish --username $PYPI_USERNAME --password $PYPI_PASSWORD
workflows:
version: 2
build-then-publish:
jobs:
- build
- publish:
requires:
- build
filters:
branches:
only: master

2
.flake8 Normal file
View File

@ -0,0 +1,2 @@
[flake8]
ignore = E501, W503

1
.gitignore vendored
View File

@ -42,3 +42,4 @@ nosetests.xml
docs/site
scratch
poetry.lock

View File

@ -1,44 +1,26 @@
.PHONY: docs
# test commands and arguments
tcommand = PYTHONPATH=. py.test -x
tcommand = py.test -x
tmessy = -svv
targs = --cov-report term-missing --cov migra
pip:
pip install --upgrade pip
pip install --upgrade -r requirements.txt
tox:
tox tests
test:
$(tcommand) $(targs) tests
stest:
$(tcommand) $(tmessy) $(targs) tests
clean:
gitclean:
git clean -fXd
clean:
find . -name \*.pyc -delete
rm -rf .cache
docs:
cd docs && mkdocs build
docsserve:
cd docs && mkdocs serve
fmt:
isort -rc .
black .
lint:
flake8 .
tidy: clean lint
all: tidy docs tox
publish:
python setup.py sdist bdist_wheel --universal
twine upload dist/*

View File

@ -11,8 +11,6 @@
You can also detect changes for a single specific schema only with `--schema myschema`.
## Already use `migra`? [Let us know how you're using it and what features would make it more useful](https://github.com/djrobstep/migra/issues/25).
## Folks, schemas are good
Schema migrations are without doubt the most cumbersome and annoying part of working with SQL databases. So much so that some people think that schemas themselves are bad!

21
deploy/vbump.py Normal file
View File

@ -0,0 +1,21 @@
from pathlib import Path
from time import time
from toml import dumps, loads
PYPROJECT = "pyproject.toml"
p = Path(PYPROJECT)
pyproject = loads(p.read_text())
v = pyproject["tool"]["poetry"]["version"]
parts = v.split(".")[:2]
unix = str(int(time()))
parts.append(unix)
v_with_timestamp = ".".join(parts)
pyproject["tool"]["poetry"]["version"] = v_with_timestamp
p.write_text(dumps(pyproject))

View File

@ -1,10 +1,10 @@
from __future__ import unicode_literals
from .util import differences
from .statements import Statements
from functools import partial
from collections import OrderedDict as od
from functools import partial
from .statements import Statements
from .util import differences
THINGS = [
"schemas",

View File

@ -1,10 +1,11 @@
from __future__ import unicode_literals, print_function
from __future__ import print_function, unicode_literals
from sqlbag import S
import argparse
import sys
from contextlib import contextmanager
from sqlbag import S
from .migra import Migration
from .statements import UnsafeMigrationException
@ -78,7 +79,7 @@ def run(args, out=None, err=None):
try:
if m.statements:
if args.force_utf8:
print(m.sql.encode('utf8'), file=out)
print(m.sql.encode("utf8"), file=out)
else:
print(m.sql, file=out)
except UnsafeMigrationException:

View File

@ -1,7 +1,8 @@
from __future__ import unicode_literals
from schemainspect import DBInspector, get_inspector
from sqlbag import raw_execute
from schemainspect import get_inspector, DBInspector
from .changes import Changes
from .statements import Statements

View File

@ -4,7 +4,7 @@ import re
def check_for_drop(s):
return not not re.search("(drop\s+)", s, re.IGNORECASE)
return not not re.search(r"(drop\s+)", s, re.IGNORECASE)
class Statements(list):

33
pyproject.toml Normal file
View File

@ -0,0 +1,33 @@
[tool.poetry]
name = "migra"
version = "1.0"
authors = [ "Robert Lechte <robertlechte@gmail.com>",]
license = "Unlicense"
readme = "README.md"
description = "Like `diff` but for PostgreSQL schemas"
repository = "https://github.com/djrobstep/migra"
homepage = "https://migra.djrobstep.com/"
[tool.poetry.dependencies]
python = "*"
sqlbag = "*"
six = "*"
schemainspect = "*"
psycopg2-binary = { version="*", optional = true }
[tool.poetry.dev-dependencies]
sqlbag = "*"
pytest = "*"
pytest-cov = "*"
pytest-sugar = "*"
psycopg2-binary = "*"
flake8 = "*"
isort = "*"
black = {version = "*", python = ">=3.6"}
[tool.poetry.scripts]
migra = 'migra:do_command'
[tool.poetry.extras]
pg = ["psycopg2-binary"]

View File

@ -1,24 +0,0 @@
-e .
sqlbag
pytest
pytest-cov
pytest-xdist
mock
pylint
flake8
psycopg2-binary
tox
yapf
pep8
wheel
twine
mkdocs
doc2md
black ; python_version > "3.0.0"

View File

@ -1,32 +0,0 @@
#!/usr/bin/env python
import io
from setuptools import setup, find_packages
readme = io.open("README.md").read()
setup(
name="migra",
version="1.0.1535509978",
url="https://github.com/djrobstep/migra",
description="Like diff but for PostgreSQL schemas",
long_description=readme,
long_description_content_type="text/markdown",
author="Robert Lechte",
author_email="robertlechte@gmail.com",
install_requires=["sqlbag", "six", "schemainspect"],
zip_safe=False,
packages=find_packages(),
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Natural Language :: English",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: Implementation :: CPython",
],
entry_points={"console_scripts": ["migra = migra:do_command"]},
extras_require={"pg": ["psycopg2-binary"]},
)

View File

@ -110,27 +110,31 @@ alter table "public"."products" alter column "x" drop not null;
set check_function_bodies = off;
create or replace function "public"."newfunc"(i integer, t text[])
returns TABLE(a text, c integer) as
$$
CREATE OR REPLACE FUNCTION public.newfunc(i integer, t text[])
RETURNS TABLE(a text, c integer)
LANGUAGE plpgsql
STABLE STRICT
AS $function$
declare
BEGIN
select 'no', 1;
END;
$$
language PLPGSQL STABLE RETURNS NULL ON NULL INPUT SECURITY INVOKER;
$function$
create or replace function "public"."changed"(i integer, t text[])
returns TABLE(a text, c integer) as
$$
CREATE OR REPLACE FUNCTION public.changed(i integer, t text[])
RETURNS TABLE(a text, c integer)
LANGUAGE plpgsql
STRICT SECURITY DEFINER
AS $function$
declare
BEGIN
select 'no', 1;
END;
$$
language PLPGSQL VOLATILE RETURNS NULL ON NULL INPUT SECURITY DEFINER;
$function$
create materialized view "public"."matvvv" as SELECT 2;

View File

@ -106,27 +106,31 @@ alter table "public"."products" alter column "x" drop not null;
set check_function_bodies = off;
create or replace function "public"."newfunc"(i integer, t text[])
returns TABLE(a text, c integer) as
$$
CREATE OR REPLACE FUNCTION public.newfunc(i integer, t text[])
RETURNS TABLE(a text, c integer)
LANGUAGE plpgsql
STABLE STRICT
AS $function$
declare
BEGIN
select 'no', 1;
END;
$$
language PLPGSQL STABLE RETURNS NULL ON NULL INPUT SECURITY INVOKER;
$function$
create or replace function "public"."changed"(i integer, t text[])
returns TABLE(a text, c integer) as
$$
CREATE OR REPLACE FUNCTION public.changed(i integer, t text[])
RETURNS TABLE(a text, c integer)
LANGUAGE plpgsql
STRICT SECURITY DEFINER
AS $function$
declare
BEGIN
select 'no', 1;
END;
$$
language PLPGSQL VOLATILE RETURNS NULL ON NULL INPUT SECURITY DEFINER;
$function$
create materialized view "public"."matvvv" as SELECT 2;

0
tests/__init__.py Normal file
View File

View File

@ -3,12 +3,11 @@ from __future__ import unicode_literals
import io
from pytest import raises
from migra import Statements, UnsafeMigrationException, Migration
from migra.command import run
from sqlbag import temporary_database, S, load_sql_from_file
from schemainspect import get_inspector
from migra.command import parse_args
from sqlbag import S, load_sql_from_file, temporary_database
from migra import Migration, Statements, UnsafeMigrationException
from migra.command import parse_args, run
SQL = """select 1;
@ -72,7 +71,7 @@ def do_fixture_test(
flags += ["--with-privileges"]
fixture_path = "tests/FIXTURES/{}/".format(fixture_name)
EXPECTED = io.open(fixture_path + "expected.sql").read().strip()
with temporary_database() as d0, temporary_database() as d1:
with temporary_database(host='localhost') as d0, temporary_database(host='localhost') as d1:
with S(d0) as s0, S(d1) as s1:
load_sql_from_file(s0, fixture_path + "a.sql")
load_sql_from_file(s1, fixture_path + "b.sql")

18
tox.ini
View File

@ -1,18 +0,0 @@
# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
[tox]
envlist = py27,py37
toxworkdir = {homedir}/.toxfiles{toxinidir}
[testenv]
commands = py.test \
[] # substitute with tox positional arguments
deps =
-rrequirements.txt
[flake8]
ignore = E501