def init_db(): """ Create board, app, mark tables in the database. Prepopulate category table with default data. """ if not model.package_table.exists(): # during tests? return session = Session() for table in [board_table, app_table, mark_table]: if not table.exists(): table.create(checkfirst=True) log.debug("Apps {} have been created".format(table.name)) for board_name, board_desc in DEFAULT_BOARDS.iteritems(): if not Board.get_by_slug(slugify(board_name)): board = Board() board.name = board_name board.slug = slugify(board_name) board.description = board_desc session.add(board) log.debug("Add {0} to {1} table".format(board_name, board_table.name)) session.commit() if not migration_table.exists(): migration_table.create(checkfirst=True) session.commit() migration_number = session.query(migration_table).count() log.debug('Migration number: %s', migration_number) migration_sql_list = [] for counter, sql in enumerate(migration_sql_list, start=1): if migration_number < counter: try: session.execute(sql) except ProgrammingError: session.rollback() finally: session.execute(migration_table.insert()) session.commit() session.close()
def init_db(): """ Create boards, threads and posts tables in the database. Prepopulate board table with default data. """ if not model.package_table.exists(): # during tests? return session = Session() if not board_table.exists(): board_table.create(checkfirst=True) thread_table.create(checkfirst=True) post_table.create(checkfirst=True) log.debug("Forum tables have been created") for board_name, board_desc in DEFAULT_BOARDS.iteritems(): board = Board() board.name = board_name board.slug = slugify(board_name) board.description = board_desc session.add(board) if session.new: log.debug('Default boards created') session.commit() if not migration_table.exists(): migration_table.create(checkfirst=True) session.commit() if not banned_table.exists(): banned_table.create(checkfirst=True) session.commit() if not unsubscription_table.exists(): unsubscription_table.create(checkfirst=True) session.commit() migration_number = session.query(migration_table).count() log.debug('Migration number: %s', migration_number) migration_sql_list = [ "ALTER TABLE forum_post ADD COLUMN active boolean DEFAULT TRUE", "ALTER TABLE forum_thread ADD COLUMN active boolean DEFAULT TRUE", "ALTER TABLE forum_board ADD COLUMN active boolean DEFAULT TRUE", "ALTER TABLE forum_thread DROP COLUMN slug", "ALTER TABLE forum_thread ADD COLUMN can_post boolean DEFAULT TRUE", "ALTER TABLE forum_board ADD COLUMN can_post boolean DEFAULT TRUE", u"INSERT INTO forum_board(\"id\", \"name\", \"slug\", \"description\", \"active\", \"can_post\") " + u"VALUES(DEFAULT, 'Запропонувати набір', 'zaproponuvati-nabir', '', true, false)" ] for counter, sql in enumerate(migration_sql_list, start=1): if migration_number < counter: try: log.debug(sql) session.execute(sql) except ProgrammingError as e: print(e) log.debug('Migration have been rolled back.') session.rollback() finally: session.execute(migration_table.insert()) session.commit() session.close()