-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from flask-dashboard/development
Development
- Loading branch information
Showing
10 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"author": "Patrick Vogel, Thijs Klooster & Bogdan Petre", | ||
"email": "patrickvogel@live.nl" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Use this file for migrating your data from SQLite to MySQL. It only works for version >=2.0.0 of the Flask | ||
Monitoring Dashboard. For migrating your data from version 1.x.x to 2.y.y, use the script "migrate_v1_to_v2.py". | ||
Before running the script, make sure to change the SQLITE_URL and MYSQL_URL variables. Refer to | ||
http://docs.sqlalchemy.org/en/latest/core/engines.html on how to configure them. Also, make sure MySQL Server | ||
is running on your machine. | ||
""" | ||
import timeit | ||
|
||
from contextlib import contextmanager | ||
|
||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from flask_monitoringdashboard.database import Base, Endpoint, Request, Outlier, CodeLine, StackLine, Test, \ | ||
TestResult, TestEndpoint | ||
|
||
SQLITE_URL = 'sqlite:////Users/user/Flask-MonitoringDashboard/fmd.db' | ||
MYSQL_URL = 'mysql+pymysql://user:password@localhost:3306/database' | ||
ENTITIES = [Endpoint, Request, Outlier, CodeLine, StackLine, Test, TestResult, TestEndpoint] | ||
CHUNK_SIZE = 10000 | ||
|
||
|
||
def create_db_session(db_url, clean=False): | ||
engine = create_engine(db_url) | ||
if clean: | ||
Base.metadata.drop_all(engine) | ||
Base.metadata.create_all(engine) | ||
Base.metadata.bind = engine | ||
return sessionmaker(bind=engine) | ||
|
||
|
||
@contextmanager | ||
def session_scope(db_session): | ||
session = db_session() | ||
try: | ||
yield session | ||
session.commit() | ||
except Exception: | ||
session.rollback() | ||
raise | ||
finally: | ||
session.close() | ||
|
||
|
||
def get_row_count(db_session_old, entity_class): | ||
with session_scope(db_session_old) as db_session: | ||
count = db_session.query(entity_class).count() | ||
db_session.expunge_all() | ||
return count | ||
|
||
|
||
def get_old_instances(db_session_old, entity_class, start, end): | ||
with session_scope(db_session_old) as db_session: | ||
old_instances = db_session.query(entity_class).slice(start, end).all() | ||
db_session.expunge_all() | ||
return old_instances | ||
|
||
|
||
def migrate_batch(old_instances, db_session_new, entity_class): | ||
new_instances = [] | ||
with session_scope(db_session_new) as db_session: | ||
for (index, old_instance) in enumerate(old_instances): | ||
# _sa_instance_state is a non-db value used internally by SQLAlchemy | ||
del old_instance.__dict__['_sa_instance_state'] | ||
new_instances.append(entity_class(**old_instance.__dict__)) | ||
db_session.bulk_save_objects(new_instances) | ||
|
||
|
||
def migrate_all(db_session_old, db_session_new, entity_class, count): | ||
chunks = count // CHUNK_SIZE | ||
for i in range(chunks+1): | ||
print("Moving batch %d of %d for entity %s..." % (i+1, chunks+1, entity_class.__name__)) | ||
old_instances = get_old_instances(db_session_old, entity_class, i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE) | ||
migrate_batch(old_instances, db_session_new, entity_class) | ||
|
||
|
||
def main(): | ||
db_session_old = create_db_session(SQLITE_URL) | ||
db_session_new = create_db_session(MYSQL_URL, clean=True) | ||
t0 = timeit.default_timer() | ||
t_previous = t0 | ||
for entity_class in ENTITIES: | ||
count = get_row_count(db_session_old, entity_class) | ||
migrate_all(db_session_old, db_session_new, entity_class, count) | ||
t_now = timeit.default_timer() | ||
print("Moving %s took %f seconds" % (entity_class.__name__, t_now - t_previous)) | ||
t_previous = t_now | ||
print("Total time was %f seconds" % (t_previous - t0)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters