Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need an API to Ensure schema exists in postgressql #7

Open
hari2m opened this issue Jan 20, 2022 · 4 comments
Open

Need an API to Ensure schema exists in postgressql #7

hari2m opened this issue Jan 20, 2022 · 4 comments

Comments

@hari2m
Copy link

hari2m commented Jan 20, 2022

When working with Postgresql. I am using docker instance as I develop locally to spin up and test my code, in this case I am using ensure database so that it creates the database when I spin up new docker container. But the code fails as the schema does not exist and I have to create a schema manually. It would help if we have a ensure schema API.

@AdrianJSClark
Copy link
Member

I'm a bit confused, @hari2m. Wouldn't ensuring the schema exists simply be part of the SQL scripts that DbUp executes on an empty database?

That's how I've always used DbUp.

@hari2m
Copy link
Author

hari2m commented Jan 22, 2022

My scripts does ensure the schema. But the problem is when the dbup tries to create the schemahistory table. I asked it to create the table within the schema and by then my scripts are not executed. So the process errors due to missing schema.

@felipeschneider88
Copy link

felipeschneider88 commented Mar 10, 2022

Hello, for me when I use a different schema, I add the create schema as part of the 0001.sql file. If you check the file src\dbup-postgresql\PostgresqlExtensions.cs, on the method

` sqlCommandText = string.Format
(
"create database "{0}";",
databaseName
);

        // Create the database...
        using (var command = new NpgsqlCommand(sqlCommandText, connection)
        {
            CommandType = CommandType.Text
        })
        {
            command.ExecuteNonQuery();

        }`https://assets.grammarly.com/emoji/v1/1f610.svg

If you want to create the schema if not exist. You should not use JournalToPostgresqlTable, you can use the PerformUpgrade like this:

var upgrader =
      DeployChanges.To
      .SqlDatabase(database.SharedConnection)
      .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
      .LogToConsole()
      .Build();

  var watch = new Stopwatch();
  watch.Start();

  var result = upgrader.PerformUpgrade();

@mjauernig
Copy link
Member

Note: Write documentation how to use dbup with auto creation of schema

@droyad droyad transferred this issue from DbUp/DbUp Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Documentation
Development

No branches or pull requests

4 participants