diff --git a/sql_export_delta/README.rst b/sql_export_delta/README.rst new file mode 100644 index 0000000000..eaaab27c52 --- /dev/null +++ b/sql_export_delta/README.rst @@ -0,0 +1,100 @@ +========================== +SQL Export (delta support) +========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:9494034b2f072b0d68ca3c3736ce6176299e3fede3f85b06ae004461e90f8431 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github + :target: https://github.com/OCA/reporting-engine/tree/18.0/sql_export_delta + :alt: OCA/reporting-engine +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/reporting-engine-18-0/reporting-engine-18-0-sql_export_delta + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/reporting-engine&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the functionality of ``sql_export`` to allow you to +only export changes between export runs. + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure this module, you need to: + +1. Go to an export in draft mode +2. Check the flag 'Delta' + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Hunki Enterprises BV + +Contributors +------------ + +- Holger Brunn + (https://hunki-enterprises.com) +- Therp BV info@therp.nl (https://therp.nl) + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-hbrunn| image:: https://github.com/hbrunn.png?size=40px + :target: https://github.com/hbrunn + :alt: hbrunn + +Current `maintainer `__: + +|maintainer-hbrunn| + +This module is part of the `OCA/reporting-engine `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sql_export_delta/__init__.py b/sql_export_delta/__init__.py new file mode 100644 index 0000000000..5f67c80400 --- /dev/null +++ b/sql_export_delta/__init__.py @@ -0,0 +1,3 @@ +from . import models +from . import wizards +from .hooks import uninstall_hook diff --git a/sql_export_delta/__manifest__.py b/sql_export_delta/__manifest__.py new file mode 100644 index 0000000000..0b5e1fc1e9 --- /dev/null +++ b/sql_export_delta/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +{ + "name": "SQL Export (delta support)", + "summary": "Support exporting only the changes from last export", + "version": "18.0.1.0.0", + "development_status": "Alpha", + "category": "Extra Tools", + "website": "https://github.com/OCA/reporting-engine", + "author": "Hunki Enterprises BV, Odoo Community Association (OCA)", + "maintainers": ["hbrunn"], + "license": "AGPL-3", + "uninstall_hook": "uninstall_hook", + "depends": [ + "sql_export", + ], + "data": [ + "views/sql_export.xml", + ], +} diff --git a/sql_export_delta/hooks.py b/sql_export_delta/hooks.py new file mode 100644 index 0000000000..1a6d76d4ae --- /dev/null +++ b/sql_export_delta/hooks.py @@ -0,0 +1,7 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +def uninstall_hook(env): + for export in env["sql.export"].search([]): + export._export_delta_cleanup(keep_last=False) diff --git a/sql_export_delta/i18n/it.po b/sql_export_delta/i18n/it.po new file mode 100644 index 0000000000..b930fd2e78 --- /dev/null +++ b/sql_export_delta/i18n/it.po @@ -0,0 +1,43 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sql_export_delta +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-11-08 11:06+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.6.2\n" + +#. module: sql_export_delta +#: model:ir.model,name:sql_export_delta.model_sql_file_wizard +msgid "Allow the user to save the file with sql request's data" +msgstr "Consente all'utente di salvare il file con i dati della richiesta SQL" + +#. module: sql_export_delta +#: model:ir.model.fields,field_description:sql_export_delta.field_sql_export__export_delta +msgid "Delta" +msgstr "Differenza" + +#. module: sql_export_delta +#: model:ir.model,name:sql_export_delta.model_sql_export +msgid "SQL export" +msgstr "Esporta SQL" + +#. module: sql_export_delta +#: model:ir.model.fields,help:sql_export_delta.field_sql_export__export_delta +msgid "" +"With this checked, the full result of the query will be stored as table in " +"the database, but the file generated will only contain rows not existing in " +"the n-1st export" +msgstr "" +"Se questa opzione è selezionata, il risultato completo della query verrà " +"memorizzato come tabella nel database, ma il file generato conterrà solo " +"righe non esistenti nell'esportazione n-1" diff --git a/sql_export_delta/i18n/sql_export_delta.pot b/sql_export_delta/i18n/sql_export_delta.pot new file mode 100644 index 0000000000..be8d9a0f99 --- /dev/null +++ b/sql_export_delta/i18n/sql_export_delta.pot @@ -0,0 +1,37 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sql_export_delta +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: sql_export_delta +#: model:ir.model,name:sql_export_delta.model_sql_file_wizard +msgid "Allow the user to save the file with sql request's data" +msgstr "" + +#. module: sql_export_delta +#: model:ir.model.fields,field_description:sql_export_delta.field_sql_export__export_delta +msgid "Delta" +msgstr "" + +#. module: sql_export_delta +#: model:ir.model,name:sql_export_delta.model_sql_export +msgid "SQL export" +msgstr "" + +#. module: sql_export_delta +#: model:ir.model.fields,help:sql_export_delta.field_sql_export__export_delta +msgid "" +"With this checked, the full result of the query will be stored as table in " +"the database, but the file generated will only contain rows not existing in " +"the n-1st export" +msgstr "" diff --git a/sql_export_delta/models/__init__.py b/sql_export_delta/models/__init__.py new file mode 100644 index 0000000000..0144620622 --- /dev/null +++ b/sql_export_delta/models/__init__.py @@ -0,0 +1 @@ +from . import sql_export diff --git a/sql_export_delta/models/sql_export.py b/sql_export_delta/models/sql_export.py new file mode 100644 index 0000000000..e8be891b52 --- /dev/null +++ b/sql_export_delta/models/sql_export.py @@ -0,0 +1,98 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +from psycopg2.sql import SQL, Identifier + +from odoo import fields, models + + +class SqlExport(models.Model): + _inherit = "sql.export" + + export_delta = fields.Boolean( + string="Delta", + help="With this checked, the full result of the query " + "will be stored as table in the database, but the file generated will " + "only contain rows not existing in the n-1st export", + ) + + def write(self, vals): + """Delete previous results when we change the query""" + if "query" in vals: + for this in self: + this._export_delta_cleanup(keep_last=False) + return super().write(vals) + + def _execute_sql_request( + self, + params=None, + mode="fetchall", + rollback=True, + view_name=False, + copy_options="CSV HEADER DELIMITER ';'", + header=False, + ): + delta_id = self.env.context.get("export_delta_id") + + if delta_id: + original_query = self.env.cr.mogrify(self.query, params).decode("utf-8") + result_table = self._export_delta_table_name(delta_id) + table_query = SQL( + "WITH result as ({0}) SELECT * INTO TABLE {1} FROM result" + ).format(SQL(original_query), Identifier(result_table)) + previous_result_table = self._export_delta_existing_tables()[-1:] + if previous_result_table: + result_query = SQL("SELECT * FROM {0} EXCEPT SELECT * FROM {1}").format( + Identifier(result_table), + Identifier(previous_result_table[0]), + ) + else: + result_query = SQL("SELECT * FROM {0}").format(Identifier(result_table)) + self.env.cr.execute(table_query) + # inject new query in cache for super to use + self._cache["query"] = result_query + result = super()._execute_sql_request( + params=None, + mode=mode, + rollback=rollback, + view_name=view_name, + copy_options=copy_options, + header=header, + ) + self.invalidate_recordset(["query"]) + self._export_delta_cleanup(keep_last=True) + else: + result = super()._execute_sql_request( + params=params, + mode=mode, + rollback=rollback, + view_name=view_name, + copy_options=copy_options, + header=header, + ) + + return result + + def _export_delta_table_name(self, identifier): + """ + Return the name of a table to store data for delta export, must end with + {identifier} + """ + return f"sql_export_delta_{self.id}_{identifier}" + + def _export_delta_existing_tables(self): + """Return all table names used for storing data for delta export""" + self.env.cr.execute( + "SELECT table_name FROM information_schema.tables WHERE table_name LIKE %s", + (self._export_delta_table_name("%"),), + ) + return sorted( + [name for (name,) in self.env.cr.fetchall()], + key=lambda name: int(name[len(self._export_delta_table_name("")) :]), + ) + + def _export_delta_cleanup(self, keep_last=True): + """Delete tables storing data for delta export""" + table_names = self._export_delta_existing_tables()[: -1 if keep_last else None] + for table_name in table_names: + self.env.cr.execute(SQL("DROP TABLE {0}").format(Identifier(table_name))) diff --git a/sql_export_delta/pyproject.toml b/sql_export_delta/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/sql_export_delta/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sql_export_delta/readme/CONFIGURE.md b/sql_export_delta/readme/CONFIGURE.md new file mode 100644 index 0000000000..42bbd0aa47 --- /dev/null +++ b/sql_export_delta/readme/CONFIGURE.md @@ -0,0 +1,4 @@ +To configure this module, you need to: + +1. Go to an export in draft mode +2. Check the flag 'Delta' diff --git a/sql_export_delta/readme/CONTRIBUTORS.md b/sql_export_delta/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..b7b6d76d2c --- /dev/null +++ b/sql_export_delta/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Holger Brunn \ (https://hunki-enterprises.com) +- Therp BV (https://therp.nl) diff --git a/sql_export_delta/readme/DESCRIPTION.md b/sql_export_delta/readme/DESCRIPTION.md new file mode 100644 index 0000000000..51811e323a --- /dev/null +++ b/sql_export_delta/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module extends the functionality of ``sql_export`` to allow you to only export changes between export runs. diff --git a/sql_export_delta/static/description/icon.png b/sql_export_delta/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/sql_export_delta/static/description/icon.png differ diff --git a/sql_export_delta/static/description/index.html b/sql_export_delta/static/description/index.html new file mode 100644 index 0000000000..ebb9f6f806 --- /dev/null +++ b/sql_export_delta/static/description/index.html @@ -0,0 +1,443 @@ + + + + + +SQL Export (delta support) + + + +
+

