django_migrate&--fake: 迁移回滚/重新执行某一次迁移/伪回滚迁移(不执行相应的sql操作,仅修改执行状态标记)

migrate&–fake: 迁移回滚/重新执行某一次迁移/伪回滚迁移(不执行相应的sql操作,仅修改执行状态标记)

模型重置可能遇到的问题

如果您的其他代码已经引用了需要被重置的模型,这会稍微棘手一些,因为在您执行makemigrations的时候,会检查各个代码的依赖等是否正常,如果简单的删除模型,可能导致makemigrations无法完整执行
这样,就需要一步步将所有相关代码都注释掉,等到模型重置后,再将这些代码恢复(撤销屏蔽)
可能出现的地方有(将DRF一同考虑)

  • urls.py

  • serializer.py

  • views.py

  • 可以将这些文件都全部注释掉,模型重置完毕后,再解开注释

细节&步骤

  • 使用python manage.py showmigrations <appName>查看当前迁移执行情况
  • 回滚前,不要删除已有的migration(回滚要依赖于这些已经执行过的文件
  • 执行回滚命令不建议使用别名(直接使用全拼命令行,保证所有参数都能够正确被manage.py接收到)
    • python manage.py migrate <appName> <00xxx(replace it with your target migration file name code>
    • 重新使用showmigrations检查撤销情况
  • 删除掉已经被撤销执行的migration.py文件
    • 重新使用showmigrations检查撤销情况
  • 现在将最新的模型代码进行迁移
    • 执行python manage.py makemigrations <appName>
    • 以及python manage.py migrate <appName>
  • 如果全部成功,那么再次查看数据库(建议使用命令行,可以最及时和准确的查看刚刚迁移完毕的数据库)

撤销最近一次成功的迁移

场景实例(当前迁移情况)
# 执行迁移情况查看(当前的应用(django app)是user应用)
python manage.py showmigrations user

 [X] 0013_alter_wordsearchhistory_spelling_and_more
 [X] 0014_testforeignkey
 [X] 0015_delete_wordstar
 [X] 0016_wordstar
 [X] 0017_alter_user_signupdate
 [X] 0018_user_openid
 [ ] 0019_alter_user_openid

  • 现在,想要撤销掉最近一次成功的迁移(0018)
  • 我们需要执行的是python manage.py migrate 0017
  • 类似于git的代码版本控制回滚那样,将指针移动到了0017
🚀  python manage.py migrate user 0017

Operations to perform:
  Target specific migration: 0017_alter_user_signupdate, from user
Running migrations:
  Rendering model states... DONE
  Unapplying user.0018_user_openid... OK

回退效果
  • 可见,django 提示我们,0018的那一次迁移操作被撤销了
 [X] 0016_wordstar
 [X] 0017_alter_user_signupdate
 [ ] 0018_user_openid
 [ ] 0019_alter_user_openid

逆向fake

  • fake的反向操作
  • 需要注意的是语法(参数&选项的先后位置),否则起不到作用!

实操

PS D:\repos\ELA\backEnd\ela> manage migrate scoreImprover 0001 --fake
Operations to perform:
  Target specific migration: 0001_initial, from scoreImprover
Running migrations:
  Rendering model states... DONE
  Unapplying scoreImprover.0002_alter_cet4study_wid_alter_cet6study_wid... FAKED

可以看到,0002那一次迁移被伪撤回了

PS D:\repos\ELA\backEnd\ela> pmg showmigrations scoreImprover
scoreImprover
 [X] 0001_initial
 [ ] 0002_alter_cet4study_wid_alter_cet6study_wid

migrate 子命令手册

PS D:\repos\ELA\backEnd\ela> .\manage.py migrate  --fake -h      
usage: manage.py migrate [-h] [--noinput] [--database DATABASE] [--fake] [--fake-initial] [--plan] [--run-syncdb]
                         [--check] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH]
                         [--traceback] [--no-color] [--force-color] [--skip-checks]
                         [app_label] [migration_name]

Updates database schema. Manages both apps with migrations and those without.

positional arguments:
  app_label             App label of an application to synchronize the state.
  migration_name        Database state will be brought to the state after that migration. Use the name "zero" to
                        unapply all migrations.

options:
  -h, --help            show this help message and exit
  --noinput, --no-input
                        Tells Django to NOT prompt the user for input of any kind.
  --database DATABASE   Nominates a database to synchronize. Defaults to the "default" database.
  --fake                Mark migrations as run without actually running them.
  --fake-initial        Detect if tables already exist and fake-apply initial migrations if so. Make sure that     
                        the current database schema matches your initial migration before using this flag. Django  
                        will only check for an existing table name.
  --plan                Shows a list of the migration actions that will be performed.
  --run-syncdb          Creates tables for apps without migrations.
  --check               Exits with a non-zero status if unapplied migrations exist.
  --version             Show program's version number and exit.
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose       
                        output
  --settings SETTINGS   The Python path to a settings module, e.g. "myproject.settings.main". If this isn't        
                        provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions.
  --no-color            Don't colorize the command output.
  --force-color         Force colorization of the command output.
  --skip-checks         Skip system checks.

fake&fake-initial

  --fake                Mark migrations as run without actually running them.
  --fake-initial        Detect if tables already exist and fake-apply initial migrations if so. Make sure that     
                        the current database schema matches your initial migration before using this flag. Django  
  • First, to clear migrations table:(清理migrations 表,注意这里直接用的是migrate而非makemigrations)

    ./manage.py migrate --fake <app-name> zero
    
  • Remove app-name/migrations/ folder or contents.

  • Make the migrations:

    ./manage.py makemigrations <app-name>
    
  • Finally tidy up(整理) your migrations without making other database changes:

    ./manage.py migrate --fake <app-name>
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我想将frontend 也是用volumes,将其映射到/app/frontend目录,在/app/frontend下install以及build,如何实现 docker-compose.yml文件: version: '3' services: frontend: build: context: ./frontend dockerfile: Dockerfile ports: - 8010:80 restart: always backend: build: context: ./backend dockerfile: Dockerfile volumes: - /app/backend:/app environment: - CELERY_BROKER_URL=redis://redis:6379/0 command: python manage.py runserver 0.0.0.0:8000 ports: - 8011:8000 restart: always celery-worker: build: context: ./backend dockerfile: Dockerfile volumes: - /app/backend:/app environment: - CELERY_BROKER_URL=redis://redis:6379/0 command: celery -A server worker -l info --pool=solo --concurrency=1 depends_on: - redis - backend restart: always celery-beat: build: context: ./backend dockerfile: Dockerfile volumes: - /app/backend:/app environment: - CELERY_BROKER_URL=redis://redis:6379/0 command: celery -A server beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler depends_on: - redis - backend restart: always redis: image: redis:latest ports: - 6379:6379 restart: always mysql: image: mysql:latest environment: - MYSQL_ROOT_PASSWORD=sacfxSql258147@ ports: - 8016:3306 volumes: - ./mysql:/var/lib/mysql restart: always frontend:dockerfile文件 FROM node:16.18.1 WORKDIR /app/frontend COPY package*.json ./ RUN npm install COPY . . RUN npm run build:prod FROM nginx:latest COPY --from=0 /app/frontend/dist/ /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值