Skip to content

Commit 281cbd7

Browse files
migrate command (#61)
1 parent 8d9d299 commit 281cbd7

File tree

19 files changed

+79
-54
lines changed

19 files changed

+79
-54
lines changed

src/demo_hic_et_nunc/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spec_version: 0.1
1+
spec_version: 1.0
22
package: demo_hic_et_nunc
33

44
database:

src/demo_quipuswap/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spec_version: 0.1
1+
spec_version: 1.0
22
package: demo_quipuswap
33

44
database:

src/demo_registrydao/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spec_version: 0.1
1+
spec_version: 1.0
22
package: demo_registrydao
33

44
database:

src/demo_tezos_domains/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spec_version: 0.1
1+
spec_version: 1.0
22
package: demo_tezos_domains
33

44
database:

src/demo_tezos_domains_big_map/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spec_version: 0.1
1+
spec_version: 1.0
22
package: demo_tezos_domains_big_map
33

44
database:

src/demo_tzbtc/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spec_version: 0.1
1+
spec_version: 1.0
22
package: demo_tzbtc
33

44
database:

src/demo_tzcolors/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spec_version: 0.1
1+
spec_version: 1.0
22
package: demo_tzcolors
33

44
database:

src/dipdup/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
__version__ = '0.4.3'
2+
__spec_version__ = '1.0'

src/dipdup/cli.py

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
import asyncio
2+
import fileinput
23
import logging
34
import os
45
from dataclasses import dataclass
56
from functools import wraps
67
from os.path import dirname, join
7-
from typing import List
8+
from typing import List, NoReturn
89

910
import click
1011
from fcache.cache import FileCache # type: ignore
1112

12-
from dipdup import __version__
13+
from dipdup import __spec_version__, __version__
1314
from dipdup.config import DipDupConfig, LoggingConfig
1415
from dipdup.dipdup import DipDup
15-
from dipdup.exceptions import HandlerImportError
1616

1717
_logger = logging.getLogger(__name__)
1818

19+
spec_version_to_version = {
20+
'0.1': 'dipdup <0.4.3',
21+
'1.0': 'dipdup ^1.0.0',
22+
}
23+
24+
migration_required_message = """
25+
26+
Migration required!
27+
28+
project spec version: %s (%s)
29+
current spec version: %s (%s)
30+
31+
1. Run `dipdup migrate`
32+
2. Review and commit changes
33+
34+
See https://baking-bad.org/blog/ for additional release information.
35+
"""
36+
37+
38+
def migration_required(from_: str, to: str) -> NoReturn:
39+
_logger.warning(
40+
migration_required_message,
41+
from_,
42+
spec_version_to_version[from_],
43+
to,
44+
spec_version_to_version[to],
45+
)
46+
quit()
47+
1948

2049
def click_async(fn):
2150
@wraps(fn)
@@ -27,6 +56,7 @@ def wrapper(*args, **kwargs):
2756

2857
@dataclass
2958
class CLIContext:
59+
config_paths: List[str]
3060
config: DipDupConfig
3161
logging_config: LoggingConfig
3262

@@ -45,8 +75,13 @@ async def cli(ctx, config: List[str], logging_config: str):
4575
path = join(dirname(__file__), 'configs', logging_config)
4676
_logging_config = LoggingConfig.load(path)
4777
_logging_config.apply()
78+
4879
_config = DipDupConfig.load(config)
80+
if _config.spec_version != __spec_version__ and ctx.invoked_subcommand != 'migrate':
81+
migration_required(_config.spec_version, __spec_version__)
82+
4983
ctx.obj = CLIContext(
84+
config_paths=config,
5085
config=_config,
5186
logging_config=_logging_config,
5287
)
@@ -59,12 +94,7 @@ async def cli(ctx, config: List[str], logging_config: str):
5994
@click_async
6095
async def run(ctx, reindex: bool, oneshot: bool) -> None:
6196
config: DipDupConfig = ctx.obj.config
62-
63-
try:
64-
config.initialize()
65-
except HandlerImportError:
66-
await DipDup(config).migrate()
67-
97+
config.initialize()
6898
dipdup = DipDup(config)
6999
await dipdup.run(reindex, oneshot)
70100

@@ -79,6 +109,22 @@ async def init(ctx):
79109
await dipdup.init()
80110

81111

112+
@cli.command(help='Migrate project to the new spec version')
113+
@click.pass_context
114+
@click_async
115+
async def migrate(ctx):
116+
config: DipDupConfig = ctx.obj.config
117+
config.pre_initialize()
118+
await DipDup(config).migrate()
119+
120+
for config_path in ctx.obj.config_paths:
121+
for line in fileinput.input(config_path, inplace=True):
122+
if 'spec_version' in line:
123+
print(f'spec_version: {__spec_version__}')
124+
else:
125+
print(line.rstrip())
126+
127+
82128
@cli.command(help='Clear development request cache')
83129
@click.pass_context
84130
@click_async

src/dipdup/codegen.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,20 +249,14 @@ async def generate_default_handlers(self, recreate=False) -> None:
249249
self._logger.info('Generating handler `%s`', CONFIGURE_HANDLER)
250250
handler_code = configure_template.render()
251251
handler_path = join(handlers_path, f'{CONFIGURE_HANDLER}.py')
252-
if exists(handler_path) and recreate:
253-
old_handler_path = join(handlers_path, f'{CONFIGURE_HANDLER}.py.bak')
254-
os.rename(handler_path, old_handler_path)
255-
if not exists(handler_path):
252+
if not exists(handler_path) or recreate:
256253
with open(handler_path, 'w') as file:
257254
file.write(handler_code)
258255

259256
self._logger.info('Generating handler `%s`', ROLLBACK_HANDLER)
260257
handler_code = rollback_template.render()
261258
handler_path = join(handlers_path, f'{ROLLBACK_HANDLER}.py')
262-
if exists(handler_path) and recreate:
263-
old_handler_path = join(handlers_path, f'{ROLLBACK_HANDLER}.py.bak')
264-
os.rename(handler_path, old_handler_path)
265-
if not exists(handler_path):
259+
if not exists(handler_path) or recreate:
266260
with open(handler_path, 'w') as file:
267261
file.write(handler_code)
268262

@@ -342,7 +336,6 @@ async def _get_schema(
342336
return self._schemas[datasource_config][address]
343337

344338
async def migrate_user_handlers_to_v1(self) -> None:
345-
# TODO: Save backups
346339
remove_lines = [
347340
'from dipdup.models import',
348341
'from dipdup.context import',
@@ -356,8 +349,8 @@ async def migrate_user_handlers_to_v1(self) -> None:
356349
'TransactionContext': 'Transaction',
357350
'OriginationContext': 'Origination',
358351
'BigMapContext': 'BigMapDiff',
359-
'HandlerContext': 'HandlerContext',
360-
'HandlerContext': 'HandlerContext',
352+
'OperationHandlerContext': 'HandlerContext',
353+
'BigMapHandlerContext': 'HandlerContext',
361354
}
362355
handlers_path = join(self._config.package_path, 'handlers')
363356

@@ -366,10 +359,8 @@ async def migrate_user_handlers_to_v1(self) -> None:
366359
if filename == '__init__.py' or not filename.endswith('.py'):
367360
continue
368361
path = join(root, filename)
369-
bak_path = path + '.bak'
370-
os.rename(path, bak_path)
371362
newfile = copy(add_lines)
372-
with open(bak_path) as file:
363+
with open(path) as file:
373364
for line in file.read().split('\n'):
374365
# Skip existing models imports
375366
if any(map(lambda l: l in line, remove_lines)):

0 commit comments

Comments
 (0)