Starcounter
HomeDownloadDocsCommunity
2.3.2
2.3.2
  • Starcounter Documentation
  • Getting Started
  • Starcounter
    • Collapsing the Stack
      • Complexity and Scalability Tradeoff
      • The Future of Micro-Services
      • 10 Benefits of Collapsing the Stack
    • Integrated Database and Web Server
  • Hello World - Tutorial
    • Create a Database Class
    • Create a Real Time UI
    • First Interactive UI
    • Computed Properties
    • Expense Tracker
    • Cancel and Delete
    • The Next Step
  • Guides
    • Database
      • Database Classes
      • Data manipulation
      • Object Identity and Object References
      • Querying with SQL
      • Data Types
      • Relations
      • Inheritance
      • Sharing data
      • Database Configuration
      • Comparing Database Objects
      • Referential Integrity and Constraints
    • SQL
      • Identifiers
      • Path Expressions
      • Data operators
      • Joins
      • Aggregates
      • Comparisons and Logical Operators
      • Sorting
      • Fetch
      • Offset Key
      • Indexes
      • Literals
      • Query Plan Hints
      • Reserved words
      • Query for Database Classes
      • SQL Isolation Between Applications
    • Transactions
      • Short-Running Transactions
      • Long running transactions
      • Using Transactions
      • Running Background Jobs
      • Commit Hooks
      • Post-commit hooks
    • Typed JSON
      • JSON-by-example
      • Code-Behind
      • Data Bindings
      • Callback Methods
      • Responding with JSON
      • Accepting JSON in Requests
      • Primitive Arrays and Single Value Types
      • Typed JSON Internals
    • Blendable Web Apps
      • Starcounter MVVM
      • Palindrom
      • Client-Side Stack
      • Sessions
      • HTML Views
      • App Shell
      • Web Components
      • View Attaching
      • View Composing
      • HTML Compositions
      • HTML Views Blending Guidelines
      • Avoiding CSS conflicts
      • Debugging
    • Network
      • HTTP
      • Internal Self Calls
      • Middleware
      • Anonymous or Substitute Handlers
      • URL Aliases and Redirects
      • Network Gateway
      • Static File Server
      • WebSocket
      • Avoiding URI conflicts
      • TCP Sockets
      • UDP Sockets
    • Publishing Apps
    • Working with Starcounter
      • Release Channels
      • Installation
      • Starting and Stopping Apps
      • Administrator Web UI
      • Star CLI
      • StarAdmin CLI
      • StarDump CLI
      • Working in Visual Studio
      • Error Log
      • Using HTTPS on NGINX
      • Using HTTPS on IIS
      • Run Starcounter in Production
      • Weaver
      • Investigating App Crashes
      • Configuration Structure
      • Database Refactoring
      • Using Unload/Reload to Modify Database Schema
      • Kernel Questions and Answers
      • Log Files
  • Cookbook
    • Attach an HTTP Request to an Existing Long-Running Transaction
    • Cookie-Based Authentication
    • Timestamp on Object Creation
    • Creating Strongly Typed JSON Collections
    • Migrating From 2.2 to 2.3+
    • Multiple Pages
    • Icons
    • Proposed Project Structure
    • Acceptance Testing with Selenium
    • Requesting a User to Authenticate
    • How to delete unused tables and columns
Powered by GitBook
On this page
  • Introduction
  • Unload the Database
  • Modify the Schema in the Database
  • Renaming One Table
  • Renaming Multiple Tables
  • Renaming Columns
  • Modify the Database Schema in the Application
  • Reload the Database
  1. Guides
  2. Working with Starcounter

Using Unload/Reload to Modify Database Schema

PreviousDatabase RefactoringNextKernel Questions and Answers

Last updated 7 years ago

Introduction

When making changes to the database schema of an application, the change has to be made in the database and in the application. This is accomplished by following these steps:

  1. Unload the database using StarDump to SQLite

  2. Modify the schema in the database using DB Browser for SQLite

  3. Modify the schema in the application

  4. Reload the database from SQLite to Starcounter using StarDump

Before starting this process, make sure that there is a backup of the database in case anything goes awry. Information on how to do that can be found in .

Unload the Database

