227 init_config(config) |
227 init_config(config) |
228 schema = repo.schema |
228 schema = repo.schema |
229 sourcescfg = config.read_sources_file() |
229 sourcescfg = config.read_sources_file() |
230 source = sourcescfg['system'] |
230 source = sourcescfg['system'] |
231 driver = source['db-driver'] |
231 driver = source['db-driver'] |
232 sqlcnx = repo.system_source.get_connection() |
232 with repo.internal_cnx() as cnx: |
233 sqlcursor = sqlcnx.cursor() |
233 sqlcnx = cnx.cnxset.cnx |
234 execute = sqlcursor.execute |
234 sqlcursor = cnx.cnxset.cu |
235 if drop: |
235 execute = sqlcursor.execute |
236 helper = database.get_db_helper(driver) |
236 if drop: |
237 dropsql = sql_drop_all_user_tables(helper, sqlcursor) |
237 helper = database.get_db_helper(driver) |
238 # We may fail dropping some tables because of table dependencies, in a first pass. |
238 dropsql = sql_drop_all_user_tables(helper, sqlcursor) |
239 # So, we try a second drop sequence to drop remaining tables if needed. |
239 # We may fail dropping some tables because of table dependencies, in a first pass. |
240 # Note that 2 passes is an arbitrary choice as it seems enougth for our usecases. |
240 # So, we try a second drop sequence to drop remaining tables if needed. |
241 # (looping may induce infinite recursion when user have no right for example) |
241 # Note that 2 passes is an arbitrary choice as it seems enough for our usecases |
242 # Here we try to keep code simple and backend independant. That why we don't try to |
242 # (looping may induce infinite recursion when user have no rights for example). |
243 # distinguish remaining tables (wrong right, dependencies, ...). |
243 # Here we try to keep code simple and backend independent. That's why we don't try to |
244 failed = sqlexec(dropsql, execute, cnx=sqlcnx, |
244 # distinguish remaining tables (missing privileges, dependencies, ...). |
245 pbtitle='-> dropping tables (first pass)') |
245 failed = sqlexec(dropsql, execute, cnx=sqlcnx, |
|
246 pbtitle='-> dropping tables (first pass)') |
|
247 if failed: |
|
248 failed = sqlexec(failed, execute, cnx=sqlcnx, |
|
249 pbtitle='-> dropping tables (second pass)') |
|
250 remainings = list(filter(drop_filter, helper.list_tables(sqlcursor))) |
|
251 assert not remainings, 'Remaining tables: %s' % ', '.join(remainings) |
|
252 handler = config.migration_handler(schema, interactive=False, repo=repo, cnx=cnx) |
|
253 # install additional driver specific sql files |
|
254 handler.cmd_install_custom_sql_scripts() |
|
255 for cube in reversed(config.cubes()): |
|
256 handler.cmd_install_custom_sql_scripts(cube) |
|
257 _title = '-> creating tables ' |
|
258 print(_title, end=' ') |
|
259 # schema entities and relations tables |
|
260 # can't skip entities table even if system source doesn't support them, |
|
261 # they are used sometimes by generated sql. Keeping them empty is much |
|
262 # simpler than fixing this... |
|
263 schemasql = sqlschema(schema, driver) |
|
264 #skip_entities=[str(e) for e in schema.entities() |
|
265 # if not repo.system_source.support_entity(str(e))]) |
|
266 failed = sqlexec(schemasql, execute, pbtitle=_title, delimiter=';;') |
246 if failed: |
267 if failed: |
247 failed = sqlexec(failed, execute, cnx=sqlcnx, |
268 print('The following SQL statements failed. You should check your schema.') |
248 pbtitle='-> dropping tables (second pass)') |
269 print(failed) |
249 remainings = list(filter(drop_filter, helper.list_tables(sqlcursor))) |
270 raise Exception('execution of the sql schema failed, you should check your schema') |
250 assert not remainings, 'Remaining tables: %s' % ', '.join(remainings) |
271 sqlcursor.close() |
251 _title = '-> creating tables ' |
272 sqlcnx.commit() |
252 print(_title, end=' ') |
|
253 # schema entities and relations tables |
|
254 # can't skip entities table even if system source doesn't support them, |
|
255 # they are used sometimes by generated sql. Keeping them empty is much |
|
256 # simpler than fixing this... |
|
257 schemasql = sqlschema(schema, driver) |
|
258 #skip_entities=[str(e) for e in schema.entities() |
|
259 # if not repo.system_source.support_entity(str(e))]) |
|
260 failed = sqlexec(schemasql, execute, pbtitle=_title, delimiter=';;') |
|
261 if failed: |
|
262 print('The following SQL statements failed. You should check your schema.') |
|
263 print(failed) |
|
264 raise Exception('execution of the sql schema failed, you should check your schema') |
|
265 sqlcursor.close() |
|
266 sqlcnx.commit() |
|
267 sqlcnx.close() |
|
268 with repo.internal_cnx() as cnx: |
273 with repo.internal_cnx() as cnx: |
269 # insert entity representing the system source |
274 # insert entity representing the system source |
270 ssource = cnx.create_entity('CWSource', type=u'native', name=u'system') |
275 ssource = cnx.create_entity('CWSource', type=u'native', name=u'system') |
271 repo.system_source.eid = ssource.eid |
276 repo.system_source.eid = ssource.eid |
272 cnx.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid}) |
277 cnx.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid}) |
287 admin = create_user(cnx, login, pwd, u'managers') |
292 admin = create_user(cnx, login, pwd, u'managers') |
288 cnx.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s', |
293 cnx.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s', |
289 {'u': admin.eid}) |
294 {'u': admin.eid}) |
290 cnx.commit() |
295 cnx.commit() |
291 repo.shutdown() |
296 repo.shutdown() |
292 # reloging using the admin user |
297 # re-login using the admin user |
293 config._cubes = None # avoid assertion error |
298 config._cubes = None # avoid assertion error |
294 repo = get_repository(config=config) |
299 repo = get_repository(config=config) |
295 with connect(repo, login, password=pwd) as cnx: |
300 with connect(repo, login, password=pwd) as cnx: |
296 with cnx.security_enabled(False, False): |
301 with cnx.security_enabled(False, False): |
297 repo.system_source.eid = ssource.eid # redo this manually |
302 repo.system_source.eid = ssource.eid # redo this manually |
298 handler = config.migration_handler(schema, interactive=False, |
303 handler = config.migration_handler(schema, interactive=False, |
299 cnx=cnx, repo=repo) |
304 cnx=cnx, repo=repo) |
300 # install additional driver specific sql files |
|
301 handler.cmd_install_custom_sql_scripts() |
|
302 for cube in reversed(config.cubes()): |
|
303 handler.cmd_install_custom_sql_scripts(cube) |
|
304 # serialize the schema |
305 # serialize the schema |
305 initialize_schema(config, schema, handler) |
306 initialize_schema(config, schema, handler) |
306 # yoo ! |
307 # yoo ! |
307 cnx.commit() |
308 cnx.commit() |
308 repo.system_source.init_creating() |
309 repo.system_source.init_creating() |