-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Added MySQL Support for DB options #3644
Conversation
✅ Deploy Preview for frigate-docs canceled.
|
@@ -34,7 +34,8 @@ RUN pip3 wheel --wheel-dir=/wheels \ | |||
matplotlib \ | |||
click \ | |||
setproctitle \ | |||
peewee | |||
peewee \ | |||
pymysql |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For which part is pymysql
needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pymysql is required by peewee when connecting to a MySQL DB, without this installed an Exception is raised
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting, good to know, I figured they would have bundled it but that makes sense 👍
frigate/app.py
Outdated
@@ -33,6 +35,8 @@ | |||
|
|||
logger = logging.getLogger(__name__) | |||
|
|||
class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With multiple db types being supported, probably makes sense to push this out to its own database.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can move the logic from frigate/app.py init_database to a database.py along with the import and recommit if you like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done the refactor to database.py, do i need to create a new pull request to merge the changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're all contained here so it's good as far as that goes 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah great! 👍
frigate/app.py
Outdated
router.run() | ||
|
||
migrate_db.close() | ||
if self.config.database.type == "sqlite": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, this can be moved to a database.py
file and pass the config to it 👍
frigate/config.py
Outdated
default="frigate", title="Database password." | ||
) | ||
port: int = Field( | ||
default=3306, title="Database password." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default=3306, title="Database password." | |
default=3306, title="Database server port." |
|
||
|
||
class Event(Model): | ||
id = CharField(null=False, primary_key=True, max_length=30) | ||
label = CharField(index=True, max_length=20) | ||
camera = CharField(index=True, max_length=20) | ||
start_time = DateTimeField() | ||
end_time = DateTimeField() | ||
end_time = DateTimeField(null=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found when testing, pymysql doesnt set DEFAULT NULL without specifying this.
frigate/events.py tries to set this as None when starting an event.
Thanks for your contribution! |
I'm not sure I want to add the overhead of compatibility with multiple database options. |
I've got another commit relating to this. |
If was 100% plug and play, then I would consider it. There are a few places where I use custom sql queries, and I am not sure they will work with anything other than sqlite. I just frankly don't have the bandwidth to commit to testing multiple databases and maintaining any db specific workarounds for each release. If you were willing to commit to maintaining it and including a robust test suite alongside this, then maybe. I would likely just remove support as soon as it became a burden for me. |
Personally I would rather see support using an ORM (like SQLAlchemy since this is python) rather than implementing a single database type. Postgres is a much more mature database over mariadb/mysql but feelings aside using an ORM would give people options to use either one in addition to sqlite. I feel the pain of having to support this though and don't see much benefit over using sqlite unless you have hundreds of cameras, in which case you might want to spin up multiple instances of frigate anyways. |
Hi @blakeblackshear I've detected a race condition, I have 2 cameras that overlaps a zone, then they both detects the same event, sometimes only ONE gets recorded even if it where detected. I know they where detected because the MQTT event arrives my HA installation. Also, when a single camera gets two events, where two objeccts are too close, coincidentially when a person is strolling a dog... Sometimes it get the dog, sometimes it get the person.... But not both as it should. BUT both are detected for a tiny space of time. I'm not reading the full code, but it could be possible the database locking is preventing multiple transactions? If thats the case. Maybe you should consider moving towards a more robust DB. Any volunteer to share the maintenance of a RDBMS with me? |
to be honest I doubt this has anything to do with the database. I've had scenarios like this plenty of times and I've never seen any issue with it recording all events to the DB that should have been recorded. Without seeing your config, the MQTT payloads, etc. there's no way to know this is related to the DB at all.
I'd suggest doing that because it seems the way you're assuming it works is incorrect. |
@luisgreen what you are describing doesn't sound like a race condition or database lock issue to me. I don't think this closed PR has anything to do with what you are seeing. If you want help troubleshooting what you are seeing, I suggest opening an issue and providing the requested information. |
Could this be re-opened? I'd really love to see an ORM implementation, where i could just plug in any SQL URL and let the library handle the connection and queries, because let's be honest: SQLite sucks. ;-) |
For this like myself who already have systems running with a MySQL db we'd like the Frigate data in