-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support deferring configuration of database to after initialization #69
Comments
I found out the author is precisely that of Peewee, so I needn't have explained this much. :-) I also identified a problem. A deferred Peewee database still needs to have its type (sqlite/postgre/...) specified. Seems Peewee needs to be enhanced so that a deferred peewee.Database can has its type unknown. |
+1. My unit tests run twice as long because they are making a connection to the MySQL database for every simulated request. Using a config that points to an in-memory SQLite database doubles the speed, but it would probably still be much faster if it could be told not to connect until later. Here's the MySQL query log created by running unit tests:
|
What is the status of this feature? |
I need to fix this. Will try and get things cleaned up in the next day or two. |
+1. What's the workaround that people are doing when they use application factories? global instance? |
@mangrish Build a proxy class. A bit clumsy, though. |
Ah... not quite there yet. You still need to specify the app when initializing the database: import flask, flask_peewee
app = flask.Flask(__name__)
db = flask_peewee.Database(app) better make it possible to write this, a la Flask-SQLAlchemy: import flask, flask_peewee
db = flask_peewee.Database()
def create_app():
app = flask.Flask(__name__)
db.init_app(app)
return db I'll probably send a PR tonight (UTC+8 here :)... |
Ah, harder than I though...
So better implement deferred engine selection in peewee... :) |
Crossposting: I think that this issue is related to #103 It would be great if we could find a way to do configuration independent of a given app, and independent of the usage of flask at all (i.e., only use the peewee models in a separate non-web app). EDIT: I just realized that github does the pingback automagically for you, thus sorry for the noise. |
Anyone found a better, "clean" solution yet? |
No. Would be interested in this as well, same use-case as mentioned by @xiaq this time. Not sure if this can be solved without substantial changes in peewee. |
So, I found a (hacky) solution for this. In my app, I use a
Code needs of the package path and name, eg., the package with all your models. Not pretty, but it works. This allows us also to get around get_model_class and some other things from Database() |
Have you guys tried using the peewee |
Works like a charm. Much nicer than my hack (but I was soo proud! ;) ) |
Is there an example somewhere on how to use the |
Peewee allows you to do this, by passing
None
to__init__
:Flask-SQLAlchemy too, by leaving out the
app
argument:This is important if you need to create the
app
in a function instead of in the module global, which makes unit testing easier and is better practice. The complete form for the above example is:Deferring configuration of database still allows you to put
db
in the module global and usedb.Model
as base class for ORM models.The text was updated successfully, but these errors were encountered: