Git Product home page Git Product logo

nschemer's Introduction

NSchemer Build status

.Net-based database schema management for .NET Standard 2.0+

Why?

If you're not managing your schema automatically, you're doing it wrong. In dev environments, automatically update the schema on project launch. In all other environments (test, UAT, production), update it during your automated deployment process.

There are plenty of options out there. NSchemer gives you options, while being simple and light-weight. Microsoft SQL Server is the target platform for now, but the architecture should support adding other platforms in the future. If you're interested in building support or having me build support for a new platform, please get in touch.

Please note version 2.x contains breaking changes - please see below for an upgrade guide.

How

Create a schema class

Start off with nuget install-package NSchemer.SqlServer

Take a look at the system test project for a real example - but it's pretty simple. Inherit from SqlClientDatabase and implement the minimum to build. Your schema class might look like this:

    public class TestSchema : SqlClientDatabase
    {
        public TestSchema(string connectionString) : base(connectionString) {}

        public override List<ITransition> Versions
        {
            get
            {
                return new List<ITransition>
                {
                    new CodeTransition(1, "Initial Schema", BuildTheWorld),
                    new CodeTransition(2, "Add Widget Table", AddWidgets)
                };
            }
        }

        private bool BuildTheWorld()
        {
            CreateTable("Thing",
                new Column("ThingId", DataType.BIGINT).AsIdentity(1, 1).AsPrimaryKey(),
                new Column("ThingName", DataType.STRING, 50)                
            );
            CreateTable("ThingAnnotation",
                new Column("AnnotationId", DataType.BIGINT).AsIdentity(1, 1).AsPrimaryKey(),
                new Column("Text", DataType.STRING, 50),
                new Column("ThingId", DataType.BIGINT, false).AsForeignKey("Thing", "ThingId")
            );
            return true;
        }

        private bool AddWidgets()
        {
            RunSql(@"CREATE TABLE DBO.Widget (WidgetId [int],WidgetName [nvarchar](50)) ON [PRIMARY]");
            return true;
        }
    }

You can use the helper methods (CreateTable, AddColumn, etc.), or you can drop SQL in-line. You can also use files:

	new SqlScriptTransition(3, "Add another table", "NSchemer.SystemTests.EmbeddedFile.sql")

Update your schema

new TestSchema("[connection string]").Update()

Upgrading from NSchemer 1.x

Once you upgrade from 1.x to 2.x, you will need to install the SQL Server provider package (this was included in the NSchemer 1.x package): nuget install-package NSchemer.SqlServer

Remove all of your return true;s from CodeTransitions, and change any return false;s (did anyone ever do that?) to throw an exception instead. There were a few other methods which had unnecessary bool return values removed - make sure any return false; is swapped with a throw new Exception("Relevant message"); and check for anything which was handling the false return value.

Optional: remove name from any of your version constructors which also provided a description. It was never used anyway. If you were using the constructor with only name it will silently start using that as the description instead.

Is it stable?

I do my best to keep it stable, but I don't provide a guarantee - this is a free library. However stable I keep it, I highly recommend your automated tests include schema update tests. If that's not feasible, at least ensure your various non-production environments will thoroughly exercise your schema migrations.

In short, NSchemer should be stable - but if you rely on it for important things, your testing should ensure that whichever version you're on will work with your environment and your schema.

What was I thinking...

... when I included both a name and description in transitions in NSchemer 1.x?

Dunno. The original interface started many years ago, and I've questioned this decision many times. 2.x removes this requirement.

... when I decided to require version numbers?

I wanted ordering to be extremely predictable. I believe that every non-developer-PC schema you ever produce should follow exactly the same code-path - and using explicit version numbers helps you do this. However, I understand that this can be difficult in multi-developer environments. In the past, I've solved this by requiring developers to 'reserve' version numbers on master before they rely on them in a pull request.

I am also considering providing alternative ordering strategies (including omitting version 'numbers' entirely) in later versions.

... when I decided to require code transitions to return true/false?