To unload the database, the tool is used. From Starcounter 2.3.0.5427 and 2.4.0.369 it can found at C:\Program Files\Starcounter\stardump.

From the stardump directory, use the stardump unload command to unload the database. For example, to unload the default database to the Desktop, the following would be used:

stardump unload --database default --file C:\Users\[User]\Desktop\Dump

This creates the file Dump.sqlite3 on the desktop.

Modify the Schema in the Database

In order to open the sqlite3 file, it is recommended to use . Using this tool, the schema of the database can be modified. The following examples displays the methods to do almost any schema modification:

Renaming One Table

If we have the following database schema where we want to change "Teenager" to "Teen":

[Database]
public class Person
{
    public string FirstName { get; set; }
}

public class Teenager : Person { }
public class Child : Teenager { }

Then, the needed modification is rather limited. In the DB Browser, after unloading the database, the screen should look something like this:

The classes from the application, Person, Teenager and Child, are at the top followed by the Simplified tables. The Simplified tables can in most cases be disregarded.

Renaming Teenager to Teen requires two changes, renaming the table itself, and modifying the metadata accordingly. To rename the table itself, right click on the table in the view above and choose Modify table.... Then, simply change [ApplicationName].Teenager to [ApplicationName].Teen. To make this change in the metadata, go to the tab Browse Data and find the table Starcounter.Metadata.Table. There, click on [ApplicationName].Teenager and rename it to [ApplicationName].Teen.

Renaming Multiple Tables

Renaming multiple tables by going through them one by one is not an efficient strategy if there are many tables to rename. To solve this, an SQL query can be used to generate SQL that makes the changes at once. For example, if the goal is to rename all the Simplified tables to go by the name of Custom, the following SQL query can be used:

SELECT 'ALTER TABLE `' || Name || '` RENAME TO `Custom.' || substr(Name, 12) || '`;'
FROM `Starcounter.Metadata.Table`
WHERE Name LIKE 'Simplified.%';

This will generate as many SQL queries as there are tables to rename. Copy these generated queries, remove the quotation marks in a text editor, paste them back into the query field in the DB Browser, and run them. It should look something like this:

The tables themselves are now renamed. Lastly, run this query to update the metadata accordingly:

UPDATE `Starcounter.Metadata.Table`
SET Name = 'Custom.' || substr(Name, 12)
WHERE Name LIKE 'Simplified.%';

Renaming Columns

Renaming columns is relatively easy in DB Browser. Consider the case where the goal is to rename the column FirstName to Name in the following model:

[Database]
public class Person
{
    public string FirstName { get; set; }
}

public class Teenager : Person { }
public class Child : Teenager { }

Similar to how tables are renamed, it needs to be renamed in two places: in the table properties and in the metadata.

To rename the column of a specific table, simply right-click on it, in this case Person and choose Modify Table.... Double click on the field FirstName and enter Name. Repeat this step for all the tables that inherits this column, in this example, they are Teenager and Child.

Rename the column in the metadata at Starcounter.Metadata.Column by finding the columns FirstName and rename them to Name. If it's not completely clear which columns that should be renamed, use the TableId to find the right columns.

Modify the Database Schema in the Application

To modify the database schema in the application, simply change it according to the modification done in the database. For example, if the Teenager table was renamed to Teen, the code would be changed from this:

[Database]
public class Person
{
    public string FirstName { get; set; }
}

public class Teenager : Person { }
public class Child : Teenager { }

To this:

[Database]
public class Person
{
    public string FirstName { get; set; }
}

public class Teen : Person { }
public class Child : Teen { }

No matter what change is made, this step is rather trivial. It might even be clever to do this change before modifying the database to get a better overview of the changes.

Reload the Database

To reload the database, use the stardump reload command. For example, to reload the sqlite3 file "Dump" into the default database, the following would be used:

stardump reload --database default --file C:\Users\[User]\Desktop\Dump.sqlite3

If the default database already contains data, it is neccessary to delete it and create a new default database first. That is done using the following two commands:

staradmin -d=default delete --force db
staradmin -d=default new db DefaultUserHttpPort=8080
Run Starcounter in Production
StarDump
DB Browser for SQLite