Skip to content

Instantly share code, notes, and snippets.

@Frenchcooc
Last active July 22, 2020 22:28
Show Gist options
  • Save Frenchcooc/919c34a5033f3b902acde72b5122dfc3 to your computer and use it in GitHub Desktop.
Save Frenchcooc/919c34a5033f3b902acde72b5122dfc3 to your computer and use it in GitHub Desktop.

How to rename a setupId in Pizzly.

Pizzly uses uuidv4 to generate pseudo-random setupId, such as a3ef22ba-8916-424f-b613-9e8608026094. In some cases, you might be interested in renaming the setupId to something more meaningful. Here's how to do it.

Server side

Assuming you can't run SQL queries on your production database, to rename a setupId on your Pizzly instance (e.g. on Heroku), you'll have to create a new endpoint on Pizzly's API and request that endpoint each time you want to rename a setupId.

Here's how to do it:

  1. Open your Pizzly repository (or clone the official repo)

  2. Open the file under src/routes/api.ts

  3. After the test endpoint (around line 30), copy/paste the following code:

    /**
     * Custom endpoint to rename a setupId
     */
    
    api.put('/custom/rename-setup-id', async (req, res, next) => {
      const currentSetupId = req.body.current_setup_id
      const newSetupId = req.body.new_setup_id
    
      if (!currentSetupId || !newSetupId) {
        next(new Error("Can't rename setupId. Invalid params"))
        return
      }
    
      let affectedRows = 0
      const startTime = Date.now()
    
      // Rename the configuration
      affectedRows += await store('configurations')
        .update({ setup_id: newSetupId })
        .where({ setup_id: currentSetupId })
    
      // Rename all authentications
      affectedRows += await store('authentications')
        .update({ setup_id: newSetupId })
        .where({ setup_id: currentSetupId })
    
      // Log result
      const endTime = Date.now()
      res.send(`Done in ${endTime - startTime}ms - ${affectedRows} rows affected.`)
    })
    
    // Example:
    //
    // curl -X PUT "http://pizzly.example.org/api/custom/rename-setup-id" \
    //  -u "replace-with-your-secret-key:" \
    //  -H "Content-Type: application/json" \
    //  --data '{"current_setup_id": "foo", "new_setup_id": "bar"}'
  4. Save then redeploy your code.

    git add . && git commit -m "custom endpoint to rename a setupId"

    To redeploy your Pizzly instance hosted on Heroku, use git push heroku master

  5. When you want to rename a setupId, make a cURL request to that custom endpoint:

    curl -X PUT "http://pizzly.example.org/api/custom/rename-setup-id" \
     -u "replace-with-your-secret-key:" \
     -H "Content-Type: application/json" \
     --data '{"current_setup_id": "", "new_setup_id": ""}'
    

    💡 Remember to use your own secretKey before triggering that command.

On success, you will have something like this: Done in 16ms - 6 rows affected.

Locally

To rename a setupId on your own machine, or if you have access to your server using SSH, follow these steps:

  1. Add a new file pizzly-rename-setup-id.js at the root of your Pizzly folder.

    touch pizzly-rename-setup-id.js
  2. Copy/paste the following content in that new file:

    const { store } = require('./dist/src/lib/database')
    
    const currentSetupId = process.env.CURRENT_SETUP_ID
    const newSetupId = process.env.NEW_SETUP_ID
    
    if (!currentSetupId || !newSetupId) {
      console.log(
        'Initialize this script as follow:',
        '\n',
        '$ CURRENT_SETUP_ID="" NEW_SETUP_ID="" node pizzly-rename-setup-id.js'
      )
    
      return process.exit(1)
    }
    
    ;(async () => {
      let affectedRows = 0
      const startTime = Date.now()
    
      // Rename the configuration
      affectedRows += await store('configurations')
        .update({ setup_id: newSetupId })
        .where({ setup_id: currentSetupId })
    
      // Rename all authentications
      affectedRows += await store('authentications')
        .update({ setup_id: newSetupId })
        .where({ setup_id: currentSetupId })
    
      // Log result
      const endTime = Date.now()
      console.log(`Done in ${endTime - startTime}ms`, '-', `${affectedRows} rows affected.`)
      process.exit(1)
    })()
  3. Run the script:

    CURRENT_SETUP_ID="" NEW_SETUP_ID="" node pizzly-rename-setup-id.js

What's next?

Got a question? Have a feedback, please open an issue on Pizzly repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment