0002-general-update-convert_to_sql-for-postgresql-enabled.patch
tests/test_convert_to_sql.py | ||
---|---|---|
18 | 18 | |
19 | 19 |
@pytest.fixture |
20 | 20 |
def formdeffix(): |
21 |
FormDef.wipe() |
|
21 |
for formdef in FormDef.select(): |
|
22 |
formdef.remove_self() |
|
22 | 23 |
formdef = FormDef() |
24 |
formdef.id = 123 |
|
23 | 25 |
formdef.name = 'testform' |
24 | 26 |
formdef.description = 'plop' |
25 | 27 |
formdef.fields = [BoolField(id='1')] |
26 | 28 |
formdef.store() |
27 | 29 | |
28 |
data_class = formdef.data_class() |
|
30 |
data_class = formdef.data_class(mode='files')
|
|
29 | 31 |
for value in (True, True, True, False): |
30 | 32 |
formdata = data_class() |
31 | 33 |
formdata.data = {'1': value} |
... | ... | |
36 | 38 | |
37 | 39 |
@pytest.fixture |
38 | 40 |
def carddeffix(): |
39 |
CardDef.wipe() |
|
41 |
for carddef in CardDef.select(): |
|
42 |
carddef.remove_self() |
|
40 | 43 |
carddef = CardDef() |
44 |
carddef.id = 456 |
|
41 | 45 |
carddef.name = 'testcard' |
42 | 46 |
carddef.description = 'plop' |
43 | 47 |
carddef.fields = [BoolField(id='1')] |
44 | 48 |
carddef.store() |
45 | 49 | |
46 |
data_class = carddef.data_class() |
|
50 |
data_class = carddef.data_class(mode='files')
|
|
47 | 51 |
for value in (True, True, True, False): |
48 | 52 |
formdata = data_class() |
49 | 53 |
formdata.data = {'1': value} |
... | ... | |
144 | 148 | |
145 | 149 | |
146 | 150 |
def test_data_is_migrated(pub, database, local_user, formdeffix, carddeffix): |
147 |
pub.load_site_options() |
|
148 |
assert not pub.site_options.has_option('options', 'postgresql') |
|
149 | 151 |
call_command('convert_to_sql', '-d', 'example.net', '--database', database) |
150 |
pub.load_site_options() |
|
151 |
assert pub.site_options.has_option('options', 'postgresql') |
|
152 |
pub.is_using_postgresql = lambda: True |
|
153 | 152 |
pub.set_config() |
154 | 153 |
formdefs = FormDef.select() |
155 | 154 |
assert len(formdefs) == 1 |
... | ... | |
177 | 176 |
role = Role(name='Test Role') |
178 | 177 |
role.store() |
179 | 178 | |
180 |
pub.load_site_options() |
|
181 |
assert not pub.site_options.has_option('options', 'postgresql') |
|
182 | 179 |
call_command('convert_to_sql', '-d', 'example.net', '--database', database) |
183 |
pub.load_site_options() |
|
184 |
assert pub.site_options.has_option('options', 'postgresql') |
|
185 |
pub.is_using_postgresql = lambda: True |
|
186 |
pub.set_config() |
|
187 | 180 |
assert len(pub.user_class.get_users_with_name_identifier('0123456789')) == 1 |
188 | 181 |
assert pub.role_class.count() == 1 |
wcs/ctl/management/commands/convert_to_sql.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU General Public License |
15 | 15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
import io |
|
18 |
import os |
|
19 | 17 |
import sys |
20 | 18 |
import traceback |
21 | 19 | |
22 | 20 |
import psycopg2 |
23 | 21 |
from django.core.management.base import BaseCommand, CommandError |
24 |
from django.utils.encoding import force_bytes |
|
25 | 22 | |
26 | 23 |
from wcs import sql |
27 | 24 |
from wcs.carddef import CardDef |
28 | 25 |
from wcs.formdef import FormDef |
29 | 26 |
from wcs.qommon.misc import localstrftime |
30 | 27 |
from wcs.qommon.publisher import UnknownTenantError, get_publisher_class |
31 |
from wcs.qommon.storage import atomic_write |
|
32 | 28 |
from wcs.roles import Role |
33 | 29 |
from wcs.users import User |
34 | 30 | |
... | ... | |
47 | 43 | |
48 | 44 |
def handle(self, **options): |
49 | 45 |
self.publisher = self.get_publisher(options['domain']) |
50 |
if self.publisher.is_using_postgresql():
|
|
46 |
if self.publisher.has_postgresql_config():
|
|
51 | 47 |
raise CommandError('tenant already using postgresql') |
52 | 48 | |
53 | 49 |
self.setup_connection(**options) |
54 | 50 |
sql.get_connection(new=True) |
51 | ||
52 |
# create early tables |
|
53 |
sql.do_snapshots_table() |
|
54 |
sql.do_custom_views_table() |
|
55 | ||
55 | 56 |
self.store_roles() |
56 | 57 |
self.store_users() |
57 | 58 |
self.store_forms() |
58 | 59 |
self.publisher.write_cfg() |
59 |
self.enable_connection() |
|
60 | 60 |
self.publisher.initialize_sql() |
61 | 61 |
self.publisher.cleanup() |
62 | 62 | |
... | ... | |
76 | 76 |
options[k] = kwargs.get(k) |
77 | 77 |
self.publisher.cfg['postgresql'] = options |
78 | 78 | |
79 |
def enable_connection(self): |
|
80 |
if not self.publisher.site_options.has_option('options', 'postgresql'): |
|
81 |
if not self.publisher.site_options.has_section('options'): |
|
82 |
self.publisher.site_options.add_section('options') |
|
83 |
self.publisher.site_options.set('options', 'postgresql', 'true') |
|
84 |
options_file = os.path.join(self.publisher.app_dir, 'site-options.cfg') |
|
85 |
stringio = io.StringIO() |
|
86 |
self.publisher.site_options.write(stringio) |
|
87 |
atomic_write(options_file, force_bytes(stringio.getvalue())) |
|
88 | ||
89 | 79 |
def store_users(self): |
90 | 80 |
self.convert_objects('user', User, sql.SqlUser, skip_global_forms_table_update=True) |
91 | 81 |
sql.SqlUser.fix_sequences() |
... | ... | |
118 | 108 | |
119 | 109 |
def store_forms(self): |
120 | 110 |
errors = [] |
121 |
for formdef in FormDef.select() + CardDef.select():
|
|
111 |
for formdef in FormDef.select(ignore_migration=True) + CardDef.select(ignore_migration=True):
|
|
122 | 112 |
print('converting %s' % formdef.name) |
123 | 113 |
sql.do_formdef_tables(formdef, rebuild_views=True, rebuild_global_views=True) |
124 | 114 |
data_class = formdef.data_class(mode='files') |
... | ... | |
160 | 150 |
self.update_progress(100 * i / count) |
161 | 151 |
sql_data_class.fix_sequences() |
162 | 152 | |
153 |
if formdef.migrate(): |
|
154 |
# run formdef migration as we're done and table name can be stored etc. |
|
155 |
formdef.store() |
|
156 | ||
163 | 157 |
if errors: |
164 | 158 |
with open('error_formdata.log', 'w') as error_log: |
165 | 159 |
for formdata, trace in errors: |
wcs/formdef.py | ||
---|---|---|
482 | 482 |
instance=self, comment=comment, store_user=snapshot_store_user |
483 | 483 |
) |
484 | 484 | |
485 |
self.update_storage() |
|
486 |
self.store_related_custom_views() |
|
485 |
if get_publisher().has_postgresql_config(): |
|
486 |
self.update_storage() |
|
487 |
self.store_related_custom_views() |
|
487 | 488 | |
488 | 489 |
def update_storage(self): |
489 | 490 |
if not get_publisher().is_using_postgresql(): |
wcs/publisher.py | ||
---|---|---|
154 | 154 | |
155 | 155 |
wcs.workflows.load_extra() |
156 | 156 | |
157 |
if self.is_using_postgresql() and not skip_sql:
|
|
157 |
if self.has_postgresql_config() and not skip_sql:
|
|
158 | 158 |
from . import sql |
159 | 159 | |
160 | 160 |
self.user_class = sql.SqlUser |
161 |
- |