How to disconnect from db and drop all tables?
See original GitHub issueHi, currently I am covering flask app with unit tests and I need to initialize database before each test starts and drop database after test is finished. But seems like there is no “easy” way to do it with PonyORM.
What I want is something like how it works with sqlalchemy http://pythonhosted.org/Flask-Testing/:
from flask.ext.testing import TestCase
from myapp import create_app, db
class MyTest(TestCase):
SQLALCHEMY_DATABASE_URI = "sqlite://"
TESTING = True
def create_app(self):
# pass in test configuration
return create_app(self)
def setUp(self):
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
Please advice: how can I disconnect from db and delete it? Is it possible?
Issue Analytics
- State:
- Created 10 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
How to drop all tables from a database with one SQL query?
Use the INFORMATION_SCHEMA.TABLES view to get the list of tables. Generate Drop scripts in the select statement and drop it using Dynamic ...
Read more >MySQL Drop All Tables: How-To With Examples - Database Star
Step 1: Generate a List of Drop Table Statements For All Tables · Step 2: Copy and Paste The Results Into a New...
Read more >How to drop all tables in MySQL? - TablePlus
First, disable foreign key check: echo "SET FOREIGN_KEY_CHECKS = 0;" > ./temp.sql. Then dump the db with no data and drop all tables:....
Read more >How to drop all tables in a MySQL database without dropping ...
If you have filesystem access, then just rm -rf databasename/* :). The Drop database statement ...
Read more >Drop all tables from database command apart from named few
Using such a CURSOR, you can print or execute all the DROP statements: DECLARE cur_del CURSOR FOR SELECT QUOTENAME(SCHEMA_NAME(schema_id))+' ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just added some methods which you can find useful:
db.drop_all_tables(with_all_data=True)
- allows to delete all Pony-related tables from the database;db.create_tables()
- creation of missing tables, indexes and foreign keys;db.drop_table('name')
,MyEntity.drop_table()
,MyEntity.my_collection.drop_table()
- drop a specific table;db.disconnect()
- close the database connection for the current thread if it is openedAlso, you can now generate mapping without generating the tables:
db.generate_mapping(create_tables=True)
- usual way, create tables if missed;db.generate_mapping()
- assuming tables are already created, check if existing tables look good;db.generate_mapping(check_tables=False)
- neither create nor check tables, useful if you plan to calldb.create_tables()
later.Now I want to show how tests can be written using this functionality. In this example I write two test functions (“create order” and “delete order”) for
pony.orm.examples.estore
example:In this example I don’t use the new
db.disconnect()
function, because it is easier for me to just drop tables in the already existed database. But you also can calldb.disconnect()
, remove SQLite database file and then call thedb.create_tables()
command.Feel free to ask any additional questions or tell me if I’m missing something.
Thank you for your reponse. You were correct about the cause of the problem. It was fixed by explicitly setting the table names via the
_table_
class attribute.I would expect
create_tables
anddrop_tables
to act on the same tables, i.e. ifdb.create_tables()``creates
person, then
db.drop_all_tables()deletes
person`.That said, I agree that drop_all_tables should not silently drop tables that have letter cases. I think the error message you suggested is fine.