tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
825 stars 93 forks source link

Problems running aerich from terminal #106

Closed athanhat closed 3 years ago

athanhat commented 3 years ago

Hi, I have read the documentation about Tortoise migration with Aerich but it's not clear what is the format of the file you create with this TORTOISE_ORM variable such as the one below, unfortunately reading aerich documentation did not help me to understand how you do the setup. This is what I tried, I created TEXT file aerich.ini

[CONFIG]
TORTOISE_ORM = {
    "connections": {"default": "postgres://athan:123@localhost:5432/testdb"},
    "apps": {
        "models": {
            "models": ["models", "aerich.models"],
            "default_connection": "default"
        },
    },
}

and then

aerich init -t aerich.ini
You have inited

aerich init-db
configparser.ParsingError: Source contains parsing errors: 'aerich.ini'
        [line 10]: '}\n'

so can you write an example of a correct config file like the one I am using above. And BTW even this command "riddle" doesn't help a newbie like me :-)

aerich init -h
-t, --tortoise-orm TEXT  Tortoise-ORM config module dict variable, like settings.TORTOISE_ORM.  [required]

Even better can you share a working boilerplate example with a models.py file that includes tortoise models ? That will be great.

PS: When I run python scripts, this is my database.py file and it generates aerich models and tortoise models tables inside my database fine. But I cannot figure out how to do the same using aerich commands in the terminal.

#
# database.py
#

import logging
from tortoise import Tortoise, run_async
from srv_users.config import settings

log = logging.getLogger(__name__)

async def init_db():
    # Initialize database and models
    await Tortoise.init(db_url=settings.db_dsn, modules={'models': ['tortoise_models', 'aerich.models']})

async def generate_schema() -> None:
    log.info("Initializing Tortoise...")
    await init_db()

    log.info("Generating database schema via Tortoise...")
    await Tortoise.generate_schemas()

    await Tortoise.close_connections()

if __name__ == "__main__":
    run_async(generate_schema())
athanhat commented 3 years ago

Hi, I have looked into a relevant issue #88 and I figured out what to do. In my case all my python code files, etc.. are inside a python package that is called srv_users, call it app if you like. Inside that folder:

  1. I created aerich.ini

    [aerich]
    tortoise_orm = srv_users.database.TORTOISE_ORM
    location = .

    and I placed the dictionary variable TORTOISE_ORM inside my database.py, you could place it in config.py if you like

  2. 
    #
    # database.py
    #

TORTOISE_ORM = { "connections": {"default": "postgres://athan:123@localhost:5432/testdb"}, "apps": { "models": { "models": ["srv_users.models", "aerich.models"], "default_connection": "default" }, }, }

Now I am ready to run aerich commands in the terminal, start with

3. aerich init -t aerich.ini
This is required so that `aerich` knows where to find the TORTOISE_ORM configuration variable. Migration location (.) is also mandatory.

4. aerich init-db
I am running it on a newly created database (testdb)
Success create app migrate location models
Success generate schema for app "models"
This command generates the schema inside my postgres database and creates a models folder according to migrate location.

This is my tree after the execution of (4)
```txt
srv_users
├── aerich.ini
├── api_items.py
├── api_users.py
├── config.py
├── database.py
├── __init__.py
├── main.py
├── models
│   └── 0_20201220215143_init.sql
├── models.py
└── populate.py
  1. aerich migrate That is the point where I got the following error
    tortoise.exceptions.ConfigurationError: Module "models.old_models" not found

    It seems I am doing something wrong with the migrate location and the running of init-db and the name, location of my models.py ??? This models.old_models has been created but it is not found....

There is a hint from the author but I cannot understand what it says...

# If your Tortoise-ORM app is not default models, you must specify --app like 
aerich --app other_models init-db.

So perhaps someone can help to solve that last bit and complete this aerich migration example. I recommend that a similar example with the folder tree structure, configuration files, etc is added to tortoise, aerich code base so that others don't loose time to figure out how to run migrations and configure correctly tortoise.

Hey I forgot to say thank you for sharing this great ORM with all of us.

athanhat commented 3 years ago

Finally I have found how to do migrations with aerich using the specific file structure and content that I describe in this issue. The specific problem here is with the initialisation of aerich. First delete the aerich.ini file above and create a new one with the command:

aerich init -t database.TORTOISE_ORM

The content of the new aerich.ini

[aerich]
tortoise_orm = database3.TORTOISE_ORM
location = ./migrations

Now each time you modify Tortoise models in models.py run aerich migration commands:

aerich migrate aerich upgrade

That now works !

athanhat commented 3 years ago

Suggestion: For the author/maintainers of this package: Include a simple working boilerplate test/example in your aerich release, like the one I described above. BTW it was difficult to find an aerich working example, looking at cookiecutter and/or docker deployments. And if possible make concrete examples in the documentation on how to use the aerich init and aerich -app commands. You may also close this issue