Skip to content
Open
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
2 changes: 2 additions & 0 deletions iam/contrib/iam_migration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

default_app_config = "iam.contrib.iam_migration.apps.IAMMigrationConfig"
29 changes: 28 additions & 1 deletion iam/contrib/iam_migration/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,39 @@
specific language governing permissions and limitations under the License.
"""

import os
import sys

import six
from django.apps import AppConfig
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from iam.contrib.iam_migration.constants import APP_NAME, APP_VERBOSE_NAME
from iam.contrib.iam_migration.constants import APP_NAME, APP_VERBOSE_NAME, BK_IAM_MIGRATION_APP_NAME


class IAMMigrationConfig(AppConfig):
name = APP_NAME
label = name.rpartition(".")[2]
default = True
verbose_name = APP_VERBOSE_NAME

def ready(self):
# dist-packages: Debian distributions modify upstream Python
for site_or_dist in ("site-packages", "dist-packages"):
module_package_path = os.path.join("lib", "python%d.%d" % sys.version_info[:2], site_or_dist, "iam")
if module_package_path in self.path:
break
else:
return
# Checking must be set `BK_IAM_MIGRATION_APP_NAME` for pip installation
if not getattr(settings, BK_IAM_MIGRATION_APP_NAME, None):
raise ImproperlyConfigured(
"The %r setting must not be empty for iam with pip installation package" % BK_IAM_MIGRATION_APP_NAME
)
if not isinstance(settings.BK_IAM_MIGRATION_APP_NAME, six.string_types):
raise ImproperlyConfigured(
"The %r setting must be instance of %s" % (BK_IAM_MIGRATION_APP_NAME, six.string_types)
)
if settings.BK_IAM_MIGRATION_APP_NAME not in settings.INSTALLED_APPS:
raise ImproperlyConfigured("The %r setting not in installed apps" % BK_IAM_MIGRATION_APP_NAME)
4 changes: 2 additions & 2 deletions iam/contrib/iam_migration/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@

from django.conf import settings

from iam.contrib.iam_migration.constants import APP_NAME
from iam.contrib.iam_migration.constants import APP_NAME, BK_IAM_MIGRATION_APP_NAME

MIGRATION_APP_NAME = getattr(settings, "BK_IAM_MIGRATION_APP_NAME", APP_NAME)
MIGRATION_APP_NAME = getattr(settings, BK_IAM_MIGRATION_APP_NAME, APP_NAME)
3 changes: 2 additions & 1 deletion iam/contrib/iam_migration/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"""


APP_NAME = "iam_migration"
APP_NAME = "iam.contrib.iam_migration"
APP_VERBOSE_NAME = "IAM Migration"
BK_IAM_MIGRATION_APP_NAME = "BK_IAM_MIGRATION_APP_NAME"
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ def handle(self, *args, **options):
json_file = options["migration_json"]
if not json_file:
sys.stderr.write("please provide a migration json file name\n")
exit(1)
sys.exit(1)

json_path = getattr(settings, "BK_IAM_MIGRATION_JSON_PATH", "support-files/iam/")
file_path = os.path.join(settings.BASE_DIR, json_path, json_file)

if not os.path.exists(file_path):
sys.stderr.write("{} is not exist.\n".format(file_path))
exit(1)
sys.exit(1)

iam_migration_app_instance = apps.get_app_config(IAMMigrationConfig.label)
iam_migration_app_instance.ready()
loader = MigrationLoader(None, ignore_no_migrations=False)
migration_leaf = loader.graph.leaf_nodes(conf.MIGRATION_APP_NAME)

Expand All @@ -62,8 +63,11 @@ def handle(self, *args, **options):

migration_name = self.migration_name(last_migration_name)
migration_file = "{}.py".format(
os.path.join(apps.get_app_config(conf.MIGRATION_APP_NAME).path, "migrations", migration_name)
os.path.join(
apps.get_app_config(conf.MIGRATION_APP_NAME.rpartition(".")[2]).path, "migrations", migration_name
)
)
sys.stdout.write("write migrations file: {}\n".format(migration_file))

with codecs.open(migration_file, mode="w", encoding="utf-8") as fp:
template = Engine.get_default().from_string(migration_template)
Expand All @@ -86,7 +90,7 @@ def migration_name(self, last_migration_name):

if not system_id:
self.stderr.write("You must set BK_IAM_SYSTEM_ID in django settings before make migrations")
exit(1)
sys.exit(1)

if not last_migration_name:
return "0001_initial"
Expand Down