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

Import pre-migration data from an SQL file #81

Closed
vesnikos opened this issue May 11, 2020 · 3 comments
Closed

Import pre-migration data from an SQL file #81

vesnikos opened this issue May 11, 2020 · 3 comments

Comments

@vesnikos
Copy link

Hi, is it possible to use an sql extract to test the migration procedure? ie

class TestDirectMigration(MigratorTestCase,_TestUtils):
    """This class is used to test direct migrations."""
    migrate_from = ('app', '0016_auto_20200416_1527')
    migrate_to = ('app', '0017_auto_20200511_1641')

    def prepare(self):
        """Prepare some data before the migration."""
        test_data = self.resource_path('migration_test_data/0016_auto_20200416_1527.sql')
        with connection.cursor() as cursor:
            for l in test_data.read_text().strip().split(';'):
                cursor.execute(l)
@skarzi
Copy link
Collaborator

skarzi commented May 11, 2020

Hello @vesnikos

What do you mean by "sql extract"? SQL EXTRACT function or just executing some raw SQL queries?
Generally you can do whatever you want in .prepare() to setup some test data for instance, you can execute raw SQL queries like in your example

@vesnikos
Copy link
Author

HI @skarzi ,

Yes, sorry. by extract I was refering to a partial data-only dump from the database before any migrations. That .sql file over there, was just containing a bunch of import statments generated from our live database.

If that's the case and could be done, it would be great there was a reference of such use case in the documentation.

Using file-fixtures to populate the table makes sense if you have multiple tables and inputing mock data is cumbersome. For that case django already supports test fixtures, but now as I write this, I am kinda confused: in which state a test-fixture would be applied at? surely it should be the before-state right?

@skarzi
Copy link
Collaborator

skarzi commented May 13, 2020

For that case django already supports test fixtures, but now as I write this, I am kinda confused: in which state a test-fixture would be applied at? surely it should be the before-state right?

It should be applied after calling .before()(example) or inside .prepare() when using unittest testcase (example), because .before() goal is to prepare database state without migration that is being tested applied, so you can populate all necessary data to test this migration.

Generally you can do whatever you want there, even load execute some raw SQL queries, however please remember that tables scheme can change, so you will have to version somehow your SQL files and solve some other challenges related to maintaining SQL files.

@skarzi skarzi closed this as completed Dec 23, 2020
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