Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle Analysis: add ability to ingest dynamic imports #445

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we need any indexes on this?

create index idx_chunk_id on dynamic_imports (chunk_id);
create index idx_asset_id on dynamic_imports (asset_id);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The good thing about these "databases" are that it is super small (like 100kB), it only matches bundle data for 1 singular commit. This means we don't need to worry about performance on querying things haha.

);
""",
]

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
Loading