Skip to content

Refactor sleep 1 in migration generator to use proper timestamp handling #49

@kanejamison

Description

@kanejamison

Description

The install rake task uses sleep 1 to ensure different migration timestamps, which is fragile and can fail under various conditions (slow systems, interruptions, etc.).

Current code

lib/tasks/bunko/install.rake:21:

create_post_types_migration(skip_seo: skip_seo, json_content: json_content)
sleep 1 # Ensure different timestamps
create_posts_migration(skip_seo: skip_seo, json_content: json_content)

Problems this causes

  1. Fragile - relies on system timing
  2. Slows down installation unnecessarily
  3. Could still collide if system is slow or task is interrupted
  4. Not how Rails generators handle this problem
  5. Fails if user Ctrl+C's during sleep

Pros of refactoring

  • More reliable timestamp generation
  • Faster installation
  • Follows Rails patterns
  • No possibility of collision
  • Better user experience

Cons of refactoring

  • Need to implement proper timestamp incrementing
  • Slightly more complex logic

Recommended approach

Increment timestamps programmatically like Rails generators do:

base_timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
timestamp1 = base_timestamp
timestamp2 = (Time.now.utc + 1.second).strftime("%Y%m%d%H%M%S")

# Or simpler:
timestamp1 = Time.now.utc.strftime("%Y%m%d%H%M%S")
timestamp2 = (Time.now.utc.to_i + 1).to_s.ljust(14, '0')

References

  • lib/tasks/bunko/install.rake:21

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions