Skip to content
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

Closed

Conversation

DevelopIdeas
Copy link

For this like myself who already have systems running with a MySQL db we'd like the Frigate data in

@netlify
Copy link

netlify bot commented Aug 14, 2022

Deploy Preview for frigate-docs canceled.

Name Link
🔨 Latest commit fe599a2
🔍 Latest deploy log https://app.netlify.com/sites/frigate-docs/deploys/62f9232a8b77eb0008a4be3c

@@ -34,7 +34,8 @@ RUN pip3 wheel --wheel-dir=/wheels \
matplotlib \
click \
setproctitle \
peewee
peewee \
pymysql
Copy link
Sponsor Collaborator

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?

Copy link
Author

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

Copy link
Sponsor Collaborator

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):
Copy link
Sponsor Collaborator

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

Copy link
Author

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?

Copy link
Author

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?

Copy link
Sponsor Collaborator

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 👍

Copy link
Author

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":
Copy link
Sponsor Collaborator

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 Show resolved Hide resolved
default="frigate", title="Database password."
)
port: int = Field(
default=3306, title="Database password."
Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Sponsor Collaborator

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?

Copy link
Author

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.

@NickM-27
Copy link
Sponsor Collaborator

Thanks for your contribution!

@blakeblackshear
Copy link
Owner

I'm not sure I want to add the overhead of compatibility with multiple database options.

@DevelopIdeas
Copy link
Author

I'm not sure I want to add the overhead of compatibility with multiple database options.

I've got another commit relating to this.
Should I send it over or not bother if you're considering not adding support?

@blakeblackshear
Copy link
Owner

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.

@onedr0p
Copy link
Contributor

onedr0p commented Aug 28, 2022

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.

@github-actions github-actions bot added the stale label Sep 28, 2022
@github-actions github-actions bot closed this Oct 2, 2022
@NickM-27 NickM-27 mentioned this pull request Nov 3, 2022
@luisgreen
Copy link

I'm not sure I want to add the overhead of compatibility with multiple database options.

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?

@NickM-27
Copy link
Sponsor Collaborator

NickM-27 commented Jan 26, 2023

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.

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'm not reading the full code, but it could be possible the database locking is preventing multiple transactions?

I'd suggest doing that because it seems the way you're assuming it works is incorrect.

@blakeblackshear
Copy link
Owner

@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.

@jangrewe
Copy link

jangrewe commented Dec 6, 2023

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. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants