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

Pony Migrations apply not working for initial table creation #378

Closed
alxmrs opened this issue Aug 21, 2018 · 1 comment
Closed

Pony Migrations apply not working for initial table creation #378

alxmrs opened this issue Aug 21, 2018 · 1 comment

Comments

@alxmrs
Copy link

alxmrs commented Aug 21, 2018

I'll reproduce the output of the workflow below for reproduction purposes. I have better information, however: I was able to pinpoint why the error was occurring and what to fix -- I just don't know where to make the change.

In migration.py::apply, we read from the database and then partition the migrations into two lists:

        with orm.db_session:
            applied = set(orm.select(m.name for m in migration_db.Migration))
            print(applied)

        applied_migrations = [m for m in migrations if m.name in applied]
        migrations_to_apply = [m for m in migrations if m.name not in applied]

I added a few print statements to see what was going on, namely this:
print('applied vs unapplied: ', applied_migrations, migrations_to_apply)
It revealed that the initial database schema creation was considered already applied:
applied vs unapplied: [Migration('0001_initial')] []

This, however, is my best guess as to what is going on. It's possible (better yet likely) that I have a configuration/setup issue and I'm just gung-ho about squashing bugs.

How to Reproduce

Here are the steps I took to reproduce the issue:

  1. Install the migrations branch of pony via ponyorm migration tool #16
    Here is my exact version (i.e. from pip list):
    pony 0.8.dev0

  2. Create entities in a database. I packaged mine in a function called init_db that takes in a configuration dictionary. It does not call bind, generate_mappings, nor connect, it merely defines the entities.

Here is my (approximate) configuration dictionary (for testing):

TEST = {
        'env': 'TEST',
        'debug': True,
        'migration_dir': 'migrations',
        'database': {
            'provider': 'sqlite',
            'filename': 'test.db',
            'create_db': True
        }
    }
  1. Create a quick migration script. Here is mine:
from <package>.config import TEST
from <package>.entities import init_db

init_db(TEST).migrate(migration_dir=TEST['migration_dir'], **TEST['database'])
  1. Call python migration.py make. Output:
GET NEW CONNECTION
RELEASE CONNECTION
Written: migrations/0001_initial.py
DISCONNECT

In my application an empty sqlite database is created. It has no tables besides the default.

  1. Call python migration.py apply. Output:
GET NEW CONNECTION
RELEASE CONNECTION
GET NEW CONNECTION
RELEASE CONNECTION
GET CONNECTION FROM THE LOCAL POOL
PRAGMA foreign_keys = false
BEGIN IMMEDIATE TRANSACTION
SELECT "migration"."id", "migration"."name", "migration"."applied"
FROM "migration" "migration"
WHERE 0 = 1

COMMIT
PRAGMA foreign_keys = true
CLOSE CONNECTION
GET NEW CONNECTION
SWITCH TO AUTOCOMMIT MODE
SELECT DISTINCT "m"."name"
FROM "migration" "m"

RELEASE CONNECTION

All migrations are applied.
DISCONNECT
DISCONNECT

Notice that All migrations are applied is reached. This means that the database doesn't get initialized -- it considers that migration to already be applied.

  1. Call python migration.py list to confirm:
GET NEW CONNECTION
RELEASE CONNECTION
GET CONNECTION FROM THE LOCAL POOL
SWITCH TO AUTOCOMMIT MODE
SELECT "m"."id", "m"."name", "m"."applied"
FROM "migration" "m"
LIMIT 1

No Migration table. Please apply the initial migration.
RELEASE CONNECTION
DISCONNECT
@sashaaero
Copy link
Member

Hello, @alxrsngrtn. Thanks for reporting. Seems that you have the same issue as one which was solved here. If it doesn't please respond. Don't forget to update pony from github.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants