2017-09-05 8 views
0

프로젝트 structue (DB 마이그레이션 만 디렉토리)에 문제가 있었나요 어떤 경우에는 그 문제가 있습니까?
P.
파일 alembic.ini :이유는 증류기 마이그레이션

# A generic, single database configuration. 

[alembic] 
# path to migration scripts 
script_location = alembic 

# template used to generate migration files 
# file_template = %%(rev)s_%%(slug)s 

# max length of characters to apply to the 
# "slug" field 
#truncate_slug_length = 40 

# set to 'true' to run the environment during 
# the 'revision' command, regardless of autogenerate 
# revision_environment = false 

# set to 'true' to allow .pyc and .pyo files without 
# a source .py file to be detected as revisions in the 
# versions/ directory 
# sourceless = false 

# version location specification; this defaults 
# to numus/versions. When using multiple version 
# directories, initial revisions must be specified with --version-path 
# version_locations = %(here)s/bar %(here)s/bat numus/versions 

# the output encoding used when revision files 
# are written from script.py.mako 
# output_encoding = utf-8 

sqlalchemy.url = postgresql://test:[email protected]/test 


# Logging configuration 
[loggers] 
keys = root,sqlalchemy,alembic 

[handlers] 
keys = console 

[formatters] 
keys = generic 

[logger_root] 
level = WARN 
handlers = console 
qualname = 

[logger_sqlalchemy] 
level = WARN 
handlers = 
qualname = sqlalchemy.engine 

[logger_alembic] 
level = INFO 
handlers = 
qualname = alembic 

[handler_console] 
class = StreamHandler 
args = (sys.stderr,) 
level = NOTSET 
formatter = generic 

[formatter_generic] 
format = %(levelname)-5.5s [%(name)s] %(message)s 
datefmt = %H:%M:%S 

파일 env.py :

from __future__ import with_statement 
from alembic import context 
from sqlalchemy import engine_from_config, pool 
from logging.config import fileConfig 

# this is the Alembic Config object, which provides 
# access to the values within the .ini file in use. 
config = context.config 

# Interpret the config file for Python logging. 
# This line sets up loggers basically. 
fileConfig(config.config_file_name) 

# add your model's MetaData object here 
# for 'autogenerate' support 
# from myapp import mymodel 
# target_metadata = mymodel.Base.metadata 
target_metadata = None 

# other values from the config, defined by the needs of env.py, 
# can be acquired: 
# my_important_option = config.get_main_option("my_important_option") 
# ... etc. 


def run_migrations_offline(): 
    """Run migrations in 'offline' mode. 

    This configures the context with just a URL 
    and not an Engine, though an Engine is acceptable 
    here as well. By skipping the Engine creation 
    we don't even need a DBAPI to be available. 

    Calls to context.execute() here emit the given string to the 
    script output. 

    """ 
    url = config.get_main_option("sqlalchemy.url") 
    context.configure(
     url=url, target_metadata=target_metadata, literal_binds=True) 

    with context.begin_transaction(): 
     context.run_migrations() 


def run_migrations_online(): 
    """Run migrations in 'online' mode. 

    In this scenario we need to create an Engine 
    and associate a connection with the context. 

    """ 
    connectable = engine_from_config(
     config.get_section(config.config_ini_section), 
     prefix='sqlalchemy.', 
     poolclass=pool.NullPool) 

    with connectable.connect() as connection: 
     context.configure(
      connection=connection, 
      target_metadata=target_metadata 
     ) 

     with context.begin_transaction(): 
      context.run_migrations() 

if context.is_offline_mode(): 
    run_migrations_offline() 
else: 
    run_migrations_online() 
+0

는 동일한 출력 – Mekicha

+0

@Mekicha 아무것도 변경하지'시도하여' alembic.ini'이 있습니다. 'alembic --config db_manage/alembic.ini ... '에서와 같이'-c' 또는'--config'를 사용하여 명시 적으로 전달하십시오. 이것이 근본 원인인지는 잘 모르겠지만 시도해 볼 가치가 있습니다. – Klimenkomud

+0

당신이 아닌 다른 디렉토리에서 명령을 실행하는 것 같다 것, 파이썬 db_manage/증류기/env.py --autogenerate' –

답변

1

마이그레이션 도구를 호출 할 길을 잘못 2 가지가 있습니다. 먼저 env.py을 직접 실행하지 말고 alembic 스크립트를 사용해야합니다. tutorial에서 :

env.py - 이것은 증류기 마이그레이션 도구가를 호출 할 때마다 을 실행하는 파이썬 스크립트입니다. 최소한 SQLAlchemy 엔진을 구성 및 생성하고 트랜잭션과 함께 해당 엔진에서 연결을 획득 한 다음 연결을 데이터베이스 연결 원본으로 사용하여 마이그레이션 엔진을 호출하는 지침이 포함되어 있습니다. 당신은 유지할 수 있지만 두 번째

, 당신의 기본 동작이 현재 디렉토리에보고 어디에 있든, alembic.ini :

증류기는 현재 디렉토리에 파일 alembic.ini을 두었다. 이 파일은 호출 될 때 alembic 스크립트가 찾는 파일입니다. 이 파일은 일반적으로 alembic 스크립트가 호출되는 디렉토리와 동일한 디렉토리에 있거나 다른 디렉토리에있는 경우 alembic 러너에 대해 --config 옵션을 사용하여 지정할 수 있습니다. 그것은 다른 곳에서 개최되는 경우

그래서, 당신은 -c, 또는 --config, 스위치를 사용하여 위치에 대한 증류기 지시해야합니다. 예를 들어 :

alembic --config db_manage/alembic.ini 
+0

그것은 많은 도움이되었지만 새로운 문제가 생겼습니다. 자격 증명이 잘못되었습니다. 'alembic upgrade head'를 실행하면 DB 비밀번호가 잘못되었다고 알려줍니다. 내가 바꿀 수있는 곳은 어디인가? 추신 바보 같은 질문에 대한 미안 해요, 나는 파이썬에서 새로운, 그리고 기존의 프로젝트를 설정하려고합니다. – Klimenkomud

+0

'sqlalchemy.'alembic.ini'의 url은 그것을 처리 할 곳입니다. 또한 필요한 경우 PostgreSQL 구성에서 ['pg_hba.conf'] (https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html)에 필요한 인증 방법을 허용하는지 확인하십시오. 있다. –

+0

네, 찾아 주셔서 대단히 감사합니다! 모두 작동하고있다. – Klimenkomud