SQL Export (delta support)

+ + +

Alpha License: AGPL-3 OCA/reporting-engine Translate me on Weblate Try me on Runboat

+

This module extends the functionality of sql_export to allow you to +only export changes between export runs.

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Configuration

+

To configure this module, you need to:

+
    +
  1. Go to an export in draft mode
  2. +
  3. Check the flag ‘Delta’
  4. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Hunki Enterprises BV
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

hbrunn

+

This module is part of the OCA/reporting-engine project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/sql_export_delta/tests/__init__.py b/sql_export_delta/tests/__init__.py new file mode 100644 index 0000000000..b9039bdce0 --- /dev/null +++ b/sql_export_delta/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sql_export_delta diff --git a/sql_export_delta/tests/test_sql_export_delta.py b/sql_export_delta/tests/test_sql_export_delta.py new file mode 100644 index 0000000000..c9ee8c3b15 --- /dev/null +++ b/sql_export_delta/tests/test_sql_export_delta.py @@ -0,0 +1,26 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +from odoo.tests.common import tagged + +from odoo.addons.sql_export.tests import test_sql_query + +from ..hooks import uninstall_hook + + +@tagged("post_install", "-at_install") +class TestSqlExportDelta(test_sql_query.TestExportSqlQuery): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.sql_report_demo.export_delta = True + + def test_sql_query(self): + """Test that exporting demo report twice exports empty delta""" + result = super().test_sql_query() + with self.assertRaises(AssertionError): + super().test_sql_query() + self.assertTrue(self.sql_report_demo._export_delta_existing_tables()) + uninstall_hook(self.env) + self.assertFalse(self.sql_report_demo._export_delta_existing_tables()) + return result diff --git a/sql_export_delta/views/sql_export.xml b/sql_export_delta/views/sql_export.xml new file mode 100644 index 0000000000..6b3a662328 --- /dev/null +++ b/sql_export_delta/views/sql_export.xml @@ -0,0 +1,14 @@ + + + + + sql.export + + + + + + + + diff --git a/sql_export_delta/wizards/__init__.py b/sql_export_delta/wizards/__init__.py new file mode 100644 index 0000000000..f9ef62c395 --- /dev/null +++ b/sql_export_delta/wizards/__init__.py @@ -0,0 +1 @@ +from . import sql_file_wizard diff --git a/sql_export_delta/wizards/sql_file_wizard.py b/sql_export_delta/wizards/sql_file_wizard.py new file mode 100644 index 0000000000..a0e4f9f0eb --- /dev/null +++ b/sql_export_delta/wizards/sql_file_wizard.py @@ -0,0 +1,14 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +from odoo import models + + +class SqlFileWizard(models.TransientModel): + _inherit = "sql.file.wizard" + + def export_sql(self): + self.ensure_one() + if self.sql_export_id.export_delta: + self = self.with_context(export_delta_id=self.id) + return super().export_sql()