Giter Site home page Giter Site logo

CreateTable should be idempotent about pglock HOT 5 CLOSED

acim avatar acim commented on July 29, 2024
CreateTable should be idempotent

from pglock.

Comments (5)

ucirello avatar ucirello commented on July 29, 2024 1

you just mark in the changelog that this is breaking change and eventually bump major version.

Great idea actually!

from pglock.

ucirello avatar ucirello commented on July 29, 2024

Hi @acim - thanks for opening this issue and the PR.

At first, I decided to add an non-idempotent CreateTable method because I wanted a clear signal that the table was already there. CreateTable will indeed error if a table with the same name is present, which I believe to be more beneficial for two problems that hit me along the way.

  1. If the structure of the table changes, you won't get a silent failure.

  2. If the table name you chose collides with the use of the table by another application or library, you won't get a silent failure.

It is quite tempting to believe that the typical case of pglock is somewhere in a standalone world. My experience so far has shown me that to not be the case, pglock usually lives inside a larger application and sharing some database context with it.

Therefore, if anything, I'd like to see the CreateTable go away instead of being made smarter.

With all that said, you are not the first one who proposes this idea, which is starting to make me believe it is about time to think of a v2 for pglock. You could refer to further reasoning in: #28

from pglock.

acim avatar acim commented on July 29, 2024

I considered your reasoning before reporting this issue, but this is kind of choosing between two bad solutions. If you drop CreateTable then everyone has to create the table somehow else, either manually or by adding migration. Even in that case the problem of changing the table schema would remain.

The only automatic way would be to read the information_schema and find out if the table with correct schema is there or not.

Or maybe to add version field in the table and keep it there forever (in each schema change). Then you could find out if the schema version is fine or not and eventually panic (yes, panic is very bad in libraries) or something. I see that this is quite difficult topic even though it looks easy.

from pglock.

ucirello avatar ucirello commented on July 29, 2024

If you drop CreateTable then everyone has to create the table somehow else, either manually or by adding migration.

And I think that's fine. It is for the consumer of this package to decide how the database should be operated, not the library itself. I added a piece of sugar that is being misused quite frequently. For some applications, hot DB changes might be fine, but for others it might not. CreateTable offers a tool that is hard to wield correctly.

Even in that case the problem of changing the table schema would remain.

You are right. The problem remains but it would not be pglock's responsibility any longer.

The only automatic way would be to read the information_schema and find out if the table with correct schema is there or not.

And perhaps the more fundamental question is why it needs to be automatic in the first place.

I see that this is quite difficult topic even though it looks easy.

I agree. And because of the Go 1 compatibility promise, I can't do a lot except trying to not make the situation any worse or much different than what it currently is.

from pglock.

acim avatar acim commented on July 29, 2024

You are right. The problem remains but it would not be pglock's responsibility any longer.

Well, in case you have to change the schema any time in the future, you just mark in the changelog that this is breaking change and eventually bump major version. I mean it would be nice to inform users about this so that they can't miss it. :D

from pglock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.