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

db.vacuum: Add module to manage SQL vaccum #2462

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scripts/db.vacuum/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MODULE_TOPDIR = ../..

PGM = db.vacuum

include $(MODULE_TOPDIR)/include/Make/Script.make

default: script
42 changes: 42 additions & 0 deletions scripts/db.vacuum/db.vacuum.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<h2>DESCRIPTION</h2>

<em>db.vacuum</em> runs SQL VACUUM or sets up automatic vacuum procedures.

<h2>NOTES</h2>

<h2>EXAMPLES</h2>

<div class="code"><pre>
db.vacuum operation=vacuum
db.vacuum operation=enable_auto_vacuum
db.vacuum operation=disable_auto_vacuum
db.vacuum operation=incremental_vacuum
</pre></div>

<div class="code"><pre>
db.vacuum driver=sqlite database=/opt/sqlite.db
</pre></div>

<h2>SEE ALSO</h2>

<em>
<a href="db.dropdb.html">db.dropdb</a>,
<a href="db.dropcolumn.html">db.dropcolumn</a>,
<a href="db.droptable.html">db.droptable</a>,
<a href="v.db.droprow.html">v.db.droprow</a>,
<a href="v.db.dropcolumn.html">v.db.dropcolumn</a>,
<a href="v.db.droptable.html">v.db.droptable</a>,
<a href="db.login.html">db.login</a>,
<a href="db.connect.html">db.connect</a>,
<a href="db.tables.html">db.tables</a>,
<a href="db.describe.html">db.describe</a>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<a href="db.describe.html">db.describe</a>
<a href="db.describe.html">db.describe</a>,

<a href="db.execute.html">db.execute</a>
</em>

<p>
<a href="sql.html">GRASS SQL interface</a>

<h2>AUTHORS</h2>

Markus Neteler<br>
Driver and database options added by Martin Landa, Czech Technical University in Prague, Czech Republic
Comment on lines +41 to +42
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Markus Neteler<br>
Driver and database options added by Martin Landa, Czech Technical University in Prague, Czech Republic
Vaclav Petras, <a href="https://geospatial.ncsu.edu/geoforall/">NCSU GeoForAll Lab</a>

(affiliation taken from other HTML file under scripts/)

86 changes: 86 additions & 0 deletions scripts/db.vacuum/db.vacuum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python3

############################################################################
#
# MODULE: db.vacuum
# AUTHOR(S): Vaclav Petras
# PURPOSE: Interface to db.execute to drop an attribute table
# COPYRIGHT: (C) 2022 by Vaclav Petras and the GRASS Development Team
#
# This program is free software under the GNU General
# Public License (>=v2). Read the file COPYING that
# comes with GRASS for details.
#
#############################################################################

# %module
# % description: Vacuums (cleans) database from dropped (deleted) data
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# % description: Vacuums (cleans) database from dropped (deleted) data
# % description: Vacuums (cleans) database from dropped (deleted) data.

# % keyword: database
# % keyword: attribute table
# %end

# %option
# % key: operation
# % type: string
# % required: yes
# % multiple: no
# % options: vacuum,enable_auto_vacuum,disable_auto_vacuum,enable_incremental_vacuum,do_incremental_vacuum
# % description: Operation to be performed
# %end

# %option G_OPT_DB_DRIVER
# % label: Name of database driver
# % description: If not given then default driver is used
# % guisection: Connection
# %end

# %option G_OPT_DB_DATABASE
# % label: Name of database
# % description: If not given then default database is used
# % guisection: Connection
# %end

import sys

import grass.script as gs


def database_vacuum(operation, database, driver):
if operation == "vacuum":
sql = "VACUUM;"
elif operation == "enable_auto_vacuum" and driver == "sqlite":
sql = "PRAGMA auto_vacuum = 'full'; VACUUM;"
elif operation == "disable_auto_vacuum" and driver == "sqlite":
sql = "PRAGMA auto_vacuum = 'none'; VACUUM;"
elif operation == "enable_incremental_vacuum" and driver == "sqlite":
sql = "PRAGMA auto_vacuum = 'incremental'; VACUUM;"
elif operation == "do_incremental_vacuum" and driver == "sqlite":
sql = "PRAGMA incremental_vacuum;"
else:
gs.fatal(_("Unsupported operation <{operation} for database driver <{driver}>").format(operation=operation, driver=driver))
gs.run_command("db.execute", input=f"{sql}", database=database, driver=driver, error="fatal")


def main():
options, unused_flags = gs.parser()
table = options["table"]

if not options["driver"] or not options["database"]:
# check if DB parameters are set, and if not set them.
gs.run_command("db.connect", flags="c", quiet=True)

kv = gs.db_connection()
if options["database"]:
database = options["database"]
else:
database = kv["database"]
if options["driver"]:
driver = options["driver"]
else:
driver = kv["driver"]
# schema needed for PG?

database_vacuum(operation=options["operation"], database, driver)

Check failure on line 83 in scripts/db.vacuum/db.vacuum.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff

scripts/db.vacuum/db.vacuum.py:83:53: SyntaxError: Positional argument cannot follow keyword argument

Check failure on line 83 in scripts/db.vacuum/db.vacuum.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff

scripts/db.vacuum/db.vacuum.py:83:63: SyntaxError: Positional argument cannot follow keyword argument

if __name__ == "__main__":

Check failure on line 85 in scripts/db.vacuum/db.vacuum.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E305)

scripts/db.vacuum/db.vacuum.py:85:1: E305 Expected 2 blank lines after class or function definition, found (1)
main()
1 change: 1 addition & 0 deletions scripts/v.db.dropcolumn/v.db.dropcolumn.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h2>EXAMPLES</h2>
<h2>SEE ALSO</h2>

<em>
<a href="db.vacuum.html">db.vacuum</a>,
<a href="db.connect.html">db.connect</a>,
<a href="db.dropcolumn.html">db.dropcolumn</a>,
<a href="db.execute.html">db.execute</a>,
Expand Down
1 change: 1 addition & 0 deletions scripts/v.db.droprow/v.db.droprow.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ <h2>EXAMPLES</h2>
<h2>SEE ALSO</h2>

<em>
<a href="db.vacuum.html">db.vacuum</a>,
Copy link
Member

Choose a reason for hiding this comment

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

Also add this to

  • db.dropcolumn.html
  • db.dropdb.html
  • db.droptable.html
    ?

<a href="db.droptable.html">db.droptable</a>,
<a href="db.execute.html">db.execute</a>,
<a href="v.db.addcolumn.html">v.db.addcolumn</a>,
Expand Down
Loading