Flask----数据库迁移指令

2020-09-06  本文已影响0人  幼姿沫

一 . 数据库迁移

flask_script Manager  扩展包 通过命令行的方式导入参数

python manager.py runserver -host  ip地址

flask_script配合迁移数据库命令使用 flask_migrate

pip install -i flask-migrate 使用国内镜像

操作数据库的指令

python manager.py runserver

创建扩展命令

app.add_command('db',MigradeCommand)

python manager.py db init      初始化数据库

python manager.py db migrate    生成迁移文件

python manager.py db upgrade    进行迁移数据库

修改表之后

python manager.py migrate

python manager.py upgrade

#直接降级到某个版本

python manager.py downgrade

二  ***  没有迁移命令 只是邮件创建数据库命令

from flaskimport Flask

from flask_sqlalchemyimport SQLAlchemy

app=Flask(__name__)

class Config(object):

DEBUG=True

    SQLALCHEMY_DATABASE_URI='mysql://root:root@localhost:3306/flask01'

    SQLALCHEMY_TRACK_MODIFICATIONS=True

    SQLALCHEMY_COMMIT_ON_TEAR=True

    SQLALCHEMY_ECHO=True

app.config.from_object(Config)

db=SQLAlchemy(app)

class Roles(db.Model):

__tablename__='roles'

    id=db.Column(db.Integer,autoincrement=True,primary_key=True)

name=db.Column(db.String(20),nullable=False)

def __init__(self,name):

self.name=name

    def __str__(self):

return "roles***name %s" %self.name

@app.route('/')

def  index():

return '

使用manager扩展命令来迁移数据库

'

if __name__ =='__main__':

db.create_all()

app.run()

控制台显示创建数据库

D:\python\first_flask\Scripts\python.exe D:/Flask项目设计/review_first/apps.py

2020-09-06 22:36:25,018 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:36:25,018 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:25,021 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'

2020-09-06 22:36:25,021 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:25,025 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:36:25,025 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:25,026 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:36:25,026 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:25,028 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1

2020-09-06 22:36:25,028 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:25,029 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1

2020-09-06 22:36:25,029 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:25,030 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:36:25,030 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:25,032 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:36:25,032 INFO sqlalchemy.engine.base.Engine ()

* Serving Flask app "apps" (lazy loading)

* Environment: production

  WARNING: This is a development server. Do not use it in a production deployment.

  Use a production WSGI server instead.

* Debug mode: on

* Restarting with stat

2020-09-06 22:36:27,347 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:36:27,347 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:27,350 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'

2020-09-06 22:36:27,351 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:27,354 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:36:27,354 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:27,355 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:36:27,355 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:27,357 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1

2020-09-06 22:36:27,357 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:27,358 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1

2020-09-06 22:36:27,358 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:27,358 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:36:27,358 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:36:27,359 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:36:27,360 INFO sqlalchemy.engine.base.Engine ()

* Debugger is active!

* Debugger PIN: 446-138-267

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

127.0.0.1 - - [06/Sep/2020 22:36:34] "GET / HTTP/1.1" 200 -

* Detected change in 'D:\\Flask项目设计\\review_first\\apps.py', reloading

* Restarting with stat

2020-09-06 22:37:32,357 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:37:32,357 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:37:32,361 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'

2020-09-06 22:37:32,361 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:37:32,364 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:37:32,364 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:37:32,365 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:37:32,365 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:37:32,366 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1

2020-09-06 22:37:32,367 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:37:32,367 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1

2020-09-06 22:37:32,367 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:37:32,368 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:37:32,368 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:37:32,369 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:37:32,369 INFO sqlalchemy.engine.base.Engine ()


数据库创建之后


显示数据库内容和结构

三  ****   使用迁移命令来迁移数据库

from flaskimport Flask

from flask_scriptimport Manager

from flask_sqlalchemyimport SQLAlchemy

from flask_migrateimport Migrate,MigrateCommand

app=Flask(__name__)

manager=Manager(app)

db=SQLAlchemy()

migrate=Migrate(app,db)

manager.add_command('db',MigrateCommand)

class Config(object):

DEBUG=True

    SQLALCHEMY_DATABASE_URI='mysql://root:root@localhost:3306/flask01'

    SQLALCHEMY_TRACK_MODIFICATIONS=True

    SQLALCHEMY_COMMIT_ON_TEAR=True

    SQLALCHEMY_ECHO=True

app.config.from_object(Config)

db=SQLAlchemy(app)

class Roles(db.Model):

__tablename__='roles'

    id=db.Column(db.Integer,autoincrement=True,primary_key=True)

name=db.Column(db.String(20),nullable=False)

def __init__(self,name):

self.name=name

    def __str__(self):

return "roles***name %s" %self.name

@app.route('/')

def  index():

return '

使用manager扩展命令来迁移数据库

'

if __name__ =='__main__':

db.create_all()

manager.run()


控制台显示

(first_flask) D:\Flask项目设计\review_first>python apps.py db init

2020-09-06 22:45:40,081 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:45:40,082 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:45:40,085 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_

names'

2020-09-06 22:45:40,085 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:45:40,089 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:45:40,089 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:45:40,092 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8

mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:45:40,092 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:45:40,096 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CH

AR(60)) AS anon_1

2020-09-06 22:45:40,097 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:45:40,100 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS

CHAR(60)) AS anon_1

2020-09-06 22:45:40,101 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:45:40,104 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS

CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:45:40,104 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:45:40,107 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:45:40,107 INFO sqlalchemy.engine.base.Engine ()

Creating directory D:\Flask项目设计\review_first\migrations ...  done

Creating directory D:\Flask项目设计\review_first\migrations\versions ...  done

Generating D:\Flask项目设计\review_first\migrations\alembic.ini ...  done

Generating D:\Flask项目设计\review_first\migrations\env.py ...  done

Generating D:\Flask项目设计\review_first\migrations\README ...  done

Generating D:\Flask项目设计\review_first\migrations\script.py.mako ...  done

Please edit configuration/connection/logging settings in 'D:\\Flask项目设计\\review_first\\migrat

ions\\alembic.ini' before proceeding.

(first_flask) D:\Flask项目设计\review_first>python apps.py db migrate

2020-09-06 22:46:30,307 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:46:30,307 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:46:30,311 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_

names'

2020-09-06 22:46:30,312 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:46:30,315 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:46:30,316 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:46:30,318 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8

mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:46:30,319 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:46:30,321 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CH

AR(60)) AS anon_1

2020-09-06 22:46:30,322 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:46:30,323 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS

CHAR(60)) AS anon_1

2020-09-06 22:46:30,323 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:46:30,325 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS

CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:46:30,325 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:46:30,327 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:46:30,327 INFO sqlalchemy.engine.base.Engine ()

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

INFO  [alembic.autogenerate.compare] Detected removed table 'roles'

Generating D:\Flask项目设计\review_first\migrations\versions\4499effb3483_.py ...  done

(first_flask) D:\Flask项目设计\review_first>python apps.py upgrade

2020-09-06 22:47:16,973 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:47:16,974 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:16,977 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_

names'

2020-09-06 22:47:16,978 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:16,982 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:47:16,983 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:16,984 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8

mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:47:16,985 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:16,987 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CH

AR(60)) AS anon_1

2020-09-06 22:47:16,988 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:16,990 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS

CHAR(60)) AS anon_1

2020-09-06 22:47:16,990 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:16,993 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS

CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:47:16,993 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:16,995 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:47:16,995 INFO sqlalchemy.engine.base.Engine ()

usage: apps.py [-?] {db,shell,runserver} ...

apps.py: error: invalid choice: 'upgrade' (choose from 'db', 'shell', 'runserver')

(first_flask) D:\Flask项目设计\review_first>python apps.py db upgrade

2020-09-06 22:47:25,474 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:47:25,474 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,479 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_

names'

2020-09-06 22:47:25,479 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,483 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:47:25,484 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,485 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8

mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:47:25,485 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,487 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CH

AR(60)) AS anon_1

2020-09-06 22:47:25,488 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,489 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS

CHAR(60)) AS anon_1

2020-09-06 22:47:25,490 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,492 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS

CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:47:25,493 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,496 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:47:25,496 INFO sqlalchemy.engine.base.Engine ()

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

INFO  [alembic.runtime.migration] Running upgrade  -> 4499effb3483, empty message

(first_flask) D:\Flask项目设计\review_first>python apps.py db upgrade

2020-09-06 22:47:25,474 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'

2020-09-06 22:47:25,474 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,479 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_

names'

2020-09-06 22:47:25,479 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,483 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()

2020-09-06 22:47:25,484 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,485 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8

mb4' and `Collation` = 'utf8mb4_bin'

2020-09-06 22:47:25,485 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,487 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CH

AR(60)) AS anon_1

2020-09-06 22:47:25,488 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,489 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS

CHAR(60)) AS anon_1

2020-09-06 22:47:25,490 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,492 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS

CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1

2020-09-06 22:47:25,493 INFO sqlalchemy.engine.base.Engine ()

2020-09-06 22:47:25,496 INFO sqlalchemy.engine.base.Engine DESCRIBE `roles`

2020-09-06 22:47:25,496 INFO sqlalchemy.engine.base.Engine ()

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

INFO  [alembic.runtime.migration] Running upgrade  -> 4499effb3483, empty message

数据库版本显示 roles表格展示
上一篇下一篇

猜你喜欢

热点阅读