peewee-db-evolve icon indicating copy to clipboard operation
peewee-db-evolve copied to clipboard

Change sql commands order

Open evstratbg opened this issue 6 years ago • 5 comments
trafficstars

If run peeweedbevolve.evolve from the scratch, wo any table, there is an error, if other tables have foreign keys, cause of commands order create table-alter table-create (unique) index

In this PR I've change the order to create table-create (unique) index-alter table

And some pep8 fixes ;)

evstratbg avatar May 29 '19 16:05 evstratbg

can you resubmit w/o the code reformatting? it's impossible to tell what the change is here.

keredson avatar May 29 '19 16:05 keredson

ye, 1 min

evstratbg avatar May 29 '19 16:05 evstratbg

cool, thanks for the contribution!

so you moved two pieces:

  • create foreign keys
  • rename tables

the latter:

  for k, v in table_renames.items():
    to_run += rename_table(migrator, k, v)

will definitely break things, as that code in the middle is all done to make changes using the new table name instead of the old. (and likely that's the root cause for those test failures.)

the former seems a good idea tho. (i can't think of anything off hand that would have to happen after a FK creation.) is there a test case we could add that would differentiate the behaviors and/or highlight the original error you encountered?

keredson avatar May 29 '19 17:05 keredson

yes, here is sample of models.py to check

import peeweedbevolve
from peewee import (
    CharField, Model, AutoField, ForeignKeyField,
    PostgresqlDatabase
)
from playhouse.shortcuts import model_to_dict


PG_CONN = {
    'host': 'localhost',
    'database': os.environ.get('PG_DATABASE'),
    'user': os.environ.get('PG_USER'),
    'password': os.environ.get('PG_PASSWORD'),
    'autorollback': True,
}

db = PostgresqlDatabase(**PG_CONN)


class BaseModel(Model):

    def as_dict(self, **kwargs):
        return model_to_dict(self, **kwargs)

    def refresh(self):
        return type(self).get(self._pk_expr())

    class Meta:
        database = db


class Users(BaseModel):
    id = AutoField(unique=True)
    uid = CharField(unique=True, index=True)


class Sessions(BaseModel):
    id = AutoField(unique=True)
    user = ForeignKeyField(
        Users,
        to_field='uid',
        on_update='CASCADE',
        column_name='user_uid',
        backref='sessions',
        null=True
    )


if __name__ == '__main__':
    peeweedbevolve.evolve(db, interactive=False, ignore_tables=['basemodel'])

evstratbg avatar May 29 '19 17:05 evstratbg

So, no need to move

for k, v in table_renames.items(): to_run += rename_table(migrator, k, v)

just FK creation?

evstratbg avatar May 29 '19 17:05 evstratbg