Hey everyone! :blush: I'm working on managing data...
# automation-api
h
Hey everyone! 😊 I'm working on managing databases with Pulumi, and I’d like to add new databases dynamically. In the future, I also want to manage roles and other resources. The issue I'm facing is that when I run the code for the first time, the database is created just fine, but if I run it again, instead of adding a new one, it either renames the existing one or deletes it and creates a new one from scratch. Does anyone know why this happens? Is there a way to make Pulumi add new databases without overwriting the existing ones? I know that changing the stack name fixes the issue, but that doesn’t seem like the best approach. Is this the only way to do it? Here’s the code for my NestJS service in case anyone wants to take a look. Thanks:
Copy code
@Injectable()
export class PulumiPostgresService {
 
  private stackName = 'dev-databases';
  private projectName = 'dev-manager';

  async createDatabase(dto:DatabaseConfigDto ) {
    try {
      const stack = await LocalWorkspace.createOrSelectStack({
        stackName: this.stackName,
        projectName: this.projectName,
        program: this.createPulumiProgram(dto),
      });

      // Desplegar
      const result = await stack.up({ onOutput: console.log });
      return { success: true, details: result.summary };
    } catch (error) {
      console.error('Error:', error);
      
    }
  }

  private createPulumiProgram(dto: DatabaseConfigDto) {
    return async () => {
      const provider = new postgresql.Provider('main-provider', {
        host: dto.host,
        port: dto.port,
        username: dto.username,
        password: dto.password,
        sslmode: dto.sslmode,
        superuser: dto.superuser,
      });

      new postgresql.Database(dto.database.name, {
        name: dto.database.name,
      }, { provider });
    };
  }
  }
h
Hey there, yeah pulumi is a little strange in that it is a combination of imperative and declarative programming. To do what you are trying to do you most likely should make a data structure like a list that includes the info about the databases you want to create and then loop through it calling createDatabase. The first time it runs (or any time it runs after the database might have been deleted manually) it will create the database, on other runs it will just check if the settings are right or not and fix any that aren't.
l
To answer this:
Does anyone know why this happens?
It's because you're using the same value for
dto.database.name
in both cases. That's a lookup into Pulumi's state. If you've run the program once with value "test1", then it'll create a database called "test1". If you run it again with value "test1", it'll update the existing database.
h
Thanks for the explanation! So just to confirm, is this the only way to handle it? My concern is that if the list gets deleted (e.g., if the project restarts), Pulumi won't correct it. Wouldn't that mean I need to store the state of the databases in an external database to ensure consistency?
h
The list has to be stored somewhere. Either a literal list in the code, or in a data structure somewhere. We settled on keeping the list in our database and pass the list in a function call to pulumi
h
Hey, thanks for the explanation! I'm still new to Pulumi and learning how it works👍
l
So long as you don't reuse names, you'll be fine. But if you're using the same stack over and over, and that stack has code for only one database, then there'll be only one database ever.
h
Yep, no worries, we're all learning!