Skip to content

Commit

Permalink
Bundle Analysis: add ability to ingest dynamic imports (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrySentry authored Dec 3, 2024
1 parent 5fb0c83 commit 45252f7
Show file tree
Hide file tree
Showing 9 changed files with 989 additions and 2 deletions.
4 changes: 4 additions & 0 deletions shared/bundle_analysis/db_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from shared.bundle_analysis.migrations.v003_modify_gzip_size_nullable import (
modify_gzip_size_nullable,
)
from shared.bundle_analysis.migrations.v004_add_dynamic_imports import (
add_dynamic_imports,
)


class BundleAnalysisMigration:
Expand All @@ -33,6 +36,7 @@ def __init__(self, db_session: Session, from_version: int, to_version: int):
2: add_gzip_size,
3: add_is_cached,
4: modify_gzip_size_nullable,
5: add_dynamic_imports,
}

def update_schema_version(self, version):
Expand Down
27 changes: 27 additions & 0 deletions shared/bundle_analysis/migrations/v004_add_dynamic_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from sqlalchemy import text
from sqlalchemy.orm import Session


def add_dynamic_imports(db_session: Session):
"""
Adds a table called dynamic_imports (DynamicImport model name)
This table represents for a given Chunk what are its dynamically
imported Assets, if applicable.
There is no data available to migrate, any older versions of bundle
reports will be considered to not have dynamic imports
"""
stmts = [
"""
CREATE TABLE dynamic_imports (
chunk_id integer not null,
asset_id integer not null,
primary key (chunk_id, asset_id),
foreign key (chunk_id) references chunks (id),
foreign key (asset_id) references assets (id)
);
""",
]

for stmt in stmts:
db_session.execute(text(stmt))
29 changes: 28 additions & 1 deletion shared/bundle_analysis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,17 @@
foreign key (chunk_id) references chunks (id),
foreign key (module_id) references modules (id)
);
create table dynamic_imports (
chunk_id integer not null,
asset_id integer not null,
primary key (chunk_id, asset_id),
foreign key (chunk_id) references chunks (id),
foreign key (asset_id) references assets (id)
);
"""

SCHEMA_VERSION = 4
SCHEMA_VERSION = 5

Base = declarative_base()

Expand Down Expand Up @@ -275,6 +283,25 @@ class Module(Base):
)


class DynamicImport(Base):
"""
These represents a mapping of each chunk's dynamically imported assets
"""

__tablename__ = "dynamic_imports"

chunk_id = Column(types.Integer, ForeignKey("chunks.id"), primary_key=True)
asset_id = Column(types.Integer, ForeignKey("assets.id"), primary_key=True)

# Relationships
chunk = relationship(
"Chunk", backref=backref("dynamic_imports", cascade="all, delete-orphan")
)
asset = relationship(
"Asset", backref=backref("dynamic_imports", cascade="all, delete-orphan")
)


class MetadataKey(Enum):
SCHEMA_VERSION = "schema_version"
COMPARE_SHA = "compare_sha"
3 changes: 2 additions & 1 deletion shared/bundle_analysis/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ijson
from sqlalchemy.orm import Session as DbSession

from shared.bundle_analysis.parsers import ParserInterface, ParserV1, ParserV2
from shared.bundle_analysis.parsers import ParserInterface, ParserV1, ParserV2, ParserV3
from shared.bundle_analysis.parsers.base import ParserTrait

log = logging.getLogger(__name__)
Expand All @@ -12,6 +12,7 @@
PARSER_VERSION_MAPPING: dict[str, type[ParserTrait]] = {
"1": ParserV1,
"2": ParserV2,
"3": ParserV3,
}


Expand Down
2 changes: 2 additions & 0 deletions shared/bundle_analysis/parsers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from shared.bundle_analysis.parsers.base import ParserInterface
from shared.bundle_analysis.parsers.v1 import ParserV1
from shared.bundle_analysis.parsers.v2 import ParserV2
from shared.bundle_analysis.parsers.v3 import ParserV3

__all__ = [
"ParserInterface",
"ParserV1",
"ParserV2",
"ParserV3",
]
Loading

0 comments on commit 45252f7

Please sign in to comment.