Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add migrations to improve initial DB state with default objects (#11554)

Marcin Gębala7adf44cc131b
diff --git a/saleor/page/migrations/0028_add_default_page_type.py b/saleor/page/migrations/0028_add_default_page_type.py
new file mode 100644
index 0000000000..bec9364043
--- /dev/null
+++ b/saleor/page/migrations/0028_add_default_page_type.py
@@ -0,0 +1,21 @@
+# Generated by Django 3.2.16 on 2022-12-20 14:39
+
+from django.db import migrations
+from django.conf import settings
+
+
+def create_default_page_type(apps, schema_editor):
+ PageType = apps.get_model("page", "PageType")
+ if not PageType.objects.all().exists() and settings.POPULATE_DEFAULTS:
+ PageType.objects.create(name="Default Type", slug="default-type")
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("page", "0027_alter_page_created_at"),
+ ]
+
+ operations = [
+ migrations.RunPython(create_default_page_type, migrations.RunPython.noop),
+ ]
diff --git a/saleor/shipping/migrations/0033_add_default_shipping.py b/saleor/shipping/migrations/0033_add_default_shipping.py
new file mode 100644
index 0000000000..d7d9199a37
--- /dev/null
+++ b/saleor/shipping/migrations/0033_add_default_shipping.py
@@ -0,0 +1,44 @@
+# Generated by Django 3.2.16 on 2022-12-20 15:52
+
+from django.db import migrations
+from django.conf import settings
+
+from .. import ShippingMethodType
+
+
+def add_default_shipping(apps, schema_editor):
+ Channel = apps.get_model("channel", "Channel")
+ ShippingMethod = apps.get_model("shipping", "ShippingMethod")
+ ShippingZone = apps.get_model("shipping", "ShippingZone")
+ Warehouse = apps.get_model("warehouse", "Warehouse")
+
+ channel = Channel.objects.filter(slug="default-channel").first()
+ warehouse = Warehouse.objects.filter(slug="default-warehouse").first()
+
+ if not ShippingZone.objects.all().exists() and settings.POPULATE_DEFAULTS:
+ shipping_zone = ShippingZone.objects.create(name="Default", countries=["US"])
+ shipping_method = ShippingMethod.objects.create(
+ name="Default",
+ type=ShippingMethodType.PRICE_BASED,
+ shipping_zone=shipping_zone,
+ )
+
+ if warehouse:
+ shipping_zone.warehouses.add(warehouse)
+
+ if channel:
+ shipping_zone.channels.add(channel)
+ shipping_method.channel_listings.create(
+ channel=channel, currency=channel.currency_code, price_amount=0
+ )
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("channel", "0004_create_default_channel"),
+ ("shipping", "0032_shippingmethod_tax_class"),
+ ("warehouse", "0031_create_default_warehouse"),
+ ]
+
+ operations = [migrations.RunPython(add_default_shipping, migrations.RunPython.noop)]
diff --git a/saleor/tax/migrations/0002_add_default_tax_configs.py b/saleor/tax/migrations/0002_add_default_tax_configs.py
index 115322a673..e41fe81a6e 100644
--- a/saleor/tax/migrations/0002_add_default_tax_configs.py
+++ b/saleor/tax/migrations/0002_add_default_tax_configs.py
@@ -18,17 +18,27 @@ def add_tax_configuration_for_channels(apps, schema_editor):
SiteSettings = apps.get_model("site", "SiteSettings")
site_settings = SiteSettings.objects.first()
-
tax_configurations = []
+
for channel in Channel.objects.all():
- tax_configurations.append(
- TaxConfiguration(
- channel=channel,
- charge_taxes=True,
- display_gross_prices=site_settings.display_gross_prices,
- prices_entered_with_tax=site_settings.include_taxes_in_prices,
- )
+
+ # For US channels create a configuration that's meaningful in US, where prices
+ # as entered and displayed without tax.
+ if channel.default_country == "US":
+ display_gross_prices = False
+ prices_entered_with_tax = False
+ else:
+ display_gross_prices = site_settings.display_gross_prices
+ prices_entered_with_tax = site_settings.include_taxes_in_prices
+
+ tax_configuration = TaxConfiguration(
+ channel=channel,
+ charge_taxes=True,
+ display_gross_prices=display_gross_prices,
+ prices_entered_with_tax=prices_entered_with_tax,
)
+ tax_configurations.append(tax_configuration)
+
TaxConfiguration.objects.bulk_create(tax_configurations)
@@ -99,7 +109,14 @@ def populate_tax_calculation_strategy(apps, schema_editor):
class Migration(migrations.Migration):
- dependencies = [("tax", "0001_initial"), ("plugins", "0010_auto_20220104_1239")]
+ dependencies = [
+ ("app", "0017_app_audience"),
+ ("channel", "0005_channel_allocation_strategy"),
+ ("site", "0034_sitesettings_limit_quantity_per_checkout"),
+ ("tax", "0001_initial"),
+ ("plugins", "0010_auto_20220104_1239"),
+ ("webhook", "0008_webhook_subscription_query"),
+ ]
operations = [
migrations.RunPython(
diff --git a/saleor/warehouse/migrations/0031_create_default_warehouse.py b/saleor/warehouse/migrations/0031_create_default_warehouse.py
index 5455c37020..b610605f51 100644
--- a/saleor/warehouse/migrations/0031_create_default_warehouse.py
+++ b/saleor/warehouse/migrations/0031_create_default_warehouse.py
@@ -5,14 +5,19 @@ from django.conf import settings
def create_default_warehouse(apps, schema_editor):
if settings.POPULATE_DEFAULTS:
address = apps.get_model("account", "Address").objects.create(country="US")
+ Channel = apps.get_model("channel", "Channel")
Warehouse = apps.get_model("warehouse", "Warehouse")
if not Warehouse.objects.all().exists():
- Warehouse.objects.create(
+ warehouse = Warehouse.objects.create(
address=address,
name="Default Warehouse",
slug="default-warehouse",
)
+ channel = Channel.objects.filter(slug="default-channel").first()
+ if channel:
+ warehouse.channels.add(channel)
+
class Migration(migrations.Migration):