# Path: neubot/database/_table_utils.py
# SIMPLE_TYPES = {
#     types.StringType  : "TEXT",
#     types.UnicodeType : "TEXT",
#     types.IntType     : "INTEGER",
#     types.FloatType   : "REAL",
# }
# def __check(value):
# def make_create_table(table, template):
# def make_insert_into(table, template):
# def do_insert_into(connection, query, dictobj, template,
#       commit=True, override_timestamp=True):
# def make_select(table, template, **kwargs):
# def rename_column_query(table1, template1, table2, template2):
# def rename_column_ntemplate(template, mapping, broken=False):
# def rename_column(connection, table, template, mapping, broken=False):


import logging
import sqlite3
import sys
import uuid
from neubot.marshal import unmarshal_object
from neubot.database import _table_utils
#
def migrate_from__v4_1__to__v4_2(connection):
    """Migrate database from version 4.1 to version 4.2"""

    cursor = connection.cursor()
    cursor.execute("SELECT value FROM config WHERE name='version';")
    ver = cursor.fetchone()[0]
    if ver == "4.1":
        logging.info("* Migrating database from version 4.1 to 4.2")
        cursor.execute("""UPDATE config SET value='4.2'
                        WHERE name='version';""")

        # Config
        cursor.execute("""UPDATE config SET name='privacy.can_publish'
                        WHERE name='privacy.can_share';""")

        logging.info("* Renaming columns (this may take a long time)")
        mapping = { "privacy_can_share": "privacy_can_publish" }

        # BitTorrent
        template_bt = { "timestamp": 0, "uuid": "", "internal_address": "",
                        "real_address": "", "remote_address": "",
                        "privacy_informed": 0, "privacy_can_collect": 0,
                        "privacy_can_share": 0, "connect_time": 0.0,
                        "download_speed": 0.0, "upload_speed": 0.0,
                        "neubot_version": "", "platform": "", }
        #
        # Here we need to break the columns order so that we can
        # fix it up again when migrating from 4.2 to 4.3
        #

# Path: neubot/database/_table_utils.py
# SIMPLE_TYPES = {
#     types.StringType  : "TEXT",
#     types.UnicodeType : "TEXT",
#     types.IntType     : "INTEGER",
#     types.FloatType   : "REAL",
# }
# def __check(value):
# def make_create_table(table, template):
# def make_insert_into(table, template):
# def do_insert_into(connection, query, dictobj, template,
#       commit=True, override_timestamp=True):
# def make_select(table, template, **kwargs):
# def rename_column_query(table1, template1, table2, template2):
# def rename_column_ntemplate(template, mapping, broken=False):
# def rename_column(connection, table, template, mapping, broken=False):


# Path: neubot/database/migrate.py
import logging
import sqlite3
import sys
import uuid
from neubot.marshal import unmarshal_object
from neubot.database import _table_utils

#
def migrate_from__v4_1__to__v4_2(connection):
    """Migrate database from version 4.1 to version 4.2"""

    cursor = connection.cursor()
    cursor.execute("SELECT value FROM config WHERE name='version';")
    ver = cursor.fetchone()[0]
    if ver == "4.1":
        logging.info("* Migrating database from version 4.1 to 4.2")
        cursor.execute("""UPDATE config SET value='4.2'
                        WHERE name='version';""")

        # Config
        cursor.execute("""UPDATE config SET name='privacy.can_publish'
                        WHERE name='privacy.can_share';""")

        logging.info("* Renaming columns (this may take a long time)")
        mapping = { "privacy_can_share": "privacy_can_publish" }

        # BitTorrent
        template_bt = { "timestamp": 0, "uuid": "", "internal_address": "",
                        "real_address": "", "remote_address": "",
                        "privacy_informed": 0, "privacy_can_collect": 0,
                        "privacy_can_share": 0, "connect_time": 0.0,
                        "download_speed": 0.0, "upload_speed": 0.0,
                        "neubot_version": "", "platform": "", }
        #
        # Here we need to break the columns order so that we can
        # fix it up again when migrating from 4.2 to 4.3
        #

        _table_utils.rename_column(connection, "bittorrent",