alembic

Altering an Enum field using Alembic

独自空忆成欢 提交于 2019-11-30 10:29:44
问题 How can I add an element to an Enum field in an alembic migration when using a version of PostgreSQL older than 9.1 (which adds the ALTER TYPE for enums)? This SO question explains the direct process, but I'm not quite sure how best to translate that using alembic. This is what I have: new_type = sa.Enum('nonexistent_executable', 'output_limit_exceeded', 'signal', 'success', 'timed_out', name='status') old_type = sa.Enum('nonexistent_executable', 'signal', 'success', 'timed_out', name='status

How to represent a custom PostgreSQL domain in SQLAlchemy?

十年热恋 提交于 2019-11-30 09:06:57
I'm beginning to incorporate Alembic into my project which already uses SQLAlchemy table definitions. At present my DB schema is managed external to my application, and I want to bring the entire schema into my table definitions file. In PostgreSQL I use a custom domain for storing email addresses. The PostgreSQL DDL is: CREATE DOMAIN email_address TEXT CHECK (value ~ '.+@.+') How do I represent the creation of this domain, and the usage of it as a column data type, in SQLAlchemy? This likely far from a working solution, but I think the best way to do this would be subclass sqlalchemy.schema.

How do I get alembic to emit custom DDL on after_create?

ⅰ亾dé卋堺 提交于 2019-11-30 09:02:35
I've got a couple of custom DDL statements that I want to run after create table: update_function = DDL(""" CREATE OR REPLACE FUNCTION update_timestamp() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = now(); RETURN NEW; END; $$ language 'pgplsql'; """) update_trigger = DDL(""" CREATE TRIGGER update %(table)s_timestamp BEFORE UPDATE ON %(table)s FOR EACH ROW EXECUTE PROCEDURE update_timestamp(); """) And I've attached them like this: event.listen(Session.__table__, 'after_create', update_function) event.listen(Session.__table__, 'after_create', update_trigger) When I do create_all , I get the SQL

Alembic support for multiple Postgres schemas

末鹿安然 提交于 2019-11-30 08:54:00
How can I use Alembic's --autogenerate to migrate multiple Postgres schemas that are not hard-coded in the SQL Alchemy model? (mirror question of SQLAlchemy support of Postgres Schemas , but for Alembic). In particular, we use Postgres schemas to seperate different clients that share the same set of tables. Moreover, there is a schema with shared stuff among clients. The SQL Alchemy model has no knowledge of schemas, the schema is set at run-time using session.execute("SET search_path TO client1,shared") . The default --autogenerate is not helping at all, as it is detecting multiple schemas

Alembic: alembic revision says Import Error

拟墨画扇 提交于 2019-11-30 08:22:07
I am trying to integrate my Flask project with Alembic My application structure looks like project/ configuration/ __init__.py dev.py test.py core/ # all source code db/ migrations/ __init__.py alembic.ini env.py versions/ When I try to run the following from my db directory, I see File "migration/env.py", line 55, in run_migrations_online from configuration import app, db ImportError: No module named configuration I tried the solution mentioned in Request a simple alembic working example for Auto Generating Migrations , but it does not work for me My method in env.py run_migrations_online()

Check if a table column exists in the database using SQLAlchemy and Alembic

和自甴很熟 提交于 2019-11-30 07:16:03
问题 I'm using Alembic as migration tool and I'm launching the following pseudo script on an already updated database (no revision entries for Alembic, the database schema is just up to date). revision = '1067fd2d11c8' down_revision = None from alembic import op import sqlalchemy as sa def upgrade(): op.add_column('box', sa.Column('has_data', sa.Boolean, server_default='0')) def downgrade(): pass It gives me the following error only with PostgreSQL behind (it's all good with MySQL): INFO [alembic

No changes detected in Alembic autogeneration of migrations with Flask-SQLAlchemy

巧了我就是萌 提交于 2019-11-30 05:01:20
I'm having trouble getting Alembic to autogenerate candidate migrations from changes to classes using db.Model (Flask-SQLAlchemy) instead of Base . I've modified env.py to create my Flask app, import all relevant models, initialize the database, and then run migrations: ... uri = 'mysql://user:password@host/dbname?charset=utf8' app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = uri app.config['SQLALCHEMY_ECHO'] = True db.init_app(app) with app.test_request_context(): target_metadata = db.Model.metadata config.set_main_option('sqlalchemy.url', uri) if context.is_offline_mode(): run

How to handle python packages with conflicting names?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 04:56:05
I'm using two python packages that have the same name. http://www.alembic.io/updates.html https://pypi.python.org/pypi/alembic Is there a canonical or pythonic way to handle installing two packages with conflicting names? So far, I've only occasionally needed one of the packages during development/building, so I've been using a separate virtualenv to deal with the conflict, but it makes the build step more complex and I wonder if there isn't a better way to handle it. You could use the --target option for pip and install to an alternate location: pip install --target=/tmp/test/lib/python3.6

Is it possible to store the alembic connect string outside of alembic.ini?

亡梦爱人 提交于 2019-11-29 21:15:35
I'm using Alembic with SQL Alchemy. With SQL Alchemy, I tend to follow a pattern where I don't store the connect string with the versioned code. Instead I have file secret.py that contains any confidential information. I throw this filename in my .gitignore so it doesn't end up on GitHub. This pattern works fine, but now I'm getting into using Alembic for migrations. It appears that I cannot hide the connect string. Instead in alembic.ini, you place the connect string as a configuration parameter : # the 'revision' command, regardless of autogenerate # revision_environment = false sqlalchemy

Altering an Enum field using Alembic

不羁的心 提交于 2019-11-29 20:37:01
How can I add an element to an Enum field in an alembic migration when using a version of PostgreSQL older than 9.1 (which adds the ALTER TYPE for enums)? This SO question explains the direct process, but I'm not quite sure how best to translate that using alembic. This is what I have: new_type = sa.Enum('nonexistent_executable', 'output_limit_exceeded', 'signal', 'success', 'timed_out', name='status') old_type = sa.Enum('nonexistent_executable', 'signal', 'success', 'timed_out', name='status') tcr = sa.sql.table('testcaseresult', sa.Column('status', new_type, nullable=False)) def upgrade():