Repository: frappe/frappe

Issue:
Do not call begin in updatedb after syncing
<!--
Welcome to the Frappe Framework issue tracker! Before creating an issue, please heed the following:

1. This tracker should only be used to report bugs and request features / enhancements to Frappe
    - For questions and general support, use https://stackoverflow.com/questions/tagged/frappe
    - For documentation issues, refer to https://frappeframework.com/docs/user/en or the developer cheetsheet https://github.com/frappe/frappe/wiki/Developer-Cheatsheet
2. Use the search function before creating a new issue. Duplicates will be closed and directed to
   the original discussion.
3. When making a bug report, make sure you provide all required information. The easier it is for
   maintainers to reproduce, the faster it'll be fixed.
4. If you think you know what the reason for the bug is, share it with us. Maybe put in a PR 😉
-->

## Description of the issue
Database.commit calls begin automatically like the following. Therefore it is not necessary to call begin after syncing table metadata.  

``` python
    def commit(self):
        """Commit current transaction. Calls SQL `COMMIT`."""
        self.before_rollback.reset()
        self.after_rollback.reset()

        self.before_commit.run()

        self.sql("commit")
        self.begin()  # explicitly start a new transaction

        self.after_commit.run()
```

```python
    def updatedb(self, doctype, meta=None):
        """
        Syncs a `DocType` to the table
        * creates if required
        * updates columns
        * updates indices
        """
        res = self.sql(f"select issingle from `tabDocType` where name='{doctype}'")
        if not res:
            raise Exception(f"Wrong doctype {doctype} in updatedb")

        if not res[0][0]:
            db_table = PostgresTable(doctype, meta)
            db_table.validate()

            self.commit()
            db_table.sync()
            self.commit()
```

## Context information (for bug reports)

**Output of `bench version`**
```
frappe 16.0.0-dev
library_management 0.0.1
```

## Steps to reproduce the issue
1. From the implementation of `Database.commit` and `Database.rollback`, there is no need to call `begin` explicitly in any cases.

### Observed result
`Database.begin` is called when it was already called earlier in `Database.commit` which effectively starts a nested transaction which is not expected and may not be supported by certain implementation of databases talking MySQL and PostgreSQL protocol.

### Expected result
Call `Database.commit` instead to commit the previously executed ddl statements.

### Stacktrace / full error message
NA

## Additional information

OS version / distribution, `Frappe` install method, etc.

```shell
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy
```


Discussion:

Resolving patch:
diff --git a/frappe/database/mariadb/database.py b/frappe/database/mariadb/database.py
--- a/frappe/database/mariadb/database.py
+++ b/frappe/database/mariadb/database.py
@@ -440,7 +440,7 @@ def updatedb(self, doctype, meta=None):
 
 			self.commit()
 			db_table.sync()
-			self.begin()
+			self.commit()
 
 	def get_database_list(self):
 		return self.sql("SHOW DATABASES", pluck=True)
diff --git a/frappe/database/postgres/database.py b/frappe/database/postgres/database.py
--- a/frappe/database/postgres/database.py
+++ b/frappe/database/postgres/database.py
@@ -329,7 +329,7 @@ def updatedb(self, doctype, meta=None):
 
 			self.commit()
 			db_table.sync()
-			self.begin()
+			self.commit()
 
 	@staticmethod
 	def get_on_duplicate_update(key="name"):


Test patch: