Projet

Général

Profil

FAIL_0003-link-old-convertsql-command-to-current-implementatio.patch

Christophe Siraut, 27 juin 2018 09:25

Télécharger (6,52 ko)

Voir les différences:

Subject: [PATCH 3/3] link old convertsql command to current implementation

 tests/test_convert_to_sql.py |  24 +++++++--
 wcs/ctl/convertsql.py        | 118 ++++---------------------------------------
 2 files changed, 28 insertions(+), 114 deletions(-)
tests/test_convert_to_sql.py
19 19
from test_api import local_user
20 20

  
21 21

  
22
class Struct:
23
    def __init__(self, **entries):
24
        self.__dict__.update(entries)
25

  
26

  
27 22
@pytest.fixture
28 23
def formdeffix():
29 24
    FormDef.wipe()
......
125 120
    assert len(formdefs) == 1
126 121
    data_class = formdefs[0].data_class(mode='sql')
127 122
    assert len(data_class.keys()) == 4
123

  
124

  
125
def test_compat_previous_ctl_command(pub, database, local_user, formdeffix):
126
    pub.load_site_options()
127
    assert not pub.site_options.has_option('options', 'postgresql')
128
    cmd = CmdConvertToSql()
129
    cmd.config.add_section('main')
130
    cmd.config.set('main', 'error_log', '')
131
    cmd.config.set('main', 'app_dir', os.path.dirname(pub.app_dir))
132
    sub_options_class = collections.namedtuple('Options', ['data_dir', 'dbname', 'user'])
133
    sub_options = sub_options_class(pub.app_dir, database, os.environ.get('USER'))
134
    cmd.execute(object, sub_options, ('example.net',))
135
    pub.load_site_options()
136
    assert pub.site_options.has_option('options', 'postgresql')
137
    assert len(pub.user_class.get_users_with_name_identifier('0123456789')) == 1
138
    formdefs = FormDef.select()
139
    assert len(formdefs) == 1
140
    data_class = formdefs[0].data_class(mode='sql')
141
    assert len(data_class.keys()) == 4
wcs/ctl/convertsql.py
15 15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import os
18
import sys
19
import traceback
20

  
21

  
22
num_columns = 0
23
try:
24
    import curses
25
except ImportError:
26
    curses = None
27
else:
28
    try:
29
        curses.setupterm()
30
        num_columns = curses.tigetnum('cols')
31
    except:
32
        pass
33

  
34

  
35 18
from qommon.ctl import Command, make_option
19
from django.core.management import call_command
36 20

  
37
def update_progress(progress):
38
    if not num_columns:
39
        return
40
    sys.stdout.write('[%s] %s%%\r' % (
41
        ('#'*((num_columns-10)*progress/100)).ljust(num_columns-15), progress))
42 21

  
43 22
class CmdConvertToSql(Command):
44 23
    name = 'convert-to-sql'
......
69 48
        pub.app_dir = os.path.join(pub.app_dir, hostname)
70 49
        pub.set_config()
71 50

  
72
        from wcs.formdef import FormDef
73
        from wcs import sql
74

  
75
        if sub_options.port:
76
            sub_options.port = int(sub_options.port)
77

  
78
        pub.cfg['postgresql'] = {
79
                'database': sub_options.dbname,
80
                'user': sub_options.user,
81
                'password': sub_options.password,
82
                'host': sub_options.host,
83
                'port': sub_options.port,
84
        }
85

  
86
        sql.get_connection_and_cursor(new=True)
87

  
88
        errors = []
89

  
90
        print 'converting users'
91
        from users import User
92
        sql.do_user_table()
93
        count = User.count()
94
        for i, user_id in enumerate(User.keys()):
95
            user = User.get(user_id)
96
            user.__class__ = sql.SqlUser
97
            try:
98
                user.store()
99
            except AssertionError:
100
                errors.append((user, traceback.format_exc()))
101
            update_progress(100*i/count)
102
        sql.SqlUser.fix_sequences()
103

  
104
        if errors:
105
            error_log = file('error_user.log', 'w')
106
            for user, trace in errors:
107
                print >> error_log, 'user_id', user.id
108
                print >> error_log, trace
109
                print >> error_log, '-'*80
110
            error_log.close()
111
            print 'There were some errors, see error_user.log for details.'
112

  
113
        errors = []
114
        for formdef in FormDef.select():
115
            print ('converting %s' % formdef.name).ljust(num_columns-1)
116
            sql.do_formdef_tables(formdef, rebuild_views=True,
117
                    rebuild_global_views=True)
118
            data_class = formdef.data_class(mode='files')
119
            count = data_class.count()
120

  
121
            # load all objects a first time, to allow the migrate() code to be
122
            # run and the eventual changes properly saved.
123
            for id in data_class.keys():
124
                formdata = data_class.get(id)
125
            delattr(sys.modules['formdef'], formdef.url_name.title())
126

  
127
            # once this is done, reload and store everything in postgresql
128
            sql_data_class = formdef.data_class(mode='sql')
129
            for i, id in enumerate(data_class.keys()):
130
                formdata = data_class.get(id)
131
                formdata._formdef = formdef
132
                formdata._evolution = formdata.evolution
133
                formdata.__class__ = sql_data_class
134
                try:
135
                    formdata.store()
136
                except AssertionError:
137
                    errors.append((formdata, traceback.format_exc()))
138
                update_progress(100*i/count)
139
            sql_data_class.fix_sequences()
140

  
141
        print 'done'.ljust(num_columns-1)
142

  
143
        sql.do_tracking_code_table()
144
        sql.do_session_table()
145
        sql.do_meta_table()
146

  
147
        if errors:
148
            error_log = file('error_formdata.log', 'w')
149
            for formdata, trace in errors:
150
                print >> error_log, formdata.formdef, formdata.id
151
                print >> error_log, trace
152
                print >> error_log, '-'*80
153
            error_log.close()
154
            print 'There were some errors, see error_formdata.log for details.'
155

  
156
        if not pub.has_site_option('postgresql'):
157
            print 'You still have to edit your site-options.cfg'
51
        cmd = ['convert_to_sql',
52
               '-d', hostname,
53
               '--database', sub_options.dbname]
158 54

  
159
        pub.write_cfg()
55
        for k in ['user', 'password', 'host', 'port']:
56
            value = getattr(sub_options, k, False)
57
            if value:
58
                cmd.extend(['--%s' % k, value])
160 59

  
60
        call_command(*cmd)
161 61

  
162 62
CmdConvertToSql.register()
163
-