Some of the code here was written way back when, and I was trying to preserve backward-compatibility. 2.x+ versions will assume transitions succeed unless they throw an exception.

Also, my early career was very C/C++ focused, and returning bools to indicate success/failure is much more of a thing in that world. I wouldn't make that choice today.

Why ...

... are there strongly-typed helpers for things like CreateTable? Why not just SQL all the things?

I tend to SQL all the things, but when I was trying to introduce this to my team, some team members wanted to leave schema changes up to the DBA. As we didn't have a DBA, that was a problem. Some basic helpers made it easier to convince the team to start writing their own schema transitions.

... aren't there more strongly-typed helpers for things like CreateForeignKey?

You can create foreign keys using the fluent syntax when you add a new column. If there's demand, it wouldn't be hard to support creating foreign keys using existing columns.

I really think you should [zzz] in version y.x.

Feel free to ping me on twitter: @rophuine. I might think you're onto something, I might tell you to create a PR if you really want it, or I might tell you I don't think it fits in NSchemer.

Why not just use DbUp/RoundHouse/whatever?

Go for it. None of those were available when I started writing this code - it just took me years to get around to open-sourcing it. I still prefer NSchemer, but that may just come down to familiarity. If you prefer NSchemer to any of the alternatives, please ping me on Twitter @rophuine and let me know why!

Roadmap

  • More fluent configuration options
    • Fluent building of transition list from things like other classes, namespaces, and folders containing .sql files
    • Combining multiple sources of transitions
  • More helper methods for things like foreign keys
  • More ordering options, including dropping version numbers
  • Better documentation

nschemer's People

Contributors

gerrod avatar rophuine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nschemer's Issues

NSchemer gets into an endless loop

To reproduce:

  • Upgrade a database to Version 2
  • Add an upgrade script for Version 1
  • Try to run NSchemer

NSchemer tries to be clever (by ignoring the version 1 script since its lower than Version 2):

// From: SqlClientDatabase.Update()
foreach (ITransition v in Versions)
{
    if (!AllVersions.Contains(v.VersionNumber) && v.VersionNumber < DatabaseVersion)
        missingUpdates.Add(v);
}

However, the IsCurrent() check reports false since the Version 1 script hasn't been applied. So, the loop:

while (!IsCurrent() && appliedUpdate)

Never exits, since appliedUpdate never gets changed.

Default constraint not created when default value is provided

The default value is currently only used when a nullable column is changed to not-null (i.e. when adding a not-null column to an existing table - NSchemer first creates the nullable column, sets all rows to the provided value, and then changes the column to not-null).

What should the API look like here? Should a default constraint always be provided, or are there cases where the developer might want to populate existing rows with a default value without creating a default constraint to populate future rows?

Default assembly for SqlScriptTransition doesn't make sense

SqlScriptTransition provides a constructor that does not require an assembly to be passed. This constructor overloads the constructor that does require an assembly, by passing null as the sourceAssembly argument:

public SqlScriptTransition(double versionNumber, string name, string description, string embeddedResourceName) 
    : this(versionNumber, name, description, null, embeddedResourceName) { }

public SqlScriptTransition(double versionNumber, string name, string description, Assembly sourceAssembly,
    string embeddedResourceName)
{
    SourceAssembly = sourceAssembly ?? GetType().Assembly;
    EmbeddedResourceName = embeddedResourceName;
    Description = description;
    Name = name;
    VersionNumber = versionNumber;
}

The default assembly used in this case then resolves to the NSchemer assembly. This is a nonsensical scenario though - it would be difficult to embed a resource into the NSchemer assembly.

Perhaps a more sensible approach would be to:

  • Throw an exception if sourceAssembly is passed as null; and
  • Either remove the constructor which does not have the sourceAssembly parameter, or provide Assembly.GetCallingAssembly() as the default for sourceAssembly.

'set nocount on' prevents versionnumber from being updated.

If a script has 'set nocount on' and doesn't revert it the sql to update the nschemer_version table with an updated version number returns false and the whole transition fails.

It might be worth programming defensively against this, by ensuring nocount is off.

image

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.