Flask Migrate資料庫遷移

2021-07-14 04:55:29 字數 4955 閱讀 4861

在命令列中實現資料遷移, 工程放在f:/erp1/server中

開始準備工作:使用命令安裝flask-migrate包

pip install flask-migrate

第一步:進入工程目錄

c:\users\administrator>f:

f:\>cd f:\erp1\server

f:\erp1\server>

f:\erp1\server>dir

f:\erp1\server 的目錄

2016/06/28  10:26    .

2016/06/28  10:26    ..

2016/06/29  16:26    .idea

2016/06/27  16:59    config

2016/06/29  16:17    control_center

2016/06/27  16:05    data_mode

2016/06/27  16:06    event_bus

2016/06/27  16:05             3,836 manage.py

2016/06/27  16:59    public

2016/06/27  16:59                46 run.bat

2016/06/27  16:59    user_center

2016/06/27  16:06                 0 __init__.py

3 個檔案          3,882 位元組

9 個目錄 233,661,837,312 可用位元組

第二步:建立遷移倉庫,執行完該步驟之後,將在工程目錄下產生乙個名為migrations資料夾,所有遷移指令碼放在這裡面

f:\erp1\server>python manage.py db init

creating directory f:\erp1\server\migrations ... done

creating directory f:\erp1\server\migrations\versions ... done

generating f:\erp1\server\migrations\alembic.ini ... done

generating f:\erp1\server\migrations\env.py ... done

generating f:\erp1\server\migrations\env.pyc ... done

generating f:\erp1\server\migrations\readme ... done

generating f:\erp1\server\migrations\script.py.mako ... done

please edit configuration/connection/logging settings in 'f:\\erp1\\server\\migr

ations\\alembic.ini' before proceeding.

f:\erp1\server>python manage.py db

usage: perform database migrations

perform database migrations

positional arguments:

upgrade             upgrade to a later version

migrate             alias for 'revision --autogenerate'

current             display the current revision for each database.

stamp               'stamp' the revision table with the given revision;

don't run any migrations

init                generates a new migration

downgrade           revert to a previous version

history             list changeset scripts in chronological order.

revision            create a new revision file.

optional arguments:

-?, --help            show this help message and exit

第三步:建立遷移指令碼

f:\erp1\server>python manage.py db migrate

info  [alembic.runtime.migration] context impl mysqlimpl.

info  [alembic.runtime.migration] will assume non-transactional ddl.

info  [alembic.autogenerate.compare] detected added table 'admin_group'

info  [alembic.autogenerate.compare] detected added table 'admin_url'

info  [alembic.autogenerate.compare] detected added table 'organ_company'

info  [alembic.autogenerate.compare] detected added table 'admin_group_url'

info  [alembic.autogenerate.compare] detected added table 'admin_url_data'

info  [alembic.autogenerate.compare] detected added table 'organ_department'

info  [alembic.autogenerate.compare] detected added table 'admin_group_url_data_

curd'

info  [alembic.autogenerate.compare] detected added table 'organ_position'

info  [alembic.autogenerate.compare] detected added table 'admin_users'

info  [alembic.autogenerate.compare] detected added index 'ix_admin_users_email'

on '['email']'

info  [alembic.autogenerate.compare] detected added index 'ix_admin_users_teleph

one' on '['telephone']'

info  [alembic.autogenerate.compare] detected added index 'ix_admin_users_userna

me' on '['username']'

info  [alembic.autogenerate.compare] detected added index 'ix_admin_users_work_n

umber' on '['work_number']'

info  [alembic.autogenerate.compare] detected added table 'admin_user_group'

info  [alembic.autogenerate.compare] detected added table 'admin_user_parttime_p

osition'

generating f:\erp1\server\migrations\versions\ab42fba096ab_.py ... done

第四步:更新資料庫

f:\erp1\server>python manage.py db upgrade

info  [alembic.runtime.migration] context impl mysqlimpl.

info  [alembic.runtime.migration] will assume non-transactional ddl.

info  [alembic.runtime.migration] running upgrade  -> ab42fba096ab, empty messag

ef:\erp1\server>python manage.py

usage: manage.py [-?]

...positional arguments:

show_urls           displays all of the url matching routes for the

project

deploy              run deployment tasks.

db                  perform database migrations

init_url

clean               remove *.pyc and *.pyo files recursively starting at

current directory

get_table

optional arguments:

-?, --help            show this help message and exit

第五步:初始化路由,這個步驟只是針對此專案而言。其他的專案不需要這個步驟的。

f:\erp1\server>python manage.py init_url

****************************************

f:\erp1\server>

Flask Migrate資料庫遷移

資料庫遷移命令列操作 為什麼使用flask migrate 在我們用 sqlchemy 模組建立完幾個表時,如果在實際生產環境中,需要對錶結構進行更改,應該怎麼辦呢?總不能把錶刪除了吧,這樣資料就會丟失了。更好的解決辦法是使用資料庫遷移框架,它可以追蹤資料庫模式的變化,然後把變動應用到資料庫中。在f...

使用Flask Migrate進行管理資料庫公升級

我們在公升級系統的時候,經常碰到需要更新伺服器端資料結構等操作,之前的方式是通過手工編寫alter sql指令碼處理,經常會發現遺漏,導致程式發布到伺服器上後無法正常使用。現在我們可以使用flask migrate外掛程式來解決之,flask migrate外掛程式是基於alembic,alembi...

資料庫 資料庫索引

索引是儲存引擎用於快速找到記錄的一種資料結構。索引以檔案的形式儲存在磁碟中。索引可以包含乙個或多個列的值。儲存引擎查詢資料的時候,先在索引中找對應值,然後根據匹配的索引記錄找到對應的資料行。1.b tree索引 2.雜湊索引 myisam和innodb儲存引擎 只支援btree索引,也就是說預設使